diff --git a/DrawFrame.java b/BaseFrame.java similarity index 74% rename from DrawFrame.java rename to BaseFrame.java index 5f8b159..f887576 100644 --- a/DrawFrame.java +++ b/BaseFrame.java @@ -1,4 +1,3 @@ -import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -6,14 +5,16 @@ import java.awt.event.ActionListener; import java.util.Random; class DrawGasoline extends JComponent { - private DrawingTanker _drawingTanker; + private DrawTanker _drawTanker; private AbstractStrategy _abstractStrategy; + private DrawTanker SelectedCar; + public DrawTanker GetSelectedCar() {return SelectedCar;} public void paintComponent(Graphics g) { super.paintComponent(g); - if (_drawingTanker == null) + if (_drawTanker == null) return; - _drawingTanker.DrawTransport(g); + _drawTanker.DrawTransport(g); super.repaint(); } protected void CreateGasolineTankerButton_Click() @@ -21,10 +22,17 @@ class DrawGasoline extends JComponent { Random rnd = new Random(); Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); - _drawingTanker = new DrawGasolineTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), + Color newColor = JColorChooser.showDialog(null, "Choose a color", null); + if (newColor != null) + addColor = newColor; + newColor = JColorChooser.showDialog(null, "Choose a color", null); + if (newColor != null) + bodyColor = newColor; + _drawTanker = new DrawGasolineTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), bodyColor, addColor, IntToBool(rnd.nextInt(0, 2)), IntToBool(rnd.nextInt(0, 2)), - IntToBool(rnd.nextInt(0, 2)), Frame.Width, Frame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); - _drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + IntToBool(rnd.nextInt(0, 2)), BaseFrame.Width, BaseFrame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); + _drawTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + SelectedCar = _drawTanker; super.repaint(); } @@ -32,20 +40,23 @@ class DrawGasoline extends JComponent { { Random rnd = new Random(); Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); - _drawingTanker = new DrawingTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), bodyColor, Frame.Width, Frame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); - Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); - _drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + Color newColor = JColorChooser.showDialog(null, "Choose a color", null); + if (newColor != null) + bodyColor = newColor; + _drawTanker = new DrawTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), bodyColor, BaseFrame.Width, BaseFrame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); + _drawTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + SelectedCar = _drawTanker; super.repaint(); } protected void ButtonMove_Click(String tag) { - if (_drawingTanker == null) + if (_drawTanker == null) return; switch (tag) { - case "U" -> _drawingTanker.MoveTransport(Direction.Up); - case "D" -> _drawingTanker.MoveTransport(Direction.Down); - case "L" -> _drawingTanker.MoveTransport(Direction.Left); - case "R" -> _drawingTanker.MoveTransport(Direction.Right); + case "U" -> _drawTanker.MoveTransport(Direction.Up); + case "D" -> _drawTanker.MoveTransport(Direction.Down); + case "L" -> _drawTanker.MoveTransport(Direction.Left); + case "R" -> _drawTanker.MoveTransport(Direction.Right); } super.repaint(); } @@ -57,7 +68,7 @@ class DrawGasoline extends JComponent { protected void ButtonStep_Click(JComboBox JCB, int width, int height) { - if (_drawingTanker == null) + if (_drawTanker == null) return; if (JCB.isEnabled()) { @@ -70,7 +81,7 @@ class DrawGasoline extends JComponent { }; if (_abstractStrategy == null) return; - _abstractStrategy.SetData(new DrawingObjectTanker(_drawingTanker), width, height); + _abstractStrategy.SetData(_drawTanker.GetMoveableObject(), width, height); JCB.setEnabled(false); } if (_abstractStrategy == null) @@ -85,8 +96,10 @@ class DrawGasoline extends JComponent { } } -class Frame extends JFrame { - public Frame() +class BaseFrame extends JFrame { + + public JButton buttonSelect; + public BaseFrame() { initUI(); } @@ -158,6 +171,11 @@ class Frame extends JFrame { } }); + buttonSelect = new JButton("Select tanker"); + buttonSelect.setBounds(Width - 120, Height - 150, 80, 30); + buttonSelect.setLayout(null); + add(buttonSelect); + buttonUp.setIcon(new ImageIcon("Arrows/Up.png")); buttonDown.setIcon(new ImageIcon("Arrows/Down.png")); buttonLeft.setIcon(new ImageIcon("Arrows/Left.png")); diff --git a/CarsGenericCollection.java b/CarsGenericCollection.java new file mode 100644 index 0000000..7f41796 --- /dev/null +++ b/CarsGenericCollection.java @@ -0,0 +1,67 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class CarsGenericCollection { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 110; + private final int _placeSizeHeight = 80; + private final SetGeneric _collection; + public CarsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + public int plus(T gasTanker) + { + return _collection.Insert(gasTanker); + } + + public boolean minus(int position) + { + return _collection.Remove(position); + } + + public U GetU(int pos) + { + return (U)_collection.Get(pos).GetMoveableObject(); + } + public Image ShowCars() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics g = bmp.getGraphics(); + DrawBackground(g); + DrawObjects(g); + return bmp; + } + + private void DrawBackground(Graphics g) + { + g.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight/_placeSizeHeight + 1; j++) + { + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + } + + private void DrawObjects(Graphics g) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count(); i++) { + DrawTanker tank = _collection.Get(i); + if (tank == null) + continue; + tank.SetPosition(i % width * _placeSizeWidth, (height - i / height - 1) * _placeSizeHeight); + tank.DrawTransport(g); + } + } + +} diff --git a/CollectionFrame.java b/CollectionFrame.java new file mode 100644 index 0000000..6f05f8b --- /dev/null +++ b/CollectionFrame.java @@ -0,0 +1,136 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +class CollectionFrame extends JComponent { + private final CarsGenericCollection _tanks; + + protected final int Width; + protected final int Height; + public CollectionFrame(int width, int height) + { + Width = width; + Height = height; + _tanks = new CarsGenericCollection<>(Width, Height); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(_tanks.ShowCars(), 0, 0, this); + super.repaint(); + } + protected void ButtonAddTank_Click() + { + BaseFrame form = new BaseFrame(); + form.setVisible(true); + form.buttonSelect.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (_tanks.plus(form.Gasoline.GetSelectedCar()) != -1) + { + form.dispose(); + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Объект добавлен"); + + } + else + { + form.dispose(); + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось добавить объект"); + } + } + } + ); + } + + protected void ButtonRemoveTank_Click(String text) + { + if (JOptionPane.showConfirmDialog(null, "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) + { + if (_tanks.minus(Integer.parseInt(text))) + { + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + super.repaint(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + } + super.repaint(); + } + + protected void ButtonUpdate_Click() {super.repaint();} + +} + +class GarageFrame extends JFrame { + public GarageFrame() + { + initUI(); + } + + protected static final int Width = 1000; + protected static final int Height = 600; + CollectionFrame Collection; + private void initUI() + { + setSize(Width, Height); + setTitle("TankGasoline"); + setLocationRelativeTo(null); + setLayout(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + + + JButton addTankerButton = new JButton("Add tanker"); + addTankerButton.setLayout(null); + addTankerButton.setBounds(Width-200, Height-550, 200, 30); + add(addTankerButton); + addTankerButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Collection.ButtonAddTank_Click(); + } + }); + + JTextField indexTankerField = new JTextField(); + indexTankerField.setBounds(Width- 200, Height-500, 200, 30); + indexTankerField.setLayout(null); + add(indexTankerField); + + JButton deleteTankerButton = new JButton("Delete tanker"); + deleteTankerButton.setLayout(null); + deleteTankerButton.setBounds(Width-200, Height-450, 200, 30); + add(deleteTankerButton); + deleteTankerButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Collection.ButtonRemoveTank_Click(indexTankerField.getText()); + } + }); + + JButton updateCollectionButton = new JButton("Update collection"); + updateCollectionButton.setLayout(null); + updateCollectionButton.setBounds(Width-200, Height-400, 200, 30); + add(updateCollectionButton); + updateCollectionButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Collection.ButtonUpdate_Click(); + } + }); + + + + Collection = new CollectionFrame(Width-200, Height); + Collection.setLayout(null); + Collection.setBounds(0, 0, Width-200, Height); + add(Collection); + + } + +} diff --git a/ComboFrame.java b/ComboFrame.java new file mode 100644 index 0000000..3d056a4 --- /dev/null +++ b/ComboFrame.java @@ -0,0 +1,105 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +class DrawRandomTanker extends JComponent { + private DrawTanker _drawTanker; + private final ComboGenericCollection GeneratorTanker; + + public DrawRandomTanker() { + GeneratorTanker = new ComboGenericCollection<>(25, 1000, 600); + GeneratorTanker.Add(GenerateTanker()); + GeneratorTanker.Add(GenerateWheel()); + GeneratorTanker.Add(GenerateTanker()); + GeneratorTanker.Add(GenerateWheel()); + GeneratorTanker.Add(GenerateTanker()); + GeneratorTanker.Add(GenerateWheel()); + } + public void paintComponent(Graphics g) + { + super.paintComponent(g); + if (_drawTanker == null) + return; + _drawTanker.DrawTransport(g); + super.repaint(); + } + private boolean IntToBool(int n) {return n % 2 == 0;} + public BaseTanker GenerateTanker() + { + Random random = new Random(); + int mode = random.nextInt(0, 100) % 2; + if (mode == 0) + { + return new BaseTanker(random.nextInt(100, 200), random.nextInt(2000, 4000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + } + else + { + return new GasolineTanker(random.nextInt(100, 200), random.nextInt(2000, 4000), + 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)), + IntToBool(random.nextInt(0, 100)),IntToBool(random.nextInt(0, 100)), IntToBool(random.nextInt(0, 100))); + } + } + + public IWheelDraw GenerateWheel() + { + Random random = new Random(); + int mode = random.nextInt(0, 100) % 3; + IWheelDraw Wheels = new DrawWheelSquare(); + switch (mode) + { + case 0 -> {Wheels = new DrawWheelCircle();} + case 1 -> {Wheels = new DrawWheelClassic();} + case 2 -> {Wheels = new DrawWheelSquare();} + } + int count = random.nextInt(0, 100); + Wheels.setWheelCount(count); + return Wheels; + } + protected void GenerateTanker_Click() + { + _drawTanker = GeneratorTanker.CreateDraw(); + super.repaint(); + } +} + +class ComboFrame extends JFrame { + + public ComboFrame() + { + initUI(); + } + + protected static final int Width = 1000; + protected static final int Height = 600; + DrawRandomTanker Tanker; + + private void initUI() + { + setSize(Width, Height); + setTitle("TankGasoline"); + setLocationRelativeTo(null); + setLayout(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + Tanker = new DrawRandomTanker(); + Tanker.setBounds(0, 0, Width, Height); + add(Tanker); + + JButton UpdateButton = new JButton("Update tanker"); + UpdateButton.setLayout(null); + UpdateButton.setBounds(Width-200, Height- 200, 100, 100); + add(UpdateButton); + UpdateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Tanker.GenerateTanker_Click(); + } + }); + + } + +} diff --git a/ComboGenericCollection.java b/ComboGenericCollection.java new file mode 100644 index 0000000..42af0f9 --- /dev/null +++ b/ComboGenericCollection.java @@ -0,0 +1,66 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class ComboGenericCollection { + private final T[] _tankers; + private final U[] _wheels; + public int MaxSize() {return _tankers.length;} + + private int CountTankers = 0; + private int CountWheels = 0; + private final Random random; + private final int Width; + private final int Height; + + public ComboGenericCollection(int count, int width, int height) + { + _tankers = (T[]) new BaseTanker[count]; + _wheels = (U[]) new IWheelDraw[count]; + random = new Random(); + Width = width; + Height = height; + } + + + + public boolean Add(T tanker) + { + for (int i = 0; i < MaxSize(); i++) + { + if (_tankers[i] == null) + { + _tankers[i] = tanker; + CountTankers++; + return true; + } + } + return false; + } + + public boolean Add(U wheel) + { + for (int i = 0; i < MaxSize(); i++) + { + if (_wheels[i] == null) + { + _wheels[i] = wheel; + CountWheels++; + return true; + } + } + return false; + } + + + + + public DrawTanker CreateDraw() + { + T tanker = _tankers[random.nextInt(0, CountTankers)]; + if (tanker instanceof GasolineTanker) { + return new DrawGasolineTanker((GasolineTanker) tanker, Width, Height, _wheels[random.nextInt(0, CountWheels)]); + } + return new DrawTanker(tanker, Width, Height, _wheels[random.nextInt(0, CountWheels)]); + } +} diff --git a/DrawGasolineTanker.java b/DrawGasolineTanker.java index 2afeea0..ced75df 100644 --- a/DrawGasolineTanker.java +++ b/DrawGasolineTanker.java @@ -1,12 +1,18 @@ import java.awt.*; -public class DrawGasolineTanker extends DrawingTanker { +public class DrawGasolineTanker extends DrawTanker { public DrawGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount, int wheelMode) { super(speed, weight, bodyColor, width, height, wheelCount, wheelMode); if (GasolineTanker != null) { GasolineTanker = new GasolineTanker(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); } } + public DrawGasolineTanker(GasolineTanker tanker, int width, int height, IWheelDraw wheelMode) + { + super(tanker, width, height, wheelMode); + if (GasolineTanker != null) + GasolineTanker = tanker; + } @Override public void DrawTransport(Graphics g) { if (!(GasolineTanker instanceof GasolineTanker)) { diff --git a/DrawingTanker.java b/DrawTanker.java similarity index 86% rename from DrawingTanker.java rename to DrawTanker.java index 592a7bf..24e8676 100644 --- a/DrawingTanker.java +++ b/DrawTanker.java @@ -1,6 +1,6 @@ import java.awt.*; -public class DrawingTanker { +public class DrawTanker { protected BaseTanker GasolineTanker; public BaseTanker GetGasolineTanker() {return GasolineTanker;} private IWheelDraw wheelsDrawing; @@ -34,7 +34,7 @@ public class DrawingTanker { } } - public DrawingTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount, int wheelMode) + public DrawTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount, int wheelMode) { _pictureHeight = height; _pictureWidth = width; @@ -49,6 +49,14 @@ public class DrawingTanker { wheelsDrawing.setWheelCount(wheelCount); } + public DrawTanker(BaseTanker tanker, int width, int height, IWheelDraw wheels) + { + GasolineTanker = tanker; + _pictureHeight = height; + _pictureWidth = width; + wheelsDrawing = wheels; + } + public void SetPosition(int x, int y) { _startPosX = x; @@ -98,4 +106,6 @@ public class DrawingTanker { wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d); } + + public IMoveableObject GetMoveableObject() {return new DrawingObjectTanker(this);} } diff --git a/DrawingObjectTanker.java b/DrawingObjectTanker.java index cc1b0cc..2800b3d 100644 --- a/DrawingObjectTanker.java +++ b/DrawingObjectTanker.java @@ -1,9 +1,7 @@ -import java.util.Optional; - public class DrawingObjectTanker implements IMoveableObject { - private final DrawingTanker _drawTanker; + private final DrawTanker _drawTanker; - public DrawingObjectTanker(DrawingTanker drawTanker) { + public DrawingObjectTanker(DrawTanker drawTanker) { _drawTanker = drawTanker; } diff --git a/Main.java b/Main.java index d155d60..618cc09 100644 --- a/Main.java +++ b/Main.java @@ -1,6 +1,7 @@ public class Main { public static void main(String[] args) { - Frame a = new Frame(); + //GarageFrame a = new GarageFrame(); + ComboFrame a = new ComboFrame(); a.setVisible(true); } } \ No newline at end of file diff --git a/SetGeneric.java b/SetGeneric.java new file mode 100644 index 0000000..cf235bd --- /dev/null +++ b/SetGeneric.java @@ -0,0 +1,56 @@ +public class SetGeneric { + private final T[] _places; + public int Count() {return _places.length;} + public SetGeneric(int count) + { + _places = (T[]) new Object[count]; + } + public int Insert(T tanker) + { + return Insert(tanker, 0); + } + public int Insert(T tanker, int position) + { + if (position < 0 || position >= Count()) + return -1; + if (_places[position] == null) + { + _places[position] = tanker; + return position; + } + int index = -1; + for (int i = position; i < Count(); i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + return -1; + for (int i = index; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = tanker; + return position; + } + + public boolean Remove(int position) + { + if (position < 0 || position > Count()) + return false; + _places[position] = null; + return true; + } + + public T Get(int position) + { + if (position < 0 || position >= Count()) + { + return null; + } + return (T)_places[position]; + } + +}