Compare commits

...

10 Commits
main ... Lab3

Author SHA1 Message Date
002ae2ac6b Lab3 Done++ 2023-11-11 18:10:39 +04:00
7739545975 Lab3 Done+ 2023-11-10 00:11:28 +04:00
385c55967a Lab3 Done 2023-11-10 00:10:11 +04:00
b6af2bfbc6 Lab2 defenetly Done 2023-10-29 18:19:20 +04:00
884f71077d Lab2 Done++ 2023-10-28 16:23:36 +04:00
15220ac426 Lab2 Done+ 2023-10-28 16:22:45 +04:00
b24bc2162b Lab2 Done 2023-10-28 16:12:47 +04:00
e607251cb3 Lab1 Done Done 2023-10-28 15:54:05 +04:00
f6f598adff Lab1 Done+ 2023-10-25 21:05:26 +04:00
dd894ab3ef Lab1 Done 2023-10-25 20:42:47 +04:00
30 changed files with 1222 additions and 4 deletions

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
Main.java

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

BIN
images/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 967 B

BIN
images/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 970 B

BIN
images/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 989 B

BIN
images/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 957 B

View File

@ -0,0 +1,59 @@
package Generics;
import drawing_objects.DrawingAirBomber;
import drawing_objects.DrawingPlane;
import drawing_objects.IDrawEngines;
import entities.EntityAirBomber;
import entities.EntityPlane;
import java.util.Random;
public class HardGeneric<T extends EntityPlane, U extends IDrawEngines> {
T[] planes;
U[] engines;
private int planesNumber;
private int enginesNumber;
private int pictureBoxWidth;
private int pictureBoxHeight;
public HardGeneric(int planesCount, int enginesCount, int width, int height) {
planesNumber = 0;
enginesNumber = 0;
planes = (T[]) new EntityPlane[planesCount];
engines = (U[]) new IDrawEngines[enginesCount];
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public int InsertPlanes(T entityPlane) {
if (planes[planes.length - 1] != null)
return -1;
for (int i = planesNumber - 1; i >= 0; i--) {
planes[i + 1] = planes[i];
}
planesNumber++;
planes[0] = entityPlane;
return 0;
}
public int InsertEngine(U engine) {
if (engines[engines.length - 1] != null)
return -1;
for (int i = enginesNumber - 1; i >= 0; i--) {
engines[i + 1] = engines[i];
}
enginesNumber++;
engines[0] = engine;
return 0;
}
public DrawingPlane makeObject() {
Random rand = new Random();
EntityPlane entity = planes[rand.nextInt(0, planesNumber)];
IDrawEngines engine = engines[rand.nextInt(0, enginesNumber)];
if(entity instanceof EntityAirBomber)
return new DrawingAirBomber(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), ((EntityAirBomber) entity).getAdditionalColor(),
((EntityAirBomber) entity).getBombs(), ((EntityAirBomber) entity).getFuel(), pictureBoxWidth, pictureBoxHeight, engine.getType(), engine.getNumber());
return new DrawingPlane(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), pictureBoxWidth, pictureBoxHeight, engine.getType(), engine.getNumber());
}
}

View File

