From 6da849d421848f38690cebe10634c8547a91b570 Mon Sep 17 00:00:00 2001 From: Alenka Date: Fri, 24 Nov 2023 22:49:33 +0400 Subject: [PATCH 1/2] Start --- Main.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Main.java diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..503e72c --- /dev/null +++ b/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} \ No newline at end of file -- 2.25.1 From bab381dabe9b49a7a960de74513400ec184bb0dd Mon Sep 17 00:00:00 2001 From: Alenka Date: Fri, 24 Nov 2023 23:51:47 +0400 Subject: [PATCH 2/2] Done --- Direction.java | 6 +++ DrawningCruiser.java | 108 ++++++++++++++++++++++++++++++++++++++++ EntityCruiser.java | 47 +++++++++++++++++ GameFrame.java | 106 +++++++++++++++++++++++++++++++++++++++ Main.java | 2 +- NumberOfWheels.java | 35 +++++++++++++ NumberOfWheelsEnum.java | 9 ++++ 7 files changed, 312 insertions(+), 1 deletion(-) create mode 100644 Direction.java create mode 100644 DrawningCruiser.java create mode 100644 EntityCruiser.java create mode 100644 GameFrame.java create mode 100644 NumberOfWheels.java create mode 100644 NumberOfWheelsEnum.java 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/DrawningCruiser.java b/DrawningCruiser.java new file mode 100644 index 0000000..24e3019 --- /dev/null +++ b/DrawningCruiser.java @@ -0,0 +1,108 @@ +import java.awt.*; +public class DrawningCruiser { + public EntityCruiser EntityCruiser; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + final private int _cruiserWidth = 110; + final private int _cruiserHeight = 60; + private NumberOfWheels wheels = new NumberOfWheels(); + public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean helicopterPad, boolean coating, int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + EntityCruiser = new EntityCruiser(); + EntityCruiser.Init(speed, weight, bodyColor, additionalColor, helicopterPad, coating); + return true; + } + public void SetPosition(int x, int y) + { + if (EntityCruiser == null) { + return; + } + _startPosX = x; + _startPosY = y; + if (x + _cruiserWidth <= _pictureWidth || y + _cruiserHeight <= _pictureHeight) { + _startPosX = 0; + _startPosY = 0; + } + } + public void MoveTransport(Direction direction) + { + if (EntityCruiser == null) + { + return; + } + switch (direction) + { + case Left: + if (_startPosX - EntityCruiser.Step() > 0) + { + _startPosX -= (int)EntityCruiser.Step(); + } + break; + case Up: + if (_startPosY - EntityCruiser.Step() > 0) + { + _startPosY -= (int)EntityCruiser.Step(); + } + break; + case Right: + if (_startPosX + EntityCruiser.Step() + _cruiserWidth < _pictureWidth) + { + _startPosX += (int)EntityCruiser.Step(); + } + break; + case Down: + if (_startPosY + EntityCruiser.Step() + _cruiserHeight < _pictureHeight) + { + _startPosY += (int)EntityCruiser.Step(); + } + break; + } + } + public void setWheels(int num){ + wheels.setNumOfWheels(Integer.toString(num)); + } + public void DrawTransport(Graphics g) + { + if (EntityCruiser == null) + { + return; + } + g.setColor(EntityCruiser.BodyColor()); + g.drawRect( _startPosX + 9, _startPosY + 15, 10, 30); + g.drawRect( _startPosX + 90, _startPosY + 15, 10, + 30); + g.drawRect( _startPosX + 20, _startPosY + 4, 70, 52); + g.fillRect( _startPosX + 10, _startPosY + 15, 10, 30); + g.fillRect( _startPosX + 90, _startPosY + 15, 10, 30); + g.fillRect( _startPosX + 20, _startPosY + 5, 70, 50); + Point[] points1 = new Point[3]; + points1[0] = new Point(_startPosX + 100, _startPosY + 5); + points1[1] = new Point(_startPosX + 100, _startPosY + 55); + points1[2] = new Point(_startPosX + 150, _startPosY + 30); + g.fillPolygon(new int[] {points1[0].x, points1[1].x, points1[2].x}, + new int[] {points1[0].y, points1[1].y, points1[2].y}, 3); + g.fillRect( _startPosX + 5, _startPosY + 15, 10, 10); + g.fillRect( _startPosX + 5, _startPosY + 35, 10, 10); + if (EntityCruiser.HelicopterPad()) + { + g.setColor(EntityCruiser.AdditionalColor()); + g.drawRect(_startPosX + 35, + _startPosY + 23, 15, 15); + g.drawRect(_startPosX + 50, + _startPosY + 19, 30, 25); + } + if (EntityCruiser.Coating()) + { + g.setColor(EntityCruiser.AdditionalColor()); + g.fillRect(_startPosX + 35, + _startPosY + 23, 15, 15); + g.fillRect(_startPosX + 50, + _startPosY + 19, 30, 25); + } + wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor()); + } +} \ No newline at end of file diff --git a/EntityCruiser.java b/EntityCruiser.java new file mode 100644 index 0000000..0c33c7b --- /dev/null +++ b/EntityCruiser.java @@ -0,0 +1,47 @@ +import java.awt.*; + +public class EntityCruiser { + private int Speed; + private double Weight; + private Color BodyColor; + private Color AdditionalColor; + private boolean HelicopterPad; + private boolean Coating; + private double Step; + public int Speed() { + return Speed; + } + public double Weight() { + return Weight; + } + + public Color BodyColor() { + return BodyColor; + } + + public Color AdditionalColor() { + return AdditionalColor; + } + + public boolean HelicopterPad() { + return HelicopterPad; + } + + public boolean Coating() { + return Coating; + } + public double Step() { + return Step; + } + public void Init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean helicopterPad, boolean coating) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + HelicopterPad = helicopterPad; + Coating = coating; + Step = Speed * 100 / Weight; + } +} \ No newline at end of file diff --git a/GameFrame.java b/GameFrame.java new file mode 100644 index 0000000..0d0d956 --- /dev/null +++ b/GameFrame.java @@ -0,0 +1,106 @@ +import javax.swing.*; +import javax.swing.plaf.basic.BasicArrowButton; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class GameFrame extends JFrame { + GameFrame() { + this.setSize(710, 535); + this.setTitle("Cruiser"); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setResizable(false); + this.setLocationRelativeTo(null); + GamePanel panel = new GamePanel(); + this.add(panel); + this.setVisible(true); + } + private boolean intToBoolean(int input) { + return input != 0; + } + public class GamePanel extends JPanel implements ActionListener { + static final int SCREEN_W = 700; + static final int SCREEN_H = 500; + int xx = 0; + int yy = 0; + private DrawningCruiser _drawningCruiser; + GamePanel() { + this.setLayout(null); + this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H)); + GridBagConstraints layers = new GridBagConstraints(); + JButton button = new JButton("Создать") ; + button.setBounds(20, 390, 90, 30); + this.add(button); + button.addActionListener(this); + JPanel panel = new JPanel(new GridBagLayout()); + JButton up = new BasicArrowButton(BasicArrowButton.NORTH); + up.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawningCruiser.MoveTransport(Direction.Up); + repaint(); + } + }); + JButton left = new BasicArrowButton(BasicArrowButton.WEST); + left.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawningCruiser.MoveTransport(Direction.Left); + repaint(); + } + }); + JButton down = new BasicArrowButton(BasicArrowButton.SOUTH); + down.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawningCruiser.MoveTransport(Direction.Down); + repaint(); + } + }); + JButton right = new BasicArrowButton(BasicArrowButton.EAST); + right.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawningCruiser.MoveTransport(Direction.Right); + repaint(); + } + }); + up.setBounds(570, 350, 30, 30); + this.add(up); + down.setBounds(570, 350 + 40, 30, 30); + this.add(down); + left.setBounds(570 - 40, 350 + 40, 30, 30); + this.add(left); + right.setBounds(570 + 40, 350 + 40, 30, 30); + this.add(right); + } + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + draw(g); + } + public void draw(Graphics g){ + if (_drawningCruiser != null){ + _drawningCruiser.DrawTransport(g); + } + } + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + _drawningCruiser = new DrawningCruiser(); + _drawningCruiser.Init(random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + intToBoolean(random.nextInt(0, 2)), + intToBoolean(random.nextInt(0, 2)), + GamePanel.SCREEN_W, GamePanel.SCREEN_H); + _drawningCruiser.SetPosition(random.nextInt(10, 100), + random.nextInt(10, 100)); + _drawningCruiser.setWheels(random.nextInt(1, 5)); + + repaint(); + }; + } +} \ No newline at end of file diff --git a/Main.java b/Main.java index 503e72c..9f73e7f 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - System.out.println("Hello World!"); + GameFrame frame = new GameFrame(); } } \ No newline at end of file diff --git a/NumberOfWheels.java b/NumberOfWheels.java new file mode 100644 index 0000000..d2af550 --- /dev/null +++ b/NumberOfWheels.java @@ -0,0 +1,35 @@ +import java.awt.*; +public class NumberOfWheels { + public int numOfWheels; + public void setNumOfWheels(String num){ + switch (Integer.valueOf(num)){ + case 2: + numOfWheels = NumberOfWheelsEnum.wheel_2.value; + break; + case 3: + numOfWheels = NumberOfWheelsEnum.wheel_3.value; + break; + case 4: + numOfWheels = NumberOfWheelsEnum.wheel_4.value; + break; + default: + numOfWheels = NumberOfWheelsEnum.wheel_2.value; + } + } + public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) { + g.setColor(Color.black); + if (numOfWheels == 2) { + g.fillOval(_startPosX+20, _startPosY + 20, 15, 15); + g.fillOval(_startPosX + 45, _startPosY + 20, 15, 15); + } else if (numOfWheels == 3) { + g.fillOval(_startPosX, _startPosY + 20, 15, 15); + g.fillOval(_startPosX + 25, _startPosY + 20, 15, 15); + g.fillOval(_startPosX + 55, _startPosY + 20, 15, 15); + } else { + g.fillOval(_startPosX, _startPosY + 20, 15, 15); + g.fillOval(_startPosX + 15, _startPosY + 20, 15, 15); + g.fillOval(_startPosX + 65 , _startPosY + 20, 15, 15); + g.fillOval(_startPosX + 85, _startPosY + 20, 15, 15); + } + } +} \ No newline at end of file diff --git a/NumberOfWheelsEnum.java b/NumberOfWheelsEnum.java new file mode 100644 index 0000000..53b6b5a --- /dev/null +++ b/NumberOfWheelsEnum.java @@ -0,0 +1,9 @@ +public enum NumberOfWheelsEnum { + wheel_2(2), + wheel_3(3), + wheel_4(4); + public int value; + NumberOfWheelsEnum(int value) { + this.value = value; + } +} \ No newline at end of file -- 2.25.1