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 0000000..63e318c Binary files /dev/null and b/Resources/ArrowDown.png differ diff --git a/Resources/ArrowLeft.png b/Resources/ArrowLeft.png new file mode 100644 index 0000000..0086b93 Binary files /dev/null and b/Resources/ArrowLeft.png differ diff --git a/Resources/ArrowRight.png b/Resources/ArrowRight.png new file mode 100644 index 0000000..a04c610 Binary files /dev/null and b/Resources/ArrowRight.png differ diff --git a/Resources/ArrowUp.png b/Resources/ArrowUp.png new file mode 100644 index 0000000..cd089f5 Binary files /dev/null and b/Resources/ArrowUp.png differ