@ -0,0 +1,123 @@
package Generics;
import drawing_objects.DrawingPlane;
import movement_strategy.IMoveableObject;
import java.awt.*;
public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> {
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private int pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private final int _placeSizeWidth = 170;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private final int _placeSizeHeight = 180;
/// <summary>
/// Набор объектов
/// </summary>
private SetGeneric<T> collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public PlanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
pictureWidth = picWidth;
_pictureHeight = picHeight;
collection = new SetGeneric<T>(width * height, (Class<T>)DrawingPlane.class);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public boolean Insert(T obj)
{
if (obj == null )
return false;
return collection.insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public boolean Remove(int pos)
{
T obj = collection.Get(pos);
if (obj == null)
return false;
return collection.remove(pos);
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U GetU(int pos)
{
if(collection.Get(pos) == null)
return null;
return (U)collection.Get(pos).GetMoveableObject();
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public void ShowPlanes(Graphics2D gr)
{
DrawBackground(gr);
DrawObjects(gr);
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics2D g)
{
BasicStroke pen = new BasicStroke(3);
g.setStroke(pen);
for (int i = 0; i < pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия разметки места
g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight +5,
i * _placeSizeWidth + _placeSizeWidth / 2 + 50, j * _placeSizeHeight +5);
}
g.drawLine(i * _placeSizeWidth + 10, 5, i * _placeSizeWidth + 10,
_pictureHeight / _placeSizeHeight * _placeSizeHeight +5);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics2D g)
{
for (int i = 0; i < collection.getCount(); i++)
{
DrawingPlane plane = collection.Get(i);
if (plane != null)
{
int inRow = pictureWidth / _placeSizeWidth;
plane.setPosition(pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth)-8, i / inRow * _placeSizeHeight + 20);
plane.drawTransport(g);
}
}
}
}

View File

@ -0,0 +1,40 @@
package Generics;
import java.lang.reflect.Array;
public class SetGeneric<T extends Object>{
private final T[] places;
public int getCount() {return places.length;}
public SetGeneric(int count, Class<T> type){
places = (T[])Array.newInstance(type, count);
}
public boolean insert(T plane){
return insert(plane, 0);
}
public boolean insert(T plane, int position){
if (!(position >= 0 && position < places.length))
return false;
if (places[position] != null)
{
int ind = position;
while (ind < places.length && places[ind] != null)
ind++;
if (ind == places.length)
return false;
for (int i = ind - 1; i >= position; i--)
places[i + 1] = places[i];
}
places[position] = plane;
return true;
}
public boolean remove(int position){
if(!(position >= 0 && position < getCount()))
return false;
places[position] = null;
return true;
}
public T Get(int position){
if(!(position >= 0 && position < getCount()))
return null;
return places[position];
}
}

View File

@ -1,5 +1,9 @@
public class Main {
public static void main(String[] args){
import form.FrameAirBomber;
import form.FrameHard;
import form.FramePlaneCollection;
}
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException { new FrameHard(); }
}

View File

@ -0,0 +1,53 @@
package drawing_objects;
import entities.*;
import java.awt.*;
public class DrawingAirBomber extends DrawingPlane {
public DrawingAirBomber(int speed, double weight, Color bodyColor,
Color additionalColor, boolean bombs, boolean fuel, int width, int height, int enginesType, int enginesNumber){
super(speed, weight, bodyColor, width, height, enginesType, enginesNumber);
if(entityPlane != null)
entityPlane = new EntityAirBomber(speed, weight, bodyColor, additionalColor,bombs, fuel);
}
public void drawTransport(Graphics2D g)
{
if (!(entityPlane instanceof EntityAirBomber))
return;
EntityAirBomber entityAirBomber = (EntityAirBomber) entityPlane;
BasicStroke pen = new BasicStroke(2);
Color penColor = Color.BLACK;
Color additionalColor = entityAirBomber.getAdditionalColor();
g.setStroke(pen);
super.drawTransport(g);
// топливо
if (entityAirBomber.getFuel())
{
g.setColor(additionalColor);
g.fillOval(this.startPosX + 60, this.startPosY - 1, 40, 10);
g.fillOval(this.startPosX + 60, this.startPosY + 150, 40, 10);
g.setColor(penColor);
g.drawOval(startPosX + 60, startPosY - 1, 40, 10);
g.drawOval(startPosX + 60, startPosY + 150, 40, 10);
}
//бомбы
if (entityAirBomber.getBombs())
{
int[] pointX = new int[]{startPosX+50, startPosX+70, startPosX+80, startPosX+90, startPosX+90, startPosX+80, startPosX+70, startPosX+50};
int[] pointY = new int[]{startPosY+75, startPosY+75, startPosY+80, startPosY+75, startPosY+85, startPosY+80, startPosY+85, startPosY+85};
g.setColor(additionalColor);
g.fillPolygon(pointX, pointY, 8);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 8);
pointX = new int[]{startPosX+100, startPosX+120, startPosX+130, startPosX+140, startPosX+140, startPosX+130, startPosX+120, startPosX+100};
pointY = new int[]{startPosY+75, startPosY+75, startPosY+80, startPosY+75, startPosY+85, startPosY+80, startPosY+85, startPosY+85};
g.setColor(additionalColor);
g.fillPolygon(pointX, pointY, 8);
g.setColor(penColor);
g.drawPolygon(pointX, pointY,8);
}
}
}

