diff --git a/.idea/PIBD-11_Shipilov_N.S._Seaplane_Hard.iml b/.idea/PIBD-11_Shipilov_N.S._Seaplane_Hard.iml index d6ebd48..c4f709e 100644 --- a/.idea/PIBD-11_Shipilov_N.S._Seaplane_Hard.iml +++ b/.idea/PIBD-11_Shipilov_N.S._Seaplane_Hard.iml @@ -2,7 +2,9 @@ - + + + diff --git a/Seaplane/.idea/uiDesigner.xml b/Seaplane/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/Seaplane/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Seaplane/Resources/30px_arrow_down.png b/Seaplane/Resources/30px_arrow_down.png new file mode 100644 index 0000000..7aa3255 Binary files /dev/null and b/Seaplane/Resources/30px_arrow_down.png differ diff --git a/Seaplane/Resources/30px_arrow_left.png b/Seaplane/Resources/30px_arrow_left.png new file mode 100644 index 0000000..9a3dfa2 Binary files /dev/null and b/Seaplane/Resources/30px_arrow_left.png differ diff --git a/Seaplane/Resources/30px_arrow_right.png b/Seaplane/Resources/30px_arrow_right.png new file mode 100644 index 0000000..0f06fd0 Binary files /dev/null and b/Seaplane/Resources/30px_arrow_right.png differ diff --git a/Seaplane/Resources/30px_arrow_up.png b/Seaplane/Resources/30px_arrow_up.png new file mode 100644 index 0000000..67ae4bc Binary files /dev/null and b/Seaplane/Resources/30px_arrow_up.png differ diff --git a/Seaplane/Resources/arrowDown.jpg b/Seaplane/Resources/arrowDown.jpg new file mode 100644 index 0000000..f21002e Binary files /dev/null and b/Seaplane/Resources/arrowDown.jpg differ diff --git a/Seaplane/Resources/arrowLeft.jpg b/Seaplane/Resources/arrowLeft.jpg new file mode 100644 index 0000000..61b8dae Binary files /dev/null and b/Seaplane/Resources/arrowLeft.jpg differ diff --git a/Seaplane/Resources/arrowRight.jpg b/Seaplane/Resources/arrowRight.jpg new file mode 100644 index 0000000..b440197 Binary files /dev/null and b/Seaplane/Resources/arrowRight.jpg differ diff --git a/Seaplane/Resources/arrowUp.jpg b/Seaplane/Resources/arrowUp.jpg new file mode 100644 index 0000000..e630cea Binary files /dev/null and b/Seaplane/Resources/arrowUp.jpg differ diff --git a/Seaplane/Seaplane.iml b/Seaplane/Seaplane.iml index c90834f..d5b343a 100644 --- a/Seaplane/Seaplane.iml +++ b/Seaplane/Seaplane.iml @@ -3,6 +3,7 @@ + diff --git a/Seaplane/src/DirectionType.java b/Seaplane/src/DirectionType.java new file mode 100644 index 0000000..d3a2058 --- /dev/null +++ b/Seaplane/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} \ No newline at end of file diff --git a/Seaplane/src/DrawingSeaplane.java b/Seaplane/src/DrawingSeaplane.java new file mode 100644 index 0000000..6d59d11 --- /dev/null +++ b/Seaplane/src/DrawingSeaplane.java @@ -0,0 +1,187 @@ +import java.awt.*; +import java.util.Random; + +public class DrawingSeaplane { + private EntitySeaplane entitySeaplane; + private Integer pictureWidth; + private Integer pictureHeight; + private Integer _startPosX; + private Integer _startPosY; + private final int drawingSeaplaneWidth = 190; + private final int drawingSeaplaneHeight = 90; + private final int drawingKeelHeight = 35; + private DrawingSeaplanePortholes _drawingSeaplanePortholes; + public EntitySeaplane EntitySeaplane() { + return entitySeaplane; + } + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean floats, boolean inflatableBoat) { + entitySeaplane = new EntitySeaplane(); + entitySeaplane.Init(speed, weight, bodyColor, additionalColor, floats, inflatableBoat); + pictureHeight = null; + pictureWidth = null; + _startPosX = null; + _startPosY = null; + _drawingSeaplanePortholes = new DrawingSeaplanePortholes(entitySeaplane); + Random random = new Random(); + int paddlesCount = random.nextInt(1,4); + _drawingSeaplanePortholes.setEnumNumber(paddlesCount * 10); + } + + public void setPictureSize(int width, int height) { + pictureWidth = width; + pictureHeight = height; + } + + public void setPosition(int x, int y) { + if (pictureHeight == null || pictureWidth == null) { + return; + } + + if (x < 0) { + x = 0; + } else if (x - drawingSeaplaneWidth >= pictureWidth) { + x = pictureWidth - drawingSeaplaneWidth - drawingSeaplaneWidth; + } + + if (y - drawingKeelHeight < 0) { + y = drawingKeelHeight; + } else if (y - drawingSeaplaneHeight >= pictureHeight) { + y = pictureHeight - drawingSeaplaneHeight - drawingSeaplaneHeight; + } + + _startPosY = y; + _startPosX = x; + } + + public boolean moveTransport(DirectionType direction) { + if (entitySeaplane == null || _startPosX == null || _startPosY == null || pictureWidth == null || pictureHeight == null) { + return false; + } + + return switch (direction) { + case Left -> { + if (_startPosX - entitySeaplane.getStep() >= 0) { + _startPosX -= (int) entitySeaplane.getStep(); + } + yield true; + } + case Up -> { + if (_startPosY - drawingKeelHeight - entitySeaplane.getStep() >= 0) { + _startPosY -= (int) entitySeaplane.getStep(); + } + yield true; + } + case Right -> { + if (_startPosX + entitySeaplane.getStep() <= pictureWidth - drawingSeaplaneWidth) { + _startPosX += (int) entitySeaplane.getStep(); + } + yield true; + } + case Down -> { + if (_startPosY + entitySeaplane.getStep() <= pictureHeight - drawingSeaplaneHeight + drawingKeelHeight) { + _startPosY += (int) entitySeaplane.getStep(); + } + yield true; + } + default -> false; + }; + } + + public void drawTransport(Graphics g) { + if (entitySeaplane == null || _startPosX == null || _startPosY == null) { + return; + } + + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + // Отрисовка корпуса + g2d.drawOval(_startPosX, _startPosY + 4, 20, 20); + g2d.drawOval(_startPosX, _startPosY + 14, 20, 20); + g2d.drawRect(_startPosX, _startPosY + 15, 10, 10); + g2d.drawRect(_startPosX + 170, _startPosY + 15, 10, 10); + g2d.drawRect(_startPosX + 10, _startPosY + 4, 160, 30); + + // Отрисовка верхней части + int[] xPointsTop = {_startPosX + 170, _startPosX + 170, _startPosX + 190}; + int[] yPointsTop = {_startPosY, _startPosY + 20, _startPosY + 20}; + g2d.drawPolygon(xPointsTop, yPointsTop, 3); + + // Отрисовка нижней части + int[] xPointsBottom = {_startPosX + 170, _startPosX + 170, _startPosX + 190}; + int[] yPointsBottom = {_startPosY + 40, _startPosY + 20, _startPosY + 20}; + g2d.drawPolygon(xPointsBottom, yPointsBottom, 3); + + // Отрисовка киля + int[] xPointsKeel = {_startPosX + 10, _startPosX + 10, _startPosX + 50}; + int[] yPointsKeel = {_startPosY - 30, _startPosY + 5, _startPosY + 5}; + g2d.drawPolygon(xPointsKeel, yPointsKeel, 3); + + if (!entitySeaplane.getFloats()) + { + // Рисуем переднее шасси с одним колесом + g2d.drawOval( _startPosX + 140, _startPosY + 45, 10, 10); + + // Рисуем задние шасси с двумя колесами + g2d.drawOval( _startPosX + 20, _startPosY + 45, 10, 10); + g2d.drawOval( _startPosX + 30, _startPosY + 45, 10, 10); + + // Рисуем ногу переднего шасси + g2d.drawLine( _startPosX + 145, _startPosY + 35, _startPosX + 145, _startPosY + 50); + + // Рисуем ноги заднего шасси + g2d.drawLine( _startPosX + 30, _startPosY + 35, _startPosX + 30, _startPosY + 50); + } + + g2d.setColor(entitySeaplane.getBodyColor()); + // Закрашиваем корпус + g2d.fillOval(_startPosX, _startPosY + 4, 20, 20); + g2d.fillOval(_startPosX, _startPosY + 14, 20, 20); + g2d.fillOval(_startPosX, _startPosY + 15, 10, 10); + g2d.fillRect(_startPosX + 10, _startPosY + 4, 160, 30); + + g2d.setColor(entitySeaplane.getAdditionalColor()); + // Закрашиваем дополнительные элементы + g2d.fillPolygon(xPointsTop, yPointsTop, 3); + g2d.fillPolygon(xPointsBottom, yPointsBottom, 3); + g2d.fillPolygon(xPointsKeel, yPointsKeel, 3); + + _drawingSeaplanePortholes.drawSeaplanePortholes(g, Color.CYAN, _startPosX, _startPosY); + + g2d.setColor(Color.BLACK); + // Рисуем триммера + g2d.drawOval( _startPosX, _startPosY, 10, 10); + g2d.drawOval( _startPosX+30, _startPosY, 10, 10); + g2d.drawRect( _startPosX+5, _startPosY, 30, 10); + + // Рисуем крыло + g2d.drawOval( _startPosX + 70, _startPosY + 15, 6, 6); + g2d.drawOval( _startPosX + 130, _startPosY + 15, 6, 6); + g2d.drawRect( _startPosX + 75, _startPosY + 15, 60, 6); + + // Закрашиваем триммера + g2d.fillOval( _startPosX, _startPosY, 10, 10); + g2d.fillOval( _startPosX+30, _startPosY, 10, 10); + g2d.fillRect( _startPosX+5, _startPosY, 30, 10); + + // Закрашиваем крыло + g2d.fillOval( _startPosX + 70, _startPosY + 15, 6, 6); + g2d.fillOval( _startPosX + 130, _startPosY + 15, 6, 6); + g2d.fillRect( _startPosX + 75, _startPosY + 15, 60, 6); + + // Поплавки + if (entitySeaplane.getFloats()) { + g2d.drawLine(_startPosX + 125, _startPosY + 35, _startPosX + 125, _startPosY + 50); + g2d.drawLine(_startPosX + 50, _startPosY + 35, _startPosX + 50, _startPosY + 50); + g2d.fillOval(_startPosX + 40, _startPosY + 45, 10, 10); + g2d.fillOval(_startPosX + 125, _startPosY + 45, 10, 10); + g2d.fillRect(_startPosX + 45, _startPosY + 45, 85, 10); + } + g2d.setColor(Color.RED); + // Надувная лодка + if (entitySeaplane.getInflatableBoat()) { + g2d.fillOval(_startPosX, _startPosY + 30, 8, 8); + g2d.fillOval(_startPosX + 165, _startPosY + 30, 8, 8); + g2d.fillRect(_startPosX + 4, _startPosY + 30, 165, 8); + } + } +} diff --git a/Seaplane/src/DrawingSeaplanePortholes.java b/Seaplane/src/DrawingSeaplanePortholes.java new file mode 100644 index 0000000..60ded54 --- /dev/null +++ b/Seaplane/src/DrawingSeaplanePortholes.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class DrawingSeaplanePortholes { + private PortholesCount _portholesCount; + private final EntitySeaplane _entitySeaplane; + public DrawingSeaplanePortholes(EntitySeaplane entitySeaplane) { + _entitySeaplane = entitySeaplane; + } + + public void setEnumNumber(int portholesCount) { + for (PortholesCount value : PortholesCount.values()) { + if (value.enumNumber == portholesCount) { + _portholesCount = value; + return; + } + } + } + + public void drawSeaplanePortholes(Graphics g, Color color, float startPosX, float startPosY) { + Graphics2D g2d = (Graphics2D) g; + g2d.setStroke(new BasicStroke(1)); + int distanceBetweenPortholes = (_portholesCount.enumNumber == 20) ? 7 : 20 - _portholesCount.enumNumber / 2; + + for (int i = 0; i < _portholesCount.enumNumber; i++) { + g2d.setColor(color); + int posX = (int)(startPosX + i * distanceBetweenPortholes); + drawPortholeBundle(g2d, posX, (int)startPosY + 5); + } + } + + private void drawPortholeBundle(Graphics2D g2d, int posX, int posY) { + g2d.fillOval(posX + 10, posY + 6, 5, 5); // Рисуем иллюминатор + g2d.setColor(Color.BLACK); + g2d.drawOval(posX + 10, posY + 6, 5, 5); // Рисуем границу иллюминатора + } +} diff --git a/Seaplane/src/EntitySeaplane.java b/Seaplane/src/EntitySeaplane.java new file mode 100644 index 0000000..181565b --- /dev/null +++ b/Seaplane/src/EntitySeaplane.java @@ -0,0 +1,46 @@ +import java.awt.*; +public class EntitySeaplane { + private int Speed; + public int getSpeed() { + return Speed; + } + + private double Weight; + public double getWeight() { + return Weight; + } + + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + + private Color AdditionalColor; + public Color getAdditionalColor() { + return AdditionalColor; + } + + private double Step; + public double getStep() { + return (double)Speed * 100 / Weight; + } + + private boolean Floats; + public boolean getFloats() { + return Floats; + } + + private boolean InflatableBoat; + public boolean getInflatableBoat() { + return InflatableBoat; + } + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean floats, boolean inflatableBoat) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Floats = floats; + InflatableBoat = inflatableBoat; + } +} diff --git a/Seaplane/src/FormSeaplane.form b/Seaplane/src/FormSeaplane.form new file mode 100644 index 0000000..9fe8e41 --- /dev/null +++ b/Seaplane/src/FormSeaplane.form @@ -0,0 +1,93 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Seaplane/src/FormSeaplane.java b/Seaplane/src/FormSeaplane.java new file mode 100644 index 0000000..32208fc --- /dev/null +++ b/Seaplane/src/FormSeaplane.java @@ -0,0 +1,107 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; + +public class FormSeaplane { + protected DrawingSeaplane _drawningSeaplane = new DrawingSeaplane(); + JPanel PanelWrapper; + private JPanel PictureBox; + private JButton buttonCreate; + private JButton buttonRight; + private JButton buttonLeft; + private JButton buttonDown; + private JButton buttonUp; + + private List controls; + public FormSeaplane() { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + InitializeControlsRepaintList(); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawningSeaplane = new DrawingSeaplane(); + Random random = new Random(); + _drawningSeaplane.Init(random.nextInt(30, 100), + random.nextInt(100, 500), + 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() ); + _drawningSeaplane.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); + _drawningSeaplane.setPosition(random.nextInt(35, 100), + random.nextInt(35, 100)); + Draw(); + + } + }); + ActionListener buttonMoveClickedListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String buttonName = ((JButton) e.getSource()).getName(); + boolean result = false; + + switch (buttonName) { + case "buttonUp": { + result = _drawningSeaplane.moveTransport(DirectionType.Up); + } + break; + case "buttonDown": { + result = _drawningSeaplane.moveTransport(DirectionType.Down); + } + break; + case "buttonLeft": { + result = _drawningSeaplane.moveTransport(DirectionType.Left); + } + break; + case "buttonRight": { + result = _drawningSeaplane.moveTransport(DirectionType.Right); + } + break; + + }; + + if (result) { + Draw(); + }; + }; + }; + buttonRight.addActionListener(buttonMoveClickedListener); + buttonDown.addActionListener(buttonMoveClickedListener); + buttonLeft.addActionListener(buttonMoveClickedListener); + buttonUp.addActionListener(buttonMoveClickedListener); + + } + private void Draw() { + if (_drawningSeaplane.EntitySeaplane() == null) { + return; + } + Graphics g = PictureBox.getGraphics(); + g.setColor(PictureBox.getBackground()); + g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight()); + _drawningSeaplane.drawTransport(g); + + RepaintControls(); + } + private void RepaintControls() { + for (JComponent control : controls) { + control.repaint(); + } + } + + private void InitializeControlsRepaintList() { + controls = new LinkedList<>(); + controls.add(buttonCreate); + controls.add(buttonUp); + controls.add(buttonDown); + controls.add(buttonLeft); + controls.add(buttonRight); + } +} diff --git a/Seaplane/src/Main.java b/Seaplane/src/Main.java index f5858db..d6f9504 100644 --- a/Seaplane/src/Main.java +++ b/Seaplane/src/Main.java @@ -1,5 +1,14 @@ +import javax.swing.*; + public class Main { public static void main(String[] args) { - + JFrame.setDefaultLookAndFeelDecorated(false); + JFrame frame = new JFrame("FormSeaplane"); + frame.setContentPane(new FormSeaplane().PanelWrapper); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(1000, 500); + frame.setVisible(true); } } \ No newline at end of file diff --git a/Seaplane/src/PortholesCount.java b/Seaplane/src/PortholesCount.java new file mode 100644 index 0000000..36bb076 --- /dev/null +++ b/Seaplane/src/PortholesCount.java @@ -0,0 +1,8 @@ +public enum PortholesCount { + Ten(10), + Twenty(20), + Thirty(30); + + public final int enumNumber; + PortholesCount(int i) {this.enumNumber = i;} +}