diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..002da1d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Main.java \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b3f7216..34de578 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,12 +1,25 @@ + + - + + + + + + @@ -16,12 +29,17 @@ \ No newline at end of file diff --git a/resources/down-arrow.png b/resources/down-arrow.png new file mode 100644 index 0000000..9d67143 Binary files /dev/null and b/resources/down-arrow.png differ diff --git a/resources/left-arrow.png b/resources/left-arrow.png new file mode 100644 index 0000000..046470d Binary files /dev/null and b/resources/left-arrow.png differ diff --git a/resources/right-arrow.png b/resources/right-arrow.png new file mode 100644 index 0000000..fac4c96 Binary files /dev/null and b/resources/right-arrow.png differ diff --git a/resources/upper-arrow.png b/resources/upper-arrow.png new file mode 100644 index 0000000..0bc323d Binary files /dev/null and b/resources/upper-arrow.png differ diff --git a/src/CountDecks.java b/src/CountDecks.java new file mode 100644 index 0000000..8114f64 --- /dev/null +++ b/src/CountDecks.java @@ -0,0 +1,5 @@ +public enum CountDecks { + OneDeck, + TwoDecks, + ThreeDecks +} diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/src/DrawingDecks.java b/src/DrawingDecks.java new file mode 100644 index 0000000..fbb6fe2 --- /dev/null +++ b/src/DrawingDecks.java @@ -0,0 +1,41 @@ +import java.awt.*; +public class DrawingDecks { + private CountDecks countDecks; + private int NumberDecks; + public void SetCountDecks(int value){ + NumberDecks = value; + switch (value){ + case 2: + countDecks = CountDecks.TwoDecks; + break; + case 3: + countDecks = CountDecks.ThreeDecks; + break; + default: + countDecks = CountDecks.OneDeck; + } + }; + public void DrawDeck(int x, int y, int width, int heght, Graphics g, Color BodyColor){ + g.setColor(BodyColor); + g.fillRect(x, y, width, heght); + g.setColor(Color.black); + g.drawRect(x, y, width, heght); + } + + public void DrawingDecks(int _startPosX, int _startPosY, Color BodyColor, Graphics g){ + switch (countDecks){ + case OneDeck: + DrawDeck(_startPosX + 25, _startPosY + 80, 130, 30, g, BodyColor); + break; + case TwoDecks: + DrawDeck(_startPosX + 25, _startPosY + 80, 130, 30, g, BodyColor); + DrawDeck(_startPosX + 40, _startPosY + 55, 100, 25, g, BodyColor); + break; + case ThreeDecks: + DrawDeck(_startPosX + 25, _startPosY + 80, 130, 30, g, BodyColor); + DrawDeck(_startPosX + 40, _startPosY + 55, 100, 25, g, BodyColor); + DrawDeck(_startPosX + 45, _startPosY + 30, 75, 25, g, BodyColor); + break; + } + } +} \ No newline at end of file diff --git a/src/DrawingShip.java b/src/DrawingShip.java new file mode 100644 index 0000000..cef2fd8 --- /dev/null +++ b/src/DrawingShip.java @@ -0,0 +1,120 @@ + +import java.awt.*; + +public class DrawingShip { + private EntityShip entityShip; + public EntityShip GetEntityShip(){ + return entityShip; + } + private void SetEntityShip(EntityShip entityShip){ + this.entityShip = entityShip; + } + private DrawingDecks drawingDecks; + public DrawingDecks GetDrawingDecks(){ + return drawingDecks; + } + private void SetDrawingDecks( DrawingDecks drawingDecks){ + this.drawingDecks = drawingDecks; + } + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private final int _shipWidth = 185; + private final int _shipHeight = 180; + public boolean Init(int speed, double weight, Color bodyColor, Color addColor, boolean pipes, boolean section, int width, int height, int countDecks){ + if (width < _shipWidth || height <_shipHeight){ + return false; + } + _pictureWidth = width; + _pictureHeight = height; + entityShip = new EntityShip(); + entityShip.Init(speed, weight, bodyColor, addColor, pipes, section); + drawingDecks = new DrawingDecks(); + drawingDecks.SetCountDecks(countDecks); + return true; + } + public void SetPosition(int x, int y){ + if (x < 0 || x + _shipWidth > _pictureWidth){ + x = 20; + } + if (y < 0 || y + _shipHeight > _pictureHeight){ + y = 20; + } + _startPosX = x; + _startPosY = y; + } + public void MoveTransport(Direction direction){ + if (entityShip == null){ + return; + } + switch (direction){ + case Left: + System.out.println(_startPosX + " " + entityShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityShip.GetStep() + _shipWidth) + " " + _pictureWidth); + if (_startPosX - entityShip.GetStep() > 0){ + _startPosX -= (int)entityShip.GetStep(); + } + break; + case Up: + System.out.println(_startPosY + " " + entityShip.GetStep() + " " + (_startPosY + entityShip.GetStep()) + " " + _pictureHeight); + if (_startPosY - entityShip.GetStep() > 0){ + _startPosY -= (int)entityShip.GetStep(); + } + break; + case Right: + System.out.println(_startPosX + " " + entityShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityShip.GetStep() + _shipWidth) + " " + _pictureWidth); + if (_startPosX + entityShip.GetStep() + _shipWidth < _pictureWidth){ + _startPosX += (int)entityShip.GetStep(); + } + break; + case Down: + System.out.println(_startPosY + " " + entityShip.GetStep() + " " + (_startPosY + entityShip.GetStep()) + " " + _pictureHeight); + if (_startPosY + entityShip.GetStep() + _shipHeight < _pictureHeight){ + _startPosY += (int)entityShip.GetStep(); + } + break; + } + } + + public void DrawTransport(Graphics g) { + if (entityShip == null || drawingDecks == null) { + System.out.println("Error"); + return; + } + //корпус + int[] XPoints = {_startPosX, _startPosX + 180, _startPosX + 140, _startPosX + 40, _startPosX}; + int[] YPoints = {_startPosY + 110, _startPosY + 110, _startPosY + 185, _startPosY + 185, _startPosY + 110}; + + int nPoints = 5; + g.setColor(entityShip.GetBodyColor()); + g.fillPolygon(XPoints, YPoints, nPoints); + g.setColor(Color.black); + g.drawPolygon(XPoints, YPoints, nPoints); + + //якорь + g.drawLine(_startPosX + 50, _startPosY + 130, _startPosX + 50, _startPosY + 150); + g.drawLine(_startPosX + 40, _startPosY + 140, _startPosX + 60, _startPosY + 140); + g.drawLine(_startPosX + 45, _startPosY + 150, _startPosX + 55, _startPosY + 150); + + //трубы + if (entityShip.GetPipes()) { + g.setColor(entityShip.GetAddColor()); + g.fillRect(_startPosX + 55, _startPosY, 25, 80); + g.fillRect(_startPosX + 90, _startPosY + 20, 25, 60); + g.setColor(Color.black); + g.drawRect(_startPosX + 55, _startPosY, 25, 80); + g.drawRect(_startPosX + 90, _startPosY + 20, 25, 60); + } + + //топливный отсек + if (entityShip.GetSection()) { + g.setColor(Color.gray); + g.fillOval(_startPosX + 130, _startPosY + 130, 20, 20); + g.setColor(Color.black); + g.drawOval(_startPosX + 130, _startPosY + 130, 20, 20); + } + + //палуба + drawingDecks.DrawingDecks(_startPosX, _startPosY, entityShip.GetBodyColor(), g); + } +} \ No newline at end of file diff --git a/src/EntityShip.java b/src/EntityShip.java new file mode 100644 index 0000000..7d537cd --- /dev/null +++ b/src/EntityShip.java @@ -0,0 +1,57 @@ +import java.awt.*; +public class EntityShip { + private int Speed; + public int GetSpeed(){ + return Speed; + } + private void SetSpeed(int speed){ + Speed = speed; + } + private double Weight; + public double GetWeight(){ + return Weight; + } + private void SetWeight(int weight){ + Weight = weight; + } + private Color BodyColor; + public Color GetBodyColor(){ + return BodyColor; + } + private void SetBodyColor(Color bodyColor){ + BodyColor = bodyColor; + } + private Color AdditionlaColor; + public Color GetAddColor(){ + return AdditionlaColor; + } + private void SetAddColor(Color addColor){ + AdditionlaColor = addColor; + } + private boolean Pipes; + public boolean GetPipes(){ + return Pipes; + } + private void SetPipes(boolean pipes){ + Pipes = pipes; + } + private boolean Section; + public boolean GetSection(){ + return Section; + } + private void SetSection(boolean section){ + Section = section; + } + private double Step; + public double GetStep() { + return (double)Speed * 100 / Weight; + } + public void Init(int speed, double weight, Color bodyColor, Color addColor, boolean pipes, boolean section){ + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionlaColor = addColor; + Pipes = pipes; + Section = section; + } +} diff --git a/src/FormWarmlyShip.java b/src/FormWarmlyShip.java new file mode 100644 index 0000000..f2277cf --- /dev/null +++ b/src/FormWarmlyShip.java @@ -0,0 +1,116 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.Graphics; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.*; + +public class FormWarmlyShip{ + private DrawingShip _drawingShip; + Canvas canv; + public void Draw(){ + canv.repaint(); + } + public FormWarmlyShip(){ + JFrame frame = new JFrame("Warmly Ship"); + JButton buttonCreate = new JButton("Создать"); + buttonCreate.setFocusPainted(false); + buttonCreate.setContentAreaFilled(false); + JButton buttonUp = new JButton(); + buttonUp.setFocusPainted(false); //контур вокруг текста + buttonUp.setContentAreaFilled(false); //раскраска конпки + buttonUp.setName("up"); //имя кнопки при обработке нажания + buttonUp.setIcon(new ImageIcon(((new ImageIcon("resources/upper-arrow.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + JButton buttonDown = new JButton(); + buttonDown.setFocusPainted(false); + buttonDown.setContentAreaFilled(false); + buttonDown.setName("down"); + buttonDown.setIcon(new ImageIcon(((new ImageIcon("resources/down-arrow.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + JButton buttonLeft = new JButton(); + buttonLeft.setFocusPainted(false); + buttonLeft.setContentAreaFilled(false); + buttonLeft.setName("left"); + buttonLeft.setIcon(new ImageIcon(((new ImageIcon("resources/left-arrow.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + JButton buttonRight = new JButton(); + buttonRight.setFocusPainted(false); + buttonRight.setContentAreaFilled(false); + buttonRight.setName("right"); + buttonRight.setIcon(new ImageIcon(((new ImageIcon("resources/right-arrow.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + buttonCreate.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.out.println(e.getActionCommand()); + Random random = new Random(); + _drawingShip = new DrawingShip(); + _drawingShip.Init( + 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(), 900, 460, random.nextInt(3) + 1); + _drawingShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + canv._drawingShip = _drawingShip; + Draw(); + } + } + ); + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.out.println(((JButton)(e.getSource())).getName()); + if (_drawingShip == null){ + return; + } + switch ((((JButton)(e.getSource())).getName())){ + case "up": + _drawingShip.MoveTransport(Direction.Up); + break; + case "down": + _drawingShip.MoveTransport(Direction.Down); + break; + case "left": + _drawingShip.MoveTransport(Direction.Left); + break; + case "right": + _drawingShip.MoveTransport(Direction.Right); + break; + } + Draw(); + } + }; + buttonUp.addActionListener(actionListener); + buttonDown.addActionListener(actionListener); + buttonLeft.addActionListener(actionListener); + buttonRight.addActionListener(actionListener); + frame.setSize(920, 500); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLayout(null); + canv = new Canvas(); + canv.setBounds(0, 0, 900, 500); + buttonCreate.setBounds(10, 400, 100, 40); + buttonUp.setBounds(800, 360, 40, 40); + buttonDown.setBounds(800, 400, 40, 40); + buttonLeft.setBounds(760, 400, 40, 40); + buttonRight.setBounds(840, 400, 40, 40); + frame.add(canv); + frame.add(buttonCreate); + frame.add(buttonUp); + frame.add(buttonDown); + frame.add(buttonLeft); + frame.add(buttonRight); + frame.setVisible(true); + } + class Canvas extends JComponent{ + public DrawingShip _drawingShip; + public Canvas(){} + + public void paintComponent(Graphics g){ + if (_drawingShip == null){ + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D)g; + _drawingShip.DrawTransport(g2d); + super.repaint(); + } + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 3e59c38..ea4aaa3 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + new FormWarmlyShip(); } } \ No newline at end of file