View File

@ -0,0 +1,44 @@
package drawing_objects;
import enums.*;
import java.awt.*;
public class DrawingEnginesNotRounded implements IDrawEngines{
private EngineNumber number;
@Override
public int getType() {
return 1;
}
public int getNumber(){
int x = 0;
if(number == EngineNumber.TWO)
x = 2;
if(number == EngineNumber.FOUR)
x = 4;
if(number == EngineNumber.SIX)
x = 6;
return x;
}
public void setNumber(int x){
if(x <= 2)
number = EngineNumber.TWO;
if(x == 4)
number = EngineNumber.FOUR;
if(x >= 6)
number = EngineNumber.SIX;
}
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
graphics2D.fillRect(_startX+70, _startY+20, 20, 15);
graphics2D.fillRect(_startX+70, _startY+125, 20, 15);
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
graphics2D.fillRect(_startX+70, _startY+40, 20, 15);
graphics2D.fillRect(_startX+70, _startY+105, 20, 15);
}
if (number == EngineNumber.SIX){
graphics2D.fillRect(_startX+130, _startY+50, 25, 15);
graphics2D.fillRect(_startX+130, _startY+95, 25, 15);
}
}
}

View File

@ -0,0 +1,50 @@
package drawing_objects;
import enums.*;
import java.awt.*;
public class DrawingEnginesRoundedBack implements IDrawEngines {
private EngineNumber number;
@Override
public int getType() {
return 0;
}
public int getNumber(){
int x = 0;
if(number == EngineNumber.TWO)
x = 2;
if(number == EngineNumber.FOUR)
x = 4;
if(number == EngineNumber.SIX)
x = 6;
return x;
}
public void setNumber(int x){
if(x <= 2)
number = EngineNumber.TWO;
if(x == 4)
number = EngineNumber.FOUR;
if(x >= 6)
number = EngineNumber.SIX;
}
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
graphics2D.fillRect(_startX+70, _startY+20, 20, 15);
graphics2D.fillOval(_startX+80, _startY+20, 20, 15);
graphics2D.fillRect(_startX+70, _startY+125, 20, 15);
graphics2D.fillOval(_startX+80, _startY+125, 20, 15);
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
graphics2D.fillRect(_startX+70, _startY+40, 20, 15);
graphics2D.fillOval(_startX+80, _startY+40, 20, 15);
graphics2D.fillRect(_startX+70, _startY+105, 20, 15);
graphics2D.fillOval(_startX+80, _startY+105, 20, 15);
}
if (number == EngineNumber.SIX){
graphics2D.fillRect(_startX+130, _startY+50, 25, 15);
graphics2D.fillOval(_startX+145, _startY+50, 20, 15);
graphics2D.fillRect(_startX+130, _startY+95, 25, 15);
graphics2D.fillOval(_startX+145, _startY+95, 20, 15);
}
}
}

View File

@ -0,0 +1,50 @@
package drawing_objects;
import enums.*;
import java.awt.*;
public class DrawingEnginesRoundedFront implements IDrawEngines {
private EngineNumber number;
@Override
public int getType() {
return 2;
}
public int getNumber(){
int x = 0;
if(number == EngineNumber.TWO)
x = 2;
if(number == EngineNumber.FOUR)
x = 4;
if(number == EngineNumber.SIX)
x = 6;
return x;
}
public void setNumber(int x){
if(x <= 2)
number = EngineNumber.TWO;
if(x == 4)
number = EngineNumber.FOUR;
if(x >= 6)
number = EngineNumber.SIX;
}
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
graphics2D.fillRect(_startX+70, _startY+20, 20, 15);
graphics2D.fillOval(_startX+60, _startY+20, 20, 15);
graphics2D.fillRect(_startX+70, _startY+125, 20, 15);
graphics2D.fillOval(_startX+60, _startY+125, 20, 15);
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
graphics2D.fillRect(_startX+70, _startY+40, 20, 15);
graphics2D.fillOval(_startX+60, _startY+40, 20, 15);
graphics2D.fillRect(_startX+70, _startY+105, 20, 15);
graphics2D.fillOval(_startX+60, _startY+105, 20, 15);
}
if (number == EngineNumber.SIX){
graphics2D.fillRect(_startX+130, _startY+50, 25, 15);
graphics2D.fillOval(_startX+120, _startY+50, 20, 15);
graphics2D.fillRect(_startX+130, _startY+95, 25, 15);
graphics2D.fillOval(_startX+120, _startY+95, 20, 15);
}
}
}

