diff --git a/ProjectElectroTrans/ProjectElectroTrans.iml b/ProjectElectroTrans/ProjectElectroTrans.iml index c90834f..d5b343a 100644 --- a/ProjectElectroTrans/ProjectElectroTrans.iml +++ b/ProjectElectroTrans/ProjectElectroTrans.iml @@ -3,6 +3,7 @@ + diff --git a/ProjectElectroTrans/Resources/arrowDown.png b/ProjectElectroTrans/Resources/arrowDown.png new file mode 100644 index 0000000..2d51306 Binary files /dev/null and b/ProjectElectroTrans/Resources/arrowDown.png differ diff --git a/ProjectElectroTrans/Resources/arrowLeft.png b/ProjectElectroTrans/Resources/arrowLeft.png new file mode 100644 index 0000000..2c406e8 Binary files /dev/null and b/ProjectElectroTrans/Resources/arrowLeft.png differ diff --git a/ProjectElectroTrans/Resources/arrowRight.png b/ProjectElectroTrans/Resources/arrowRight.png new file mode 100644 index 0000000..36ad5e5 Binary files /dev/null and b/ProjectElectroTrans/Resources/arrowRight.png differ diff --git a/ProjectElectroTrans/Resources/arrowUp.png b/ProjectElectroTrans/Resources/arrowUp.png new file mode 100644 index 0000000..e3a0383 Binary files /dev/null and b/ProjectElectroTrans/Resources/arrowUp.png differ diff --git a/ProjectElectroTrans/src/DirectionType.java b/ProjectElectroTrans/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/ProjectElectroTrans/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/ProjectElectroTrans/src/DrawningElectroTrans.java b/ProjectElectroTrans/src/DrawningElectroTrans.java new file mode 100644 index 0000000..36538cb --- /dev/null +++ b/ProjectElectroTrans/src/DrawningElectroTrans.java @@ -0,0 +1,184 @@ +import java.awt.*; +import java.util.Random; + +public class DrawningElectroTrans { + private EntityElectroTrans entityElectroTrans; + + public EntityElectroTrans getEntityElectroTrans() { + return entityElectroTrans; + } + + private Integer _pictureWidth; + private Integer _pictureHeight; + private Integer _startPosX; + private Integer _startPosY; + private final int _drawingElectroTransWidth = 120; + private final int _drawingElectroTransHeight = 70; + public DrawningElectroTransWheels drawningElectroTransWheels; + + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean floaters, boolean sail) { + entityElectroTrans = new EntityElectroTrans(); + entityElectroTrans.Init(speed, weight, bodyColor, additionalColor, floaters, sail); + _startPosY = null; + _startPosX = null; + _pictureWidth = null; + _pictureHeight = null; + + drawningElectroTransWheels = new DrawningElectroTransWheels(); + Random random = new Random(); + int paddlesCount = random.nextInt(2, 5); + drawningElectroTransWheels.setEnumNumber(paddlesCount); + + } + + + public void setPosition(int x, int y) { + if (_pictureHeight == null || _pictureWidth == null) + return; + _startPosX = x; + _startPosY = y; + + if (_drawingElectroTransWidth + x > _pictureWidth || x < 0) { + _startPosX = 0; + } + if (_drawingElectroTransHeight + y > _pictureHeight || y < 0) { + _startPosY = 0; + } + } + + public boolean setPictureSize(int width, int height) { + + if (_drawingElectroTransHeight > height || _drawingElectroTransWidth > width) + return false; + _pictureHeight = height; + _pictureWidth = width; + + if (_startPosX != null && _startPosY != null) { + if (_startPosX + _drawingElectroTransWidth > width) + _startPosX = width - _drawingElectroTransWidth; + if (_startPosY + _drawingElectroTransHeight > height) + _startPosY = height - _drawingElectroTransHeight; + } + + return true; + + + } + + public boolean moveTransport(DirectionType direction) { + if (entityElectroTrans == null || _pictureWidth == null || _pictureHeight == null) + return false; + switch (direction) { + case Left: + if (_startPosX - entityElectroTrans.Step() > 0) + _startPosX -= (int) entityElectroTrans.Step(); + return true; + case Up: + if (_startPosY - entityElectroTrans.Step() > 0) + _startPosY -= (int) entityElectroTrans.Step(); + return true; + case Right: + if (_startPosX + entityElectroTrans.Step() < _pictureWidth - _drawingElectroTransWidth) + _startPosX += (int) entityElectroTrans.Step(); + return true; + case Down: + if (_startPosY + entityElectroTrans.Step() < _pictureHeight - _drawingElectroTransHeight) + _startPosY += (int) entityElectroTrans.Step(); + return true; + default: + return false; + + + } + } + + public void drawElectroTrans(Graphics g) { + if (entityElectroTrans == null || _startPosX == null || _startPosY == null) { + return; + } + + Graphics2D g2d = (Graphics2D) g; + + Point[] electroTransBorders = new Point[]{ + new Point(_startPosX, _startPosY + 30), + new Point(_startPosX + 10, _startPosY + 10), + new Point(_startPosX + 70, _startPosY + 10), + new Point(_startPosX + 80, _startPosY + 30), + new Point(_startPosX + 80, _startPosY + 50), + new Point(_startPosX, _startPosY + 50), + }; + Polygon electroTransPolygon = new Polygon(); + for (Point point : electroTransBorders) + electroTransPolygon.addPoint(point.x, point.y); + + g2d.setColor(entityElectroTrans.getBodyColor()); + g2d.drawPolygon(electroTransPolygon); + + Point[] electroTransGlass = new Point[]{ + new Point(_startPosX + 2, _startPosY + 30), + new Point(_startPosX + 10, _startPosY + 13), + new Point(_startPosX + 70, _startPosY + 13), + new Point(_startPosX + 78, _startPosY + 30), + }; + + Polygon electroTransGlassPolygon = new Polygon(); + for (Point point : electroTransGlass) + electroTransGlassPolygon.addPoint(point.x, point.y); + + g2d.setColor(entityElectroTrans.getBodyColor()); + g2d.drawPolygon(electroTransGlassPolygon); + + + Point[] electroTransHorns; + if (entityElectroTrans.getHorns()) { + electroTransHorns = new Point[]{ + new Point(_startPosX + 40, _startPosY + 10), + new Point(_startPosX + 20, _startPosY), + new Point(_startPosX + 60, _startPosY), + + }; + } else { + electroTransHorns = new Point[]{ + new Point(_startPosX + 40, _startPosY + 7), + new Point(_startPosX + 20, _startPosY + 7), + new Point(_startPosX + 60, _startPosY + 7), + + }; + } + + Polygon electroTransHornsPolygon = new Polygon(); + for (Point point : electroTransHorns) + electroTransHornsPolygon.addPoint(point.x, point.y); + + g2d.setColor(entityElectroTrans.getAdditionalColor()); + g2d.drawPolygon(electroTransHornsPolygon); + + + if (entityElectroTrans.getBattery()) { + Point[] electroTransBattery = new Point[]{ + new Point(_startPosX + 25, _startPosY + 32), + new Point(_startPosX + 25, _startPosY + 36), + new Point(_startPosX + 22, _startPosY + 36), + new Point(_startPosX + 22, _startPosY + 40), + new Point(_startPosX + 25, _startPosY + 40), + new Point(_startPosX + 25, _startPosY + 46), + new Point(_startPosX + 58, _startPosY + 46), + new Point(_startPosX + 58, _startPosY + 32), + + }; + Polygon electroTransBatteryPolygon = new Polygon(); + for (Point point : electroTransBattery) + electroTransBatteryPolygon.addPoint(point.x, point.y); + + g2d.setColor(entityElectroTrans.getAdditionalColor()); + g2d.fillPolygon(electroTransBatteryPolygon); + + } + + drawningElectroTransWheels.drawElectroTransWheels(g, entityElectroTrans.getAdditionalColor(), _startPosX, _startPosY); + + + } + +} + diff --git a/ProjectElectroTrans/src/DrawningElectroTransWheels.java b/ProjectElectroTrans/src/DrawningElectroTransWheels.java new file mode 100644 index 0000000..03dca71 --- /dev/null +++ b/ProjectElectroTrans/src/DrawningElectroTransWheels.java @@ -0,0 +1,24 @@ +import java.awt.*; + +public class DrawningElectroTransWheels { + private WheelsCount wheelsCount; + + public void setEnumNumber(int wheelCount) { + for (WheelsCount value : WheelsCount.values()) { + if (value.getEnumNumber() == wheelCount) { + wheelsCount = value; + return; + } + } + } + + public void drawElectroTransWheels(Graphics g, Color color, float startPosX, float startPosY) { + + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(color); + g2d.setStroke(new BasicStroke(4)); + for (int i = 0; i < wheelsCount.getEnumNumber(); i++) { + g2d.drawOval((int) startPosX + (70 / 4) * (i + 1) + -4, (int) startPosY + 46, 8, 8); + } + } +} diff --git a/ProjectElectroTrans/src/EntityElectroTrans.java b/ProjectElectroTrans/src/EntityElectroTrans.java new file mode 100644 index 0000000..f987970 --- /dev/null +++ b/ProjectElectroTrans/src/EntityElectroTrans.java @@ -0,0 +1,41 @@ +import java.awt.*; + +public class EntityElectroTrans { + 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; + } + + + public double Step() { + return Speed*100/Weight; + } + private boolean Horns; + public boolean getHorns() { + return Horns; + } + private boolean Battery; + public boolean getBattery() { + return Battery; + } + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean battery) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Horns = horns; + Battery = battery; + } +} diff --git a/ProjectElectroTrans/src/FormElectroTrans.form b/ProjectElectroTrans/src/FormElectroTrans.form new file mode 100644 index 0000000..054558e --- /dev/null +++ b/ProjectElectroTrans/src/FormElectroTrans.form @@ -0,0 +1,93 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectElectroTrans/src/FormElectroTrans.java b/ProjectElectroTrans/src/FormElectroTrans.java new file mode 100644 index 0000000..fefd453 --- /dev/null +++ b/ProjectElectroTrans/src/FormElectroTrans.java @@ -0,0 +1,114 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import java.util.Random; +import java.util.List; + +public class FormElectroTrans extends JFrame { + protected DrawningElectroTrans _drawningElectroTrans = new DrawningElectroTrans(); + JPanel PanelWrapper; + private JPanel PictureBox; + private JButton buttonCreate; + private JButton buttonRight; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonUp; + + private List controls; + public FormElectroTrans() { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + InitializeControlsRepaintList(); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawningElectroTrans = new DrawningElectroTrans(); + Random random = new Random(); + + _drawningElectroTrans.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() ); + _drawningElectroTrans.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); + _drawningElectroTrans.setPosition(random.nextInt(25, 100), + random.nextInt(25, 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 = _drawningElectroTrans.moveTransport(DirectionType.Up); + } + break; + case "buttonDown": { + result = _drawningElectroTrans.moveTransport(DirectionType.Down); + } + break; + case "buttonLeft": { + result = _drawningElectroTrans.moveTransport(DirectionType.Left); + } + break; + case "buttonRight": { + result = _drawningElectroTrans.moveTransport(DirectionType.Right); + } + break; + + } + if (result) + Draw(); + + } + }; + buttonRight.addActionListener(buttonMoveClickedListener); + buttonDown.addActionListener(buttonMoveClickedListener); + buttonLeft.addActionListener(buttonMoveClickedListener); + buttonUp.addActionListener(buttonMoveClickedListener); + + } + private void Draw() { + if (_drawningElectroTrans.getEntityElectroTrans() == null) + return; + if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) { + return; + } + Graphics g = PictureBox.getGraphics(); + g.setColor(PictureBox.getBackground()); + g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight()); + _drawningElectroTrans.drawElectroTrans(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/ProjectElectroTrans/src/Main.java b/ProjectElectroTrans/src/Main.java index 71d53dd..7f24cba 100644 --- a/ProjectElectroTrans/src/Main.java +++ b/ProjectElectroTrans/src/Main.java @@ -1,9 +1,19 @@ +import javax.swing.*; + //TIP To Run code, press or // click the icon in the gutter. public class Main { public static void main(String[] args) { + JFrame.setDefaultLookAndFeelDecorated(false); + JFrame frame = new JFrame("Катамаран"); + frame.setContentPane(new FormElectroTrans().PanelWrapper); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(700, 500); + frame.setVisible(true); //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. - System.out.println("Hello and welcome!"); + } } \ No newline at end of file diff --git a/ProjectElectroTrans/src/WheelsCount.java b/ProjectElectroTrans/src/WheelsCount.java new file mode 100644 index 0000000..847d4b0 --- /dev/null +++ b/ProjectElectroTrans/src/WheelsCount.java @@ -0,0 +1,14 @@ +public enum WheelsCount { + Two(2), + Three(3), + Four(4); + + final private int EnumNumber; + WheelsCount(int enumNumber) { + EnumNumber = enumNumber; + } + public int getEnumNumber() { + return EnumNumber; + } + +}