From febf171390bfba3d3dd41d23dbacb82990a5ea79 Mon Sep 17 00:00:00 2001 From: the Date: Tue, 22 Nov 2022 08:45:54 +0400 Subject: [PATCH] LabHard01 --- DecksCount.java | 5 ++ Direction.java | 6 ++ DrawingDecks.java | 36 ++++++++++ DrawingShip.java | 111 ++++++++++++++++++++++++++++ EntityShip.java | 31 ++++++++ FormShip.form | 152 +++++++++++++++++++++++++++++++++++++++ FormShip.java | 75 +++++++++++++++++++ Program.java | 10 +++ Resources/ArrowDown.png | Bin 0 -> 264 bytes Resources/ArrowLeft.png | Bin 0 -> 292 bytes Resources/ArrowRight.png | Bin 0 -> 287 bytes Resources/ArrowUp.png | Bin 0 -> 262 bytes 12 files changed, 426 insertions(+) create mode 100644 DecksCount.java create mode 100644 Direction.java create mode 100644 DrawingDecks.java create mode 100644 DrawingShip.java create mode 100644 EntityShip.java create mode 100644 FormShip.form create mode 100644 FormShip.java create mode 100644 Program.java create mode 100644 Resources/ArrowDown.png create mode 100644 Resources/ArrowLeft.png create mode 100644 Resources/ArrowRight.png create mode 100644 Resources/ArrowUp.png diff --git a/DecksCount.java b/DecksCount.java new file mode 100644 index 0000000..ce88f8e --- /dev/null +++ b/DecksCount.java @@ -0,0 +1,5 @@ +public enum DecksCount { + One, + Two, + Three +} \ No newline at end of file diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/DrawingDecks.java b/DrawingDecks.java new file mode 100644 index 0000000..96d1d02 --- /dev/null +++ b/DrawingDecks.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class DrawingDecks { + private DecksCount deckCount; + private Color color; + + public void Init(int deckCount, Color bodyColor) { + setdecksCount(deckCount); + color = bodyColor; + } + + public void setdecksCount(int num) { + if (num <= 1) { + deckCount = DecksCount.One; + } else if (num >= 3) { + deckCount = DecksCount.Three; + } + else { + deckCount = DecksCount.Two; + } + } + + public void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight) { + g.setColor(color != null ? color : Color.BLACK); + switch (deckCount) { + case Two: { + g.fillRect(x, y + 5, 15, 5); + g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3); + } + case Three: { + g.fillRect(x + shipWidth - 20, y, 20, 10); + g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth - 25}, new int[] {y, y + 10, y + 10}, 3); + } + } + } +} \ No newline at end of file diff --git a/DrawingShip.java b/DrawingShip.java new file mode 100644 index 0000000..5fc3804 --- /dev/null +++ b/DrawingShip.java @@ -0,0 +1,111 @@ +import java.awt.*; + +public class DrawingShip { + private EntityShip ship; + private DrawingDecks drawingDecks; + private float _startPosX; + private float _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _shipWidth = 80; + private final int _shipHeight = 30; + + public EntityShip getShip() { + return ship; + } + + public void Init(int speed, float weight, Color bodyColor, int decksCount) { + ship = new EntityShip(); + ship.Init(speed, weight, bodyColor); + drawingDecks = new DrawingDecks(); + drawingDecks.Init(decksCount, bodyColor); + } + + public void SetPosition(int x, int y, int width, int height) { + if (x < 0 || x + _shipWidth >= width) + { + return; + } + + if (y < 0 || y + _shipHeight >= height) + { + return; + } + + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + + public void moveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + switch (direction) + { + case Right: + if (_startPosX + _shipWidth + ship.getStep() < _pictureWidth) + { + _startPosX += ship.getStep(); + } + break; + + case Left: + if (_startPosX - ship.getStep() >= 0) + { + _startPosX -= ship.getStep(); + } + break; + + case Up: + if (_startPosY - ship.getStep() >= 0) + { + _startPosY -= ship.getStep(); + } + break; + + case Down: + if (_startPosY + _shipHeight + ship.getStep() < _pictureHeight) + { + _startPosY += ship.getStep(); + } + break; + } + } + + public void drawTransport(Graphics2D g) { + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) + { + return; + } + g.setColor(ship.getBodyColor() != null ? ship.getBodyColor() : Color.BLACK); + int xPoly[] = {(int)_startPosX, (int)_startPosX + 80, (int)_startPosX + 70, (int)_startPosX + 10}; + int yPoly[] = {(int)_startPosY + 10, (int)_startPosY + 10, (int)_startPosY + 30, (int)_startPosY + 30}; + g.fillPolygon(xPoly, yPoly, 4); + g.fillRect((int)_startPosX + 30, (int)_startPosY, 20, 10); + drawingDecks.draw(g, (int) _startPosX, (int) _startPosY, _shipWidth, _shipHeight); + } + + public void changeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _shipWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _shipWidth; + } + if (_startPosY + _shipHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _shipHeight; + } + } +} \ No newline at end of file diff --git a/EntityShip.java b/EntityShip.java new file mode 100644 index 0000000..51fecd8 --- /dev/null +++ b/EntityShip.java @@ -0,0 +1,31 @@ +import java.awt.*; +import java.util.Random; + +public class EntityShip { + private int speed; + private float weight; + private Color bodyColor; + + public void Init(int speed, float weight, Color bodyColor) { + Random rnd = new Random(); + this.speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed; + this.weight = weight <= 0 ? rnd.nextInt(30) + 40 : weight; + this.bodyColor = bodyColor; + } + + public int getSpeed() { + return speed; + } + + public float getWeight() { + return weight; + } + + public Color getBodyColor() { + return bodyColor; + } + + public float getStep() { + return speed * 100 / weight; + } +} diff --git a/FormShip.form b/FormShip.form new file mode 100644 index 0000000..b20580f --- /dev/null +++ b/FormShip.form @@ -0,0 +1,152 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormShip.java b/FormShip.java new file mode 100644 index 0000000..8a06582 --- /dev/null +++ b/FormShip.java @@ -0,0 +1,75 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormShip extends JFrame { + private JPanel shipPane; + private JLabel speedLabel; + private JLabel weightLabel; + private JLabel colorLabel; + private JPanel pictureBox; + private JButton createButton; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + + private DrawingShip _ship; + + public FormShip() { + this.setTitle("Ship"); + this.setContentPane(shipPane); + createButton.addActionListener(e -> { + Random rnd = new Random(); + _ship = new DrawingShip(); + _ship.Init( + rnd.nextInt(200) + 100, + rnd.nextInt(1000) + 1000, + new Color( + rnd.nextInt(256), + rnd.nextInt(256), + rnd.nextInt(256)), + rnd.nextInt(3) + 1 + ); + _ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText(String.format("Speed: %s", _ship.getShip().getSpeed())); + weightLabel.setText(String.format("Weight: %s", _ship.getShip().getWeight())); + colorLabel.setText(String.format("Color: %x", _ship.getShip().getBodyColor().getRGB())); + repaint(); + }); + pictureBox.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + if (_ship != null) _ship.changeBorders(pictureBox.getWidth(), pictureBox.getHeight()); + repaint(); + } + }); + buttonLeft.addActionListener(e -> { + if (_ship != null) _ship.moveTransport(Direction.Left); + repaint(); + }); + buttonRight.addActionListener(e -> { + if (_ship != null) _ship.moveTransport(Direction.Right); + repaint(); + }); + buttonUp.addActionListener(e -> { + if (_ship != null) _ship.moveTransport(Direction.Up); + repaint(); + }); + buttonDown.addActionListener(e -> { + if (_ship != null) _ship.moveTransport(Direction.Down); + repaint(); + }); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2d = (Graphics2D) shipPane.getGraphics(); + if (_ship != null) { + _ship.drawTransport(g2d); + } + } +} diff --git a/Program.java b/Program.java new file mode 100644 index 0000000..5f19d46 --- /dev/null +++ b/Program.java @@ -0,0 +1,10 @@ +import javax.swing.*; + +public class Program { + public static void main(String[] args) { + FormShip form = new FormShip(); + form.setSize(640, 480); + form.setVisible(true); + form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} \ No newline at end of file diff --git a/Resources/ArrowDown.png b/Resources/ArrowDown.png new file mode 100644 index 0000000000000000000000000000000000000000..63e318c44a27017dfe86f61a0aec8f87e5abc4cb GIT binary patch literal 264 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jljKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!MUC;jv*f2Z>I)wH7E$UZ2f)zrkcp6gEwPY z?V0Ykv#vd#y5`K%DOV;I=aqk+@G9#%Z%&AHFVdQ I&MBb@0IG;-6#xJL literal 0 HcmV?d00001 diff --git a/Resources/ArrowLeft.png b/Resources/ArrowLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..0086b937ae2d8472410a81baf3fcfc10d65168ea GIT binary patch literal 292 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jljKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!JVEijv*f2YtK0H9Z}$MzIgBdC0Uc7Q)f*& z!u6pn;rR8nF~Ur)UnD-Cn6m2lrIrZK>N74G8ioD6EUKz63^(-eW#8{^@bma}%{9+< zmqxAB*Ojfu6zPlKgkcDDN2@fssc~_0%vs8-L>EX16_~HNmneTkGQ#m3H zBHuTEJ2+42h-7y5TA7gS+I9PK^Z(eq-=8jTCc0ekda#L7)ik$x`);oP{AT5;43n2$ zQoU6z``&-ww&V53%QN=P fEUYT_SQ>LbJW#-F@2`zO_cM69`njxgN@xNA0zq`R literal 0 HcmV?d00001 diff --git a/Resources/ArrowUp.png b/Resources/ArrowUp.png new file mode 100644 index 0000000000000000000000000000000000000000..cd089f563b0d7942e8bd2bab7c7fbe73780a8e5e GIT binary patch literal 262 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jljKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!P%ZJjv*f2Z>Ku)H7M|~9Q^-Z($L#vT~2h5 zoXHthq1z{yd7M}}C1p}^-frW($CttyKZW)99|+ogwyZH^cU;1huU8%Q1CGjW)mr;} zS>L+bhq4ZPOFz0*5wtr_V2k{#1>!Dmd~KhwuB#2c|AFcDz3E{w@B6%t)t_iSapl0d zb)TBVgX@FO`ZM