View File

@ -0,0 +1,143 @@
package drawing_objects;
import entities.EntityPlane;
import enums.*;
import movement_strategy.DrawingObjectPlane;
import movement_strategy.IMoveableObject;
import java.awt.*;
public class DrawingPlane {
protected EntityPlane entityPlane;
protected void setEntityPlane(EntityPlane entityPlane){this.entityPlane = entityPlane;}
public EntityPlane getEntityPlane() {
return entityPlane;
}
private IDrawEngines drawingEngines;
private int pictureWidth;
private int pictureHeight;
protected int startPosX;
public int getPosX(){return startPosX;}
protected int startPosY;
public int getPosY(){return startPosY;}
private final int PLANE_WIDTH = 160;
public int getWidth(){return PLANE_WIDTH;}
private final int PLANE_HEIGHT = 160;
public int getHeight(){return PLANE_HEIGHT;}
public DrawingPlane(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) {
if (width < PLANE_WIDTH || height < PLANE_HEIGHT)
return;
pictureWidth = width;
pictureHeight = height;
entityPlane = new EntityPlane(speed, weight, bodyColor);
switch (enginesType){
case 1:
drawingEngines = new DrawingEnginesNotRounded();
break;
case 2:
drawingEngines = new DrawingEnginesRoundedFront();
break;
default:
drawingEngines = new DrawingEnginesRoundedBack();
break;
}
drawingEngines.setNumber(enginesNumber);
}
public IMoveableObject GetMoveableObject() {return new DrawingObjectPlane(this);}
public void setPosition(int x, int y) {
if (x < 0 || y < 0 || x + PLANE_WIDTH > pictureWidth || y + PLANE_HEIGHT > pictureHeight)
x = y = 0;
startPosX = x;
startPosY = y;
}
public void drawTransport(Graphics2D g) {
if (entityPlane == null)
return;
BasicStroke pen = new BasicStroke(2);
Color penColor = Color.BLACK;
Color bodyColor = entityPlane.getBodyColor();
g.setStroke(pen);
g.setColor(bodyColor);
//фюзеляж
g.fillRect(startPosX + 20, startPosY + 70, 140, 20);
//кабина
int[] pointX = new int[]{startPosX, startPosX + 20, startPosX + 20};
int[] pointY = new int[]{startPosY + 80, startPosY + 70, startPosY + 90};
g.setColor(Color.BLUE);
g.fillPolygon(pointX, pointY, 3);
//границы самолета
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 3);
g.drawRect(startPosX + 20, startPosY + 70, 140, 20);
//Крылья
pointX = new int[]{startPosX + 70, startPosX + 70, startPosX + 90, startPosX + 100};
pointY = new int[]{startPosY + 70, startPosY, startPosY, startPosY + 70};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
pointX = new int[]{startPosX + 70, startPosX + 70, startPosX + 90, startPosX + 100};
pointY = new int[]{startPosY + 90, startPosY + 160, startPosY + 160, startPosY + 90};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
pointX = new int[]{startPosX + 130, startPosX + 130, startPosX + 160, startPosX + 160};
pointY = new int[]{startPosY + 70, startPosY + 50, startPosY + 30, startPosY + 70};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
pointX = new int[]{startPosX + 130, startPosX + 130, startPosX + 160, startPosX + 160};
pointY = new int[]{startPosY + 90, startPosY + 110, startPosY + 130, startPosY + 90};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
//двигатели
drawingEngines.drawEngines(g, startPosX, startPosY);
}
public boolean canMove(DirectionType direction) {
if (entityPlane == null) {
return false;
}
switch(direction) {
case LEFT:
return startPosX - entityPlane.step.get().intValue() > 0;
case UP:
return startPosY - entityPlane.step.get().intValue() > 0;
case RIGHT:
return startPosX + entityPlane.step.get().intValue() + PLANE_WIDTH < pictureWidth;
case DOWN:
return startPosY + entityPlane.step.get().intValue() + PLANE_WIDTH < pictureHeight;
default:
return false;
}
}
public void moveTransport(DirectionType direction)
{
if (!canMove(direction) || entityPlane == null)
return;
switch (direction)
{
//влево
case LEFT:
startPosX -= entityPlane.step.get().intValue();
break;
//вверх
case UP:
startPosY -= entityPlane.step.get().intValue();
break;
// вправо
case RIGHT:
startPosX += entityPlane.step.get().intValue();
break;
//вниз
case DOWN:
startPosY += entityPlane.step.get().intValue();
break;
}
}
}

