From ba8046949ce7542d95c0a0156a6a139ba89908cd Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Fri, 21 Oct 2022 09:13:03 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B5=D0=B8=D1=8F=20=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/AircraftCarrier_Hard.iml | 1 + .idea/compiler.xml | 8 ++ src/DrawingMap.java | 70 ++++++++++++++++ src/DrawingObjectWarship.java | 4 +- src/FormMap.form | 137 +++++++++++++++++++++++++++++++ src/FormMap.java | 144 +++++++++++++++++++++++++++++++++ src/IDrawingObject.java | 2 +- src/LineMap.java | 53 ++++++++++++ src/Main.java | 2 +- src/SimpleMap.java | 43 ++++++++++ 10 files changed, 460 insertions(+), 4 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 src/DrawingMap.java create mode 100644 src/FormMap.form create mode 100644 src/FormMap.java create mode 100644 src/LineMap.java create mode 100644 src/SimpleMap.java diff --git a/.idea/AircraftCarrier_Hard.iml b/.idea/AircraftCarrier_Hard.iml index c90834f..b319ae2 100644 --- a/.idea/AircraftCarrier_Hard.iml +++ b/.idea/AircraftCarrier_Hard.iml @@ -4,6 +4,7 @@ + diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..4b1fc99 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/src/DrawingMap.java b/src/DrawingMap.java new file mode 100644 index 0000000..3a2c2ab --- /dev/null +++ b/src/DrawingMap.java @@ -0,0 +1,70 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class DrawingMap extends JPanel{ + private final FormMap Map; + private AbstractMap _abstractMap; + BufferedImage bufferedImage; + + public DrawingMap(FormMap map){ + Map=map; + _abstractMap = new SimpleMap(); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(bufferedImage,0,0,null); + } + + private void SetData(DrawingWarship warship) { + Random rand=new Random(); + warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); + Map.SpeedLabel.setText("Скорость: "+warship.GetWarship().GetSpeed()); + Map.WeightLabel.setText("Вес: "+warship.GetWarship().GetWeight()); + Map.BodyColorLabel.setText("Цвет: "+Integer.toHexString(warship.GetWarship().GetBodyColor().getRGB()).substring(2)); + bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectWarship(warship)); + } + public void CreateButtonAction(){ + Random rand=new Random(); + Color color1 = JColorChooser.showDialog(Map, "Выберите цвет тела корабля", null); + if(color1==null) + color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + DrawingWarship warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3)); + SetData(warship); + } + public void CreateModifButtonAction(){ + Random rand=new Random(); + Color color1=JColorChooser.showDialog(Map, "Выберите цвет тела корабля",null); + Color color2=JColorChooser.showDialog(Map, "Выборите цвет модификаций корабля",null); + if(color1==null) + color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + if (color2==null) + color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + DrawingWarship warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1, + color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); + SetData(warship); + } + public void DirectionButtonAction(Direction side){ + if(_abstractMap != null){ + bufferedImage = _abstractMap.MoveObject(side); + } + } + + public void ComboBoxSelectorMapAction(String name){ + switch (name){ + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + case "Вторая карта": + _abstractMap = new SecondMap(); + break; + } + } + + public void DrawMap(Graphics g){ + g.drawImage(bufferedImage,0,0,null); + } +} diff --git a/src/DrawingObjectWarship.java b/src/DrawingObjectWarship.java index 6815af4..02d0a4a 100644 --- a/src/DrawingObjectWarship.java +++ b/src/DrawingObjectWarship.java @@ -25,8 +25,8 @@ public class DrawingObjectWarship implements IDrawingObject { } @Override - public void DrawningObject(Graphics2D g2) { - _warship.DrawTransport(g2); + public void DrawningObject(Graphics g) { + _warship.DrawTransport(g); } @Override diff --git a/src/FormMap.form b/src/FormMap.form new file mode 100644 index 0000000..5e59229 --- /dev/null +++ b/src/FormMap.form @@ -0,0 +1,137 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormMap.java b/src/FormMap.java new file mode 100644 index 0000000..ab233da --- /dev/null +++ b/src/FormMap.java @@ -0,0 +1,144 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Objects; +import java.util.Random; + +public class FormMap extends JFrame{ + private AbstractMap _abstractMap; + private JPanel mainPanel; + private JToolBar toolBar; + private JLabel toolBarLabelSpeed; + private JLabel toolBarLabelWieght; + private JLabel toolBarLabelColor; + private JButton buttonCreate; + private JButton buttonCreateModif; + private JButton buttonRight; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JComboBox ComboBoxSelectorMap; + private JPanel drawPanel; + + public FormMap(){ + InitializeComponent(); + _abstractMap = new SimpleMap(); + } + + private void SetData(DrawingWarship warship){ + Graphics2D graphics = (Graphics2D) drawPanel.getGraphics(); + graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight()); + drawPanel.paintComponents(graphics); + graphics.drawImage(_abstractMap.CreateMap(drawPanel.getWidth(), drawPanel.getHeight(), new DrawingObjectWarship(warship)),0,0,null); + toolBarLabelSpeed.setText("Color: " + warship.GetWarship().GetSpeed() + " "); + toolBarLabelWieght.setText("Weight: " + warship.GetWarship().GetWeight() + " "); + toolBarLabelColor.setText("Color: " + warship.GetWarship().GetBodyColor().getRed() + " " + + warship.GetWarship().GetBodyColor().getGreen() + " " + warship.GetWarship().GetBodyColor().getBlue()); + } + + private void resizeWindow() { + _warship.ChangeBorders(drawPanel.getWidth(), drawPanel.getHeight()); + Draw(); + } + + private void InitializeComponent() { + setContentPane(mainPanel); + setTitle("Warship"); + setSize(900, 700); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setVisible(true); + + Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg"); + buttonUp.setIcon(iconUp); + Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg"); + buttonDown.setIcon(iconDown); + Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg"); + buttonLeft.setIcon(iconLeft); + Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); + buttonRight.setIcon(iconRight); + + ComboBoxSelectorMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String mapName = Objects.requireNonNull(ComboBoxSelectorMap.getSelectedItem()).toString(); + switch (mapName){ + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + case "Вторая карта": + _abstractMap = new LineMap(); + break; + } + } + }); + + //кнопка добавления объекта + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + var warship = new DrawingWarship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + SetData(warship); + Draw(); + } + }); + + //добавление модифицированного объекта + /*buttonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + _warship = new DrawingAircraftCarrier(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean()); + SetData(); + Draw(); + } + }); + + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_warship != null) _warship.MoveTransport(Direction.Left); + Draw(); + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_warship != null) _warship.MoveTransport(Direction.Right); + Draw(); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_warship != null) _warship.MoveTransport(Direction.Up); + Draw(); + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_warship != null) _warship.MoveTransport(Direction.Down); + Draw(); + } + });*/ + + mainPanel.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + resizeWindow(); + } + }); + } +} diff --git a/src/IDrawingObject.java b/src/IDrawingObject.java index 230cd63..f65592b 100644 --- a/src/IDrawingObject.java +++ b/src/IDrawingObject.java @@ -8,7 +8,7 @@ public interface IDrawingObject { // Изменение направления пермещения объекта void MoveObject(Direction direction); // Отрисовка объекта - void DrawningObject(Graphics2D g2); + void DrawningObject(Graphics g); // Получение текущей позиции объекта // /Left, Right, Top, Bottom) float[] GetCurrentPosition(); diff --git a/src/LineMap.java b/src/LineMap.java new file mode 100644 index 0000000..8bb3801 --- /dev/null +++ b/src/LineMap.java @@ -0,0 +1,53 @@ +import java.awt.*; + +public class LineMap extends AbstractMap{ + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.gray); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.BLUE); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + boolean flag = true; + while (counter < 20) + { + int lineX = _random.nextInt(11, 89); + int lineY = _random.nextInt(11, 89); + + if (flag) + { + for (int i = lineY; i <= lineY + 10; i++) + _map[lineX][i] = _barrier; + flag = false; + } + else + { + for (int i = lineX; i <= lineX + 10; i++) + _map[i][lineY] = _barrier; + flag = true; + } + counter++; + } + } +} diff --git a/src/Main.java b/src/Main.java index 2603a8a..68ad080 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - new FormWarship(); + new FormMap(); } } diff --git a/src/SimpleMap.java b/src/SimpleMap.java new file mode 100644 index 0000000..32f9f76 --- /dev/null +++ b/src/SimpleMap.java @@ -0,0 +1,43 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap{ + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.gray); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.BLUE); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } +}