From eabca5415f6c32f00106b8bcc33aab567b774049 Mon Sep 17 00:00:00 2001 From: Salikh Date: Sat, 30 Dec 2023 09:28:39 +0400 Subject: [PATCH] second commit --- src/AbstractStrategy.java | 1 - src/BomberGenericCollection.java | 118 +++++++++++++++++++++++++++++++ src/DrawingAir.java | 57 +++++++-------- src/DrawingEnginesRound.java | 20 +++++- src/DrawingEnginesSquare.java | 42 +++++++++-- src/DrawingObjectPlane.java | 25 +++++++ src/EntityAir.java | 10 ++- src/EntityAirBomber.java | 5 +- src/FrameAirBomber.java | 49 +++++++++---- src/FrameHard.java | 77 ++++++++++++++++++++ src/FramePlaneCollection.java | 102 ++++++++++++++++++++++++++ src/HardGeneric.java | 51 +++++++++++++ src/IDrawEngines.java | 2 + src/Main.java | 2 +- src/MoveToBorder.java | 20 ++---- src/MoveToCenter.java | 7 +- src/ObjectParameters.java | 28 ++++---- src/SetGeneric.java | 39 ++++++++++ 18 files changed, 555 insertions(+), 100 deletions(-) create mode 100644 src/BomberGenericCollection.java create mode 100644 src/DrawingObjectPlane.java create mode 100644 src/FrameHard.java create mode 100644 src/FramePlaneCollection.java create mode 100644 src/HardGeneric.java create mode 100644 src/SetGeneric.java diff --git a/src/AbstractStrategy.java b/src/AbstractStrategy.java index 4a70f71..8f1208f 100644 --- a/src/AbstractStrategy.java +++ b/src/AbstractStrategy.java @@ -1,5 +1,4 @@ import java.util.function.Supplier; - public abstract class AbstractStrategy { private IMoveableObject moveableObject; private Status state = Status.NOTINIT; diff --git a/src/BomberGenericCollection.java b/src/BomberGenericCollection.java new file mode 100644 index 0000000..2b33df8 --- /dev/null +++ b/src/BomberGenericCollection.java @@ -0,0 +1,118 @@ +import java.awt.*; + +public class BomberGenericCollection { + /// + /// Ширина окна прорисовки + /// + private int pictureWidth; + /// + /// Высота окна прорисовки + /// + private int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private final int _placeSizeWidth = 170; + /// + /// Размер занимаемого объектом места (высота) + /// + private final int _placeSizeHeight = 180; + /// + /// Набор объектов + /// + private SetGeneric collection; + /// + /// Конструктор + /// + /// + /// + public BomberGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + pictureWidth = picWidth; + _pictureHeight = picHeight; + collection = new SetGeneric(width * height, (Class) DrawingAir.class); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public boolean Insert(T obj) + { + if (obj == null ) + return false; + return collection.insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public boolean Remove(int pos) + { + T obj = collection.Get(pos); + if (obj == null) + return false; + return collection.remove(pos); + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U GetU(int pos) + { + if(collection.Get(pos) == null) + return null; + return (U)collection.Get(pos).GetMoveableObject(); + } + /// + /// Вывод всего набора объектов + /// + /// + public void ShowPlanes(Graphics2D gr) + { + DrawBackground(gr); + DrawObjects(gr); + } + /// + /// Метод отрисовки фона + /// + /// + 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); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics2D g) + { + for (int i = 0; i < collection.getCount(); i++) + { + DrawingAir bomber = collection.Get(i); + if (bomber != null) + { + int inRow = pictureWidth / _placeSizeWidth; + bomber.setPosition(pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth)-8, i / inRow * _placeSizeHeight + 20); + bomber.drawTransport(g); + } + } + } +} diff --git a/src/DrawingAir.java b/src/DrawingAir.java index 11fa3ad..feada3e 100644 --- a/src/DrawingAir.java +++ b/src/DrawingAir.java @@ -2,31 +2,21 @@ import java.awt.*; public class DrawingAir { protected EntityAir entityAir; - protected void setEntityPlane(EntityAir entityPlane){ - this.entityAir = entityAir; - } - public EntityAir getEntityPlane() { + protected void setEntityAir(EntityAir entityAir){this.entityAir = entityAir;} + public EntityAir getEntityAir() { return entityAir; } private IDrawEngines drawingEngines; private int _pictureWidth; private int _pictureHeight; protected int _startPosX; - public int getPosX(){ - return _startPosX; - } + public int getPosX(){return _startPosX;} protected int _startPosY; - public int getPosY(){ - return _startPosY; - } + public int getPosY(){return _startPosY;} private final int _PlaneWidth = 160; - public int getWidth() { - return _PlaneWidth; - } + public int getWidth(){return _PlaneWidth;} private final int _PlaneHeight = 160; - public int getHeight(){ - return _PlaneHeight; - } + public int getHeight(){return _PlaneHeight;} public DrawingAir(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) { if (width < _PlaneWidth || height < _PlaneHeight) return; @@ -35,21 +25,23 @@ public class DrawingAir { entityAir = new EntityAir(speed, weight, bodyColor); switch (enginesType){ case 1: - drawingEngines = new DrawingEnginesSquare(); + drawingEngines = new DrawingEnginesOval(); break; case 2: drawingEngines = new DrawingEnginesRound(); break; default: - drawingEngines = new DrawingEnginesOval(); + drawingEngines = new DrawingEnginesSquare(); break; } drawingEngines.setNumber(enginesNumber); } - public void setPosition(int x, int y) - { - if (x <= _pictureWidth - _PlaneWidth && x >= 0 && y <= _pictureHeight - _PlaneHeight && y >= 0) - x = y = 2; + + public IMoveableObject GetMoveableObject() {return new DrawingObjectPlane(this);} + + public void setPosition(int x, int y) { + if (x < 0 || y < 0 || x + _PlaneWidth > _pictureWidth || y + _PlaneHeight > _pictureHeight) + x = y = 0; _startPosX = x; _startPosY = y; } @@ -111,33 +103,32 @@ public class DrawingAir { case RIGHT: return _startPosX + entityAir.step.get().intValue() + _PlaneWidth < _pictureWidth; case DOWN: - return _startPosY + entityAir.step.get().intValue() + _PlaneWidth < _pictureWidth; + return _startPosY + entityAir.step.get().intValue() + _PlaneWidth < _pictureHeight; default: return false; } } public void moveTransport(DirectionType direction) { - if (entityAir == null) + if (!canMove(direction) || entityAir == null) return; - int step = entityAir.step.get().intValue(); switch (direction) { + //влево case LEFT: - if (_startPosX - step > 0) - _startPosX -= step; + _startPosX -= entityAir.step.get().intValue(); break; + //вверх case UP: - if (_startPosY - step > 0) - _startPosY -= step; + _startPosY -= entityAir.step.get().intValue(); break; + // вправо case RIGHT: - if (_startPosX + _PlaneWidth + step < _pictureWidth) - _startPosX += step; + _startPosX += entityAir.step.get().intValue(); break; + //вниз case DOWN: - if (_startPosY + _PlaneHeight + step < _pictureHeight) - _startPosY += step; + _startPosY += entityAir.step.get().intValue(); break; } } diff --git a/src/DrawingEnginesRound.java b/src/DrawingEnginesRound.java index b6587df..a8ff259 100644 --- a/src/DrawingEnginesRound.java +++ b/src/DrawingEnginesRound.java @@ -1,7 +1,23 @@ import java.awt.*; -public class DrawingEnginesRound implements IDrawEngines{ +public class DrawingEnginesRound 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; @@ -22,4 +38,4 @@ public class DrawingEnginesRound implements IDrawEngines{ graphics2D.fillOval(_startX+135, _startY+95, 20, 15); } } -} +} \ No newline at end of file diff --git a/src/DrawingEnginesSquare.java b/src/DrawingEnginesSquare.java index a5b45cc..c2aee8e 100644 --- a/src/DrawingEnginesSquare.java +++ b/src/DrawingEnginesSquare.java @@ -1,7 +1,23 @@ import java.awt.*; -public class DrawingEnginesSquare implements IDrawEngines{ +public class DrawingEnginesSquare 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; @@ -11,15 +27,27 @@ public class DrawingEnginesSquare implements IDrawEngines{ number = EngineNumber.SIX; } public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ - graphics2D.fillRect(_startX+75, _startY+20, 15, 15); - graphics2D.fillRect(_startX+75, _startY+125, 15, 15); + graphics2D.fillOval(_startX+85, _startY+20, 20, 15); + graphics2D.fillRect(_startX+75, _startY+20, 20, 15); + graphics2D.fillOval(_startX+65, _startY+20, 20, 15); + graphics2D.fillOval(_startX+85, _startY+125, 20, 15); + graphics2D.fillRect(_startX+75, _startY+125, 20, 15); + graphics2D.fillOval(_startX+65, _startY+125, 20, 15); if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ - graphics2D.fillRect(_startX+75, _startY+40, 15, 15); - graphics2D.fillRect(_startX+75, _startY+105, 15, 15); + graphics2D.fillOval(_startX+85, _startY+40, 20, 15); + graphics2D.fillRect(_startX+75, _startY+40, 20, 15); + graphics2D.fillOval(_startX+65, _startY+40, 20, 15); + graphics2D.fillOval(_startX+85, _startY+105, 20, 15); + graphics2D.fillRect(_startX+75, _startY+105, 20, 15); + graphics2D.fillOval(_startX+65, _startY+105, 20, 15); } if (number == EngineNumber.SIX){ - graphics2D.fillRect(_startX+140, _startY+50, 15, 15); - graphics2D.fillRect(_startX+140, _startY+95, 15, 15); + graphics2D.fillOval(_startX+145, _startY+50, 20, 15); + graphics2D.fillRect(_startX+135, _startY+50, 25, 15); + graphics2D.fillOval(_startX+125, _startY+50, 20, 15); + graphics2D.fillOval(_startX+145, _startY+95, 20, 15); + graphics2D.fillRect(_startX+135, _startY+95, 25, 15); + graphics2D.fillOval(_startX+125, _startY+95, 20, 15); } } } \ No newline at end of file diff --git a/src/DrawingObjectPlane.java b/src/DrawingObjectPlane.java new file mode 100644 index 0000000..bf0b405 --- /dev/null +++ b/src/DrawingObjectPlane.java @@ -0,0 +1,25 @@ +public class DrawingObjectPlane implements IMoveableObject{ + private final DrawingAir drawingPlane; + public DrawingObjectPlane(DrawingAir drawingPlane){ + this.drawingPlane = drawingPlane; + } + public ObjectParameters getObjectPosition(){ + if(drawingPlane == null || drawingPlane.getEntityAir() == null) + return null; + return new ObjectParameters(drawingPlane.getPosX(), drawingPlane.getPosY(), + drawingPlane.getWidth(), drawingPlane.getHeight()); + } + public int getStep(){ + if(drawingPlane.getEntityAir() == null) + return 0; + return drawingPlane.getEntityAir().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); + } +} diff --git a/src/EntityAir.java b/src/EntityAir.java index 514b824..aeea6a7 100644 --- a/src/EntityAir.java +++ b/src/EntityAir.java @@ -1,21 +1,19 @@ import java.awt.*; import java.util.function.Supplier; - public class EntityAir { private int speed; - public int getSpeed(){ + public int getSpeed() { return speed; } private double weight; - public double getWeight(){ + public double getWeight() { return weight; } private Color bodyColor; - public Color getBodyColor(){ + public Color getBodyColor() { return bodyColor; } - public Supplier step = () ->(double) speed * 100 / weight; - + public Supplier step = () -> (double) speed * 100 / weight; public EntityAir(int speed, double weight, Color bodyColor){ this.speed = speed; this.weight = weight; diff --git a/src/EntityAirBomber.java b/src/EntityAirBomber.java index 4146ce8..8abfafd 100644 --- a/src/EntityAirBomber.java +++ b/src/EntityAirBomber.java @@ -1,5 +1,6 @@ import java.awt.*; -public class EntityAirBomber extends EntityAir{ + +public class EntityAirBomber extends EntityAir { private Color additionalColor; public Color getAdditionalColor(){ return additionalColor; @@ -13,7 +14,7 @@ public class EntityAirBomber extends EntityAir{ return isBombs; } public EntityAirBomber(int speed, double weight, Color bodyColor, Color - additionalColor, boolean isFuel, boolean isBombs) { + additionalColor, boolean isBombs, boolean isFuel) { super(speed, weight, bodyColor); this.additionalColor = additionalColor; this.isFuel = isFuel; diff --git a/src/FrameAirBomber.java b/src/FrameAirBomber.java index ee17374..2e368f9 100644 --- a/src/FrameAirBomber.java +++ b/src/FrameAirBomber.java @@ -6,8 +6,14 @@ import java.io.File; import java.io.IOException; import java.util.Random; public class FrameAirBomber extends JFrame { - private DrawingAir drawingAir; + private DrawingAir drawingPlane; private AbstractStrategy abstractStrategy; + public JButton selectPlaneButton; + + private DrawingAir selectedPlane; + public DrawingAir getSelectedPlane() { + return selectedPlane; + } private JComboBox comboBoxStrategy; private final JComponent pictureBox; public FrameAirBomber() throws IOException { @@ -19,7 +25,7 @@ public class FrameAirBomber extends JFrame { public void paintComponent(Graphics graphics){ super.paintComponent(graphics); Graphics2D graphics2D = (Graphics2D) graphics; - if (drawingAir != null) drawingAir.drawTransport(graphics2D); + if (drawingPlane != null) drawingPlane.drawTransport(graphics2D); super.repaint(); } }; @@ -31,6 +37,7 @@ public class FrameAirBomber extends JFrame { 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()); @@ -59,6 +66,9 @@ public class FrameAirBomber extends JFrame { 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)); @@ -98,21 +108,24 @@ public class FrameAirBomber extends JFrame { private void buttonCreateAirBomberClick() { Random random = new Random(); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); - drawingAir = new DrawingAirBomber(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); - drawingAir.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + 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()); - drawingAir = new DrawingAir(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED); + drawingPlane = new DrawingAir(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor, pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); - drawingAir.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); } private void buttonStepClick(){ - if (drawingAir == null) { + if (drawingPlane == null) { return; } if (comboBoxStrategy.isEnabled()) { @@ -132,7 +145,7 @@ public class FrameAirBomber extends JFrame { { return; } - abstractStrategy.SetData(new DrawingObjectAirBomber(drawingAir), pictureBox.getWidth(), + abstractStrategy.SetData(new DrawingObjectPlane(drawingPlane), pictureBox.getWidth(), pictureBox.getHeight()); comboBoxStrategy.setEnabled(false); } @@ -149,28 +162,34 @@ public class FrameAirBomber extends JFrame { } } private void buttonMoveClick(ActionEvent event) { - if(drawingAir == null || drawingAir.getEntityPlane() == null) + if(drawingPlane == null || drawingPlane.getEntityAir() == null) return; switch (event.getActionCommand()) { case "left": - drawingAir.moveTransport(DirectionType.LEFT); + drawingPlane.moveTransport(DirectionType.LEFT); break; case "right": - drawingAir.moveTransport(DirectionType.RIGHT); + drawingPlane.moveTransport(DirectionType.RIGHT); break; case "up": - drawingAir.moveTransport(DirectionType.UP); + drawingPlane.moveTransport(DirectionType.UP); break; case "down": - drawingAir.moveTransport(DirectionType.DOWN); + drawingPlane.moveTransport(DirectionType.DOWN); break; } draw(); } private void draw() { - if (drawingAir == null) + if (drawingPlane == null) return; pictureBox.repaint(); } + public void select(){ + if (drawingPlane == null) { + return; + } + selectedPlane = drawingPlane; + } } \ No newline at end of file diff --git a/src/FrameHard.java b/src/FrameHard.java new file mode 100644 index 0000000..9ff6bd6 --- /dev/null +++ b/src/FrameHard.java @@ -0,0 +1,77 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FrameHard extends JFrame { + HardGeneric generic; + DrawingAir drawing; + private JComponent pictureBox; + private final int pictureBoxWidth = 500; + 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("Создать новый объект"); + buttonMakeObject.setPreferredSize(new Dimension(150, 30)); + JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + buttonPanel.add(buttonMakeObject); + 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 -> { + DrawingAir 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(buttonPanel, BorderLayout.SOUTH); + pack(); + setVisible(true); + } + + public EntityAir makeRandomPlane() { + Random rand = new Random(); + EntityAir 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 EntityAir(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 DrawingEnginesOval(); + case 2 -> engines = new DrawingEnginesRound(); + default -> engines = new DrawingEnginesSquare(); + } + engines.setNumber((random.nextInt(3) + 1) * 2); + return engines; + } + + void draw() { + pictureBox.repaint(); + } +} diff --git a/src/FramePlaneCollection.java b/src/FramePlaneCollection.java new file mode 100644 index 0000000..0865276 --- /dev/null +++ b/src/FramePlaneCollection.java @@ -0,0 +1,102 @@ +import javax.swing.*; +import java.awt.*; +import java.io.IOException; + +public class FramePlaneCollection extends JFrame { + private BomberGenericCollection planes; + JComponent pictureBoxCollection; + TextField textFieldNumber; + public FramePlaneCollection(){ + super("Набор самолетов"); + setSize(new Dimension(900,600)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + creteGUI(); + planes = new BomberGenericCollection<>(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(); + } +} diff --git a/src/HardGeneric.java b/src/HardGeneric.java new file mode 100644 index 0000000..0b82985 --- /dev/null +++ b/src/HardGeneric.java @@ -0,0 +1,51 @@ +import java.util.Random; + +public class HardGeneric { + 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 EntityAir[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 DrawingAir makeObject() { + Random rand = new Random(); + EntityAir 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 DrawingAir(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), pictureBoxWidth, pictureBoxHeight, engine.getType(), engine.getNumber()); + } +} \ No newline at end of file diff --git a/src/IDrawEngines.java b/src/IDrawEngines.java index 9b95621..6f69c21 100644 --- a/src/IDrawEngines.java +++ b/src/IDrawEngines.java @@ -1,6 +1,8 @@ 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); } diff --git a/src/Main.java b/src/Main.java index b191374..2df0988 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ import java.io.IOException; public class Main { - public static void main(String[] args) throws IOException { new FrameAirBomber(); } + public static void main(String[] args) throws IOException { new FrameHard(); } } diff --git a/src/MoveToBorder.java b/src/MoveToBorder.java index 07b0c19..cbf5d22 100644 --- a/src/MoveToBorder.java +++ b/src/MoveToBorder.java @@ -5,7 +5,7 @@ public class MoveToBorder extends AbstractStrategy{ if(objParams == null) return false; return objParams.getRightBorder() + GetStep() >= getFieldWidth() && - objParams.getDownBorder() + GetStep() >= getFieldHeight(); + objParams.getDownBorder() + GetStep() >= getFieldHeight(); } @Override protected void MoveToTarget() { @@ -13,22 +13,10 @@ public class MoveToBorder extends AbstractStrategy{ if(objParams == null) return; var diffX = objParams.getRightBorder() - getFieldWidth(); + if (Math.abs(diffX) > GetStep()) + MoveRight(); var diffY = objParams.getDownBorder() - getFieldHeight(); - if(diffX >= 0) - { + if (Math.abs(diffY) > GetStep()) MoveDown(); - } - else if(diffY >= 0) - { - MoveRight(); - } - else if(Math.abs(diffX) > Math.abs(diffY)) - { - MoveRight(); - } - else - { - MoveDown(); - } } } diff --git a/src/MoveToCenter.java b/src/MoveToCenter.java index 18363f9..2927d00 100644 --- a/src/MoveToCenter.java +++ b/src/MoveToCenter.java @@ -4,10 +4,11 @@ public class MoveToCenter extends AbstractStrategy{ 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; + objParams.getObjectMiddleHorizontal()+GetStep() >= getFieldWidth() / 2 && + objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 && + objParams.getObjectMiddleVertical() + GetStep() >= getFieldHeight() / 2; } @Override protected void MoveToTarget() { diff --git a/src/ObjectParameters.java b/src/ObjectParameters.java index 7b04f57..2026666 100644 --- a/src/ObjectParameters.java +++ b/src/ObjectParameters.java @@ -1,19 +1,19 @@ public class ObjectParameters { - private final int _x; - private final int _y; - private final int _width; - private final int _height; - public int getLeftBorder() {return _x;} - public int getTopBorder() {return _y;} - public int getRightBorder() {return _x + _width;} - public int getDownBorder() {return _y + _height;} - public int getObjectMiddleHorizontal() {return _x + this._width / 2;} - public int getObjectMiddleVertical() {return _y + this._height / 2;} + 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) { - _x = x; - _y = y; - _width = width; - _height = height; + POS_X = x; + POS_Y = y; + WIDTH = width; + HEIGHT = height; } } diff --git a/src/SetGeneric.java b/src/SetGeneric.java new file mode 100644 index 0000000..24ec845 --- /dev/null +++ b/src/SetGeneric.java @@ -0,0 +1,39 @@ +import java.lang.reflect.Array; + +public class SetGeneric{ + private final T[] places; + public int getCount() {return places.length;} + public SetGeneric(int count, Class 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]; + } +} \ No newline at end of file