View File

@ -0,0 +1,10 @@
package drawing_objects;
import java.awt.*;
public interface IDrawEngines {
public int getType();
public int getNumber();
public void setNumber(int x);
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY);
}

View File

@ -0,0 +1,25 @@
package entities;
import java.awt.*;
public class EntityAirBomber extends EntityPlane {
private Color additionalColor;
public Color getAdditionalColor(){
return additionalColor;
}
private boolean isFuel;
public boolean getFuel() {
return isFuel;
}
private boolean isBombs;
public boolean getBombs() {
return isBombs;
}
public EntityAirBomber(int speed, double weight, Color bodyColor, Color
additionalColor, boolean isBombs, boolean isFuel) {
super(speed, weight, bodyColor);
this.additionalColor = additionalColor;
this.isFuel = isFuel;
this.isBombs = isBombs;
}
}

View File

@ -0,0 +1,24 @@
package entities;
import java.awt.*;
import java.util.function.Supplier;
public class EntityPlane {
private int speed;
public int getSpeed() {
return speed;
}
private double weight;
public double getWeight() {
return weight;
}
private Color bodyColor;
public Color getBodyColor() {
return bodyColor;
}
public Supplier<Double> step = () -> (double) speed * 100 / weight;
public EntityPlane(int speed, double weight, Color bodyColor){
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
}
}

View File

@ -0,0 +1,8 @@
package enums;
public enum DirectionType {
UP,
DOWN,
LEFT,
RIGHT
}

View File

@ -0,0 +1,7 @@
package enums;
public enum EngineNumber {
TWO,
FOUR,
SIX
}

7
src/enums/Status.java Normal file
View File

@ -0,0 +1,7 @@
package enums;
public enum Status {
NOTINIT,
INPROGRESS,
FINISH
}

View File

@ -0,0 +1,203 @@
package form;
import drawing_objects.DrawingAirBomber;
import drawing_objects.DrawingPlane;
import enums.*;
import movement_strategy.*;
import movement_strategy.DrawingObjectPlane;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class FrameAirBomber extends JFrame {
private DrawingPlane drawingPlane;
private AbstractStrategy abstractStrategy;
public JButton selectPlaneButton;
private DrawingPlane selectedPlane;
public DrawingPlane getSelectedPlane() {
return selectedPlane;
}
private JComboBox comboBoxStrategy;
private final JComponent pictureBox;
public FrameAirBomber() throws IOException {
super("Бомбардировщик");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//components initialisation
pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingPlane != null) drawingPlane.drawTransport(graphics2D);
super.repaint();
}
};
comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"});
JButton stepButton = new JButton("Шаг");
JButton createPlaneButton = new JButton("Создать самолет");
JButton createAirBomberButton = new JButton("Создать бомбардировщик");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
selectPlaneButton = new JButton("Выбрать самолет");
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
//ActionListeners and ActionCommand addition
createPlaneButton.addActionListener(e -> buttonCreatePlaneClick());
createAirBomberButton.addActionListener(e -> buttonCreateAirBomberClick());
stepButton.addActionListener(e -> buttonStepClick());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left");
leftButton.addActionListener(this::buttonMoveClick);
upButton.setActionCommand("up");
upButton.addActionListener(this::buttonMoveClick);
downButton.setActionCommand("down");
downButton.addActionListener(this::buttonMoveClick);
//component addition
setLayout(new BorderLayout());
JPanel panelAirBomber = new JPanel(new BorderLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
//createPanel
JPanel createPanel = new JPanel(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 0;
createPanel.add(createPlaneButton, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
createPanel.add(createAirBomberButton, constraints);
constraints.gridx = 2;
constraints.gridy = 0;
createPanel.add(selectPlaneButton, constraints);
//movementPanel
JPanel movementPanel = new JPanel(new GridBagLayout());
rightButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 2;
constraints.gridy = 1;
movementPanel.add(rightButton, constraints);
leftButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 0;
constraints.gridy = 1;
movementPanel.add(leftButton, constraints);
upButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1;
constraints.gridy = 0;
movementPanel.add(upButton, constraints);
downButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1;
constraints.gridy = 1;
movementPanel.add(downButton, constraints);
//stepPanel
JPanel stepPanel = new JPanel(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 0;
stepPanel.add(comboBoxStrategy, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
stepPanel.add(stepButton, constraints);
//addition to frame
add(pictureBox);
rightPanel.add(movementPanel, BorderLayout.SOUTH);
rightPanel.add(stepPanel, BorderLayout.NORTH);
leftPanel.add(createPanel, BorderLayout.SOUTH);
panelAirBomber.add(rightPanel, BorderLayout.EAST);
panelAirBomber.add(leftPanel, BorderLayout.WEST);
add(panelAirBomber,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateAirBomberClick() {
Random random = new Random();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
Color additionalColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
drawingPlane = new DrawingAirBomber(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor,
additionalColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonCreatePlaneClick(){
Random random = new Random();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
drawingPlane = new DrawingPlane(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor,
pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonStepClick(){
if (drawingPlane == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
;
switch(comboBoxStrategy.getSelectedIndex()) {
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.SetData(new DrawingObjectPlane(drawingPlane), pictureBox.getWidth(),
pictureBox.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null)
{
return;
}
abstractStrategy.MakeStep();
draw();
if (abstractStrategy.GetStatus() == Status.FINISH)
{
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
private void buttonMoveClick(ActionEvent event) {
if(drawingPlane == null || drawingPlane.getEntityPlane() == null)
return;
switch (event.getActionCommand())
{
case "left":
drawingPlane.moveTransport(DirectionType.LEFT);
break;
case "right":
drawingPlane.moveTransport(DirectionType.RIGHT);
break;
case "up":
drawingPlane.moveTransport(DirectionType.UP);
break;
case "down":
drawingPlane.moveTransport(DirectionType.DOWN);
break;
}
draw();
}
private void draw() {
if (drawingPlane == null)
return;
pictureBox.repaint();
}
public void select(){
if (drawingPlane == null) {
return;
}
selectedPlane = drawingPlane;
}
}

81
src/form/FrameHard.java Normal file
View File

@ -0,0 +1,81 @@
package form;
import Generics.HardGeneric;
import drawing_objects.*;
import entities.EntityAirBomber;
import entities.EntityPlane;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FrameHard extends JFrame {
HardGeneric<EntityPlane, IDrawEngines> generic;
DrawingPlane drawing;
private JComponent pictureBox;
private final int pictureBoxWidth = 200;
private final int pictureBoxHeight = 200;
public FrameHard() {
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pictureBox = new JComponent() {
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawing != null) drawing.drawTransport(graphics2D);
super.repaint();
}
};
pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight));
JButton buttonMakeObject = new JButton("Создать новый объект");
Random rand = new Random();
int size = rand.nextInt(1, 10);
generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight);
for (int i = 0; i < size; i++) {
generic.InsertPlanes(makeRandomPlane());
generic.InsertEngine(makeRandomEngine());
}
buttonMakeObject.addActionListener(e -> {
DrawingPlane drawingPlane = generic.makeObject();
drawingPlane.setPosition(pictureBoxWidth / 2 - drawingPlane.getWidth() / 2, pictureBoxHeight / 2 - drawingPlane.getHeight() / 2);
drawing = drawingPlane;
draw();
});
setLayout(new BorderLayout());
add(pictureBox, BorderLayout.CENTER);
add(buttonMakeObject, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public EntityPlane makeRandomPlane() {
Random rand = new Random();
EntityPlane plane;
switch (rand.nextInt(2)){
case 1 -> plane = new EntityAirBomber(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
rand.nextBoolean(), rand.nextBoolean());
default -> plane = new EntityPlane(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
}
return plane;
}
public IDrawEngines makeRandomEngine() {
Random random = new Random();
IDrawEngines engines;
switch (random.nextInt(3)) {
case 1 -> engines = new DrawingEnginesNotRounded();
case 2 -> engines = new DrawingEnginesRoundedFront();
default -> engines = new DrawingEnginesRoundedBack();
}
engines.setNumber((random.nextInt(3) + 1) * 2);
return engines;
}
void draw() {
pictureBox.repaint();
}
}

View File

@ -0,0 +1,108 @@
package form;
import javax.swing.*;
import Generics.PlanesGenericCollection;
import drawing_objects.DrawingPlane;
import movement_strategy.DrawingObjectPlane;
import java.awt.*;
import java.io.IOException;
public class FramePlaneCollection extends JFrame {
private PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> planes;
JComponent pictureBoxCollection;
TextField textFieldNumber;
public FramePlaneCollection(){
super("Набор самолетов");
setSize(new Dimension(900,600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
creteGUI();
planes = new PlanesGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
pictureBoxCollection.repaint();
setVisible(true);
}
public void creteGUI(){
pictureBoxCollection = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (planes != null) planes.ShowPlanes(graphics2D);
super.repaint();
}
};
pictureBoxCollection.setBounds(0, 0, 700, 600);
JButton buttonAddPlane = new JButton("Добавить самолет");
textFieldNumber = new TextField();
JButton buttonRemovePlane = new JButton("Удалить самолет");
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
//ActionListeners
buttonAddPlane.addActionListener(e -> ButtonAddPlaneClick());
buttonRemovePlane.addActionListener(e -> ButtonRemovePlaneClick());
buttonRefreshCollection.addActionListener(e -> ButtonRefreshCollectionClick());
//addition to panel
JPanel panelCollection = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.right = 2;
constraints.insets.top = constraints.insets.bottom = 20;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
panelCollection.add(buttonAddPlane, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panelCollection.add(textFieldNumber, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
panelCollection.add(buttonRemovePlane, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
panelCollection.add(buttonRefreshCollection, constraints);
JPanel upperPanel = new JPanel(new BorderLayout());
setLayout(new BorderLayout());
add(panelCollection, BorderLayout.EAST);
add(pictureBoxCollection, BorderLayout.CENTER);
}
private void ButtonAddPlaneClick(){
FrameAirBomber form;
try {
form = new FrameAirBomber();
} catch (IOException e){
throw new RuntimeException(e);
}
form.selectPlaneButton.addActionListener(e -> {
form.select();
var selectedPlane = form.getSelectedPlane();
form.dispose();
if (planes.Insert(selectedPlane))
{
JOptionPane.showMessageDialog(this, "Объект добавлен");
pictureBoxCollection.repaint();
}
else
{
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
}
});
}
private void ButtonRemovePlaneClick()
{
int pos = Integer.parseInt(textFieldNumber.getText());
if (planes.Remove(pos))
{
JOptionPane.showMessageDialog(this, "Объект удален");
pictureBoxCollection.repaint();
}
else
{
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
}
}
private void ButtonRefreshCollectionClick()
{
pictureBoxCollection.repaint();
}
}

View File

@ -0,0 +1,60 @@
package movement_strategy;
import enums.DirectionType;
import enums.Status;
import java.util.function.Supplier;
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private Status state = Status.NOTINIT;
private int FieldWidth;
protected int getFieldWidth(){return FieldWidth;}
private int FieldHeight;
protected int getFieldHeight(){return FieldHeight;}
public Status GetStatus() {return state;}
public void SetData(IMoveableObject moveableObject, int width, int height){
if(moveableObject == null){
state = Status.NOTINIT;
return;
}
state = Status.INPROGRESS;
this.moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep(){
if(state != Status.INPROGRESS)
return;
if(IsTargetDestination()){
state = Status.FINISH;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() {return MoveTo(DirectionType.LEFT);}
protected boolean MoveRight() {return MoveTo(DirectionType.RIGHT);}
protected boolean MoveUp() {return MoveTo(DirectionType.UP);}
protected boolean MoveDown() {return MoveTo(DirectionType.DOWN);}
protected Supplier<ObjectParameters> getObjectParameters = () -> moveableObject.getObjectPosition();
protected Integer GetStep(){
if(state != Status.INPROGRESS)
return null;
return moveableObject.getStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
private boolean MoveTo(DirectionType direction){
if(state != Status.INPROGRESS)
return false;
if(moveableObject.checkCanMove(direction)){
moveableObject.moveObject(direction);
return true;
}
return false;
}
}

View File

@ -0,0 +1,30 @@
package movement_strategy;
import drawing_objects.DrawingPlane;
import enums.DirectionType;
public class DrawingObjectPlane implements IMoveableObject{
private final DrawingPlane drawingPlane;
public DrawingObjectPlane(DrawingPlane drawingPlane){
this.drawingPlane = drawingPlane;
}
public ObjectParameters getObjectPosition(){
if(drawingPlane == null || drawingPlane.getEntityPlane() == null)
return null;
return new ObjectParameters(drawingPlane.getPosX(), drawingPlane.getPosY(),
drawingPlane.getWidth(), drawingPlane.getHeight());
}
public int getStep(){
if(drawingPlane.getEntityPlane() == null)
return 0;
return drawingPlane.getEntityPlane().step.get().intValue();
}
public boolean checkCanMove(DirectionType direction){
if(drawingPlane == null)
return false;
return drawingPlane.canMove(direction);
}
public void moveObject(DirectionType direction){
drawingPlane.moveTransport(direction);
}
}

View File

@ -0,0 +1,8 @@
package movement_strategy;
import enums.DirectionType;
public interface IMoveableObject {
ObjectParameters getObjectPosition();
int getStep();
boolean checkCanMove(DirectionType direction);
void moveObject(DirectionType direction);
}

View File

@ -0,0 +1,24 @@
package movement_strategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestination() {
var objParams = getObjectParameters.get();
if(objParams == null)
return false;
return objParams.getRightBorder() + GetStep() >= getFieldWidth() &&
objParams.getDownBorder() + GetStep() >= getFieldHeight();
}
@Override
protected void MoveToTarget() {
var objParams = getObjectParameters.get();
if(objParams == null)
return;
var diffX = objParams.getRightBorder() - getFieldWidth();
if (Math.abs(diffX) > GetStep())
MoveRight();
var diffY = objParams.getDownBorder() - getFieldHeight();
if (Math.abs(diffY) > GetStep())
MoveDown();
}
}

View File

@ -0,0 +1,35 @@
package movement_strategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestination() {
var objParams = getObjectParameters.get();
if(objParams == null)
return false;
return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 &&
objParams.getObjectMiddleHorizontal()+GetStep() >= getFieldWidth() / 2 &&
objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 &&
objParams.getObjectMiddleVertical() + GetStep() >= getFieldHeight() / 2;
}
@Override
protected void MoveToTarget() {
var objParams = getObjectParameters.get();
if(objParams == null)
return;
var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2;
if(Math.abs(diffX) > GetStep()){
if(diffX > 0)
MoveLeft();
else
MoveRight();
}
var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2;
if(Math.abs(diffY) > GetStep()){
if(diffY > 0)
MoveUp();
else
MoveDown();
}
}
}

View File

@ -0,0 +1,21 @@
package movement_strategy;
public class ObjectParameters {
private final int POS_X;
private final int POS_Y;
private final int WIDTH;
private final int HEIGHT;
public int getLeftBorder() {return POS_X;}
public int getTopBorder() {return POS_Y;}
public int getRightBorder() {return POS_X + WIDTH;}
public int getDownBorder() {return POS_Y + HEIGHT;}
public int getObjectMiddleHorizontal() {return POS_X + this.WIDTH / 2;}
public int getObjectMiddleVertical() {return POS_Y + this.HEIGHT / 2;}
public ObjectParameters(int x, int y, int width, int height)
{
POS_X = x;
POS_Y = y;
WIDTH = width;
HEIGHT = height;
}
}