diff --git a/src/RoadTrain/DirectionType.java b/src/RoadTrain/DirectionType.java index 722f88a..bad0777 100644 --- a/src/RoadTrain/DirectionType.java +++ b/src/RoadTrain/DirectionType.java @@ -1,4 +1,5 @@ package RoadTrain; + public enum DirectionType { Up, Down, diff --git a/src/RoadTrain/DrawingObjects/DrawingRoadTrain.java b/src/RoadTrain/DrawingObjects/DrawingRoadTrain.java index 8fc05a0..d9f4cf6 100644 --- a/src/RoadTrain/DrawingObjects/DrawingRoadTrain.java +++ b/src/RoadTrain/DrawingObjects/DrawingRoadTrain.java @@ -1,6 +1,7 @@ package RoadTrain.DrawingObjects; import java.awt.*; import RoadTrain.Entities.*; +import RoadTrain.Extra.IDrawingWheels; public class DrawingRoadTrain extends DrawingTruck{ @@ -14,6 +15,11 @@ public class DrawingRoadTrain extends DrawingTruck{ additionalColor, waterContainer, sweepingBrush); } } + public DrawingRoadTrain(EntityRoadTrain truck, IDrawingWheels _wheelDrawing, int width, int height, int wheelNumber ){ + super(truck, _wheelDrawing, width, height, wheelNumber); + if (height < _truckHeight || width < _truckWidth) + return; + } @Override public void DrawTransport(Graphics g) { if (!(entityTruck instanceof EntityRoadTrain RoadTrain)) { diff --git a/src/RoadTrain/DrawingObjects/DrawingTruck.java b/src/RoadTrain/DrawingObjects/DrawingTruck.java index 00f4c35..19bf6a4 100644 --- a/src/RoadTrain/DrawingObjects/DrawingTruck.java +++ b/src/RoadTrain/DrawingObjects/DrawingTruck.java @@ -2,6 +2,7 @@ package RoadTrain.DrawingObjects; import RoadTrain.DirectionType; import RoadTrain.Entities.*; import RoadTrain.Extra.*; +import RoadTrain.MovementStraregy.*; import java.util.Random; import java.awt.*; @@ -13,6 +14,7 @@ public class DrawingTruck { private int _pictureHeight; protected int _startPosX; protected int _startPosY; + public IMoveableObject GetMoveableObject() { return new DrawingObjectTruck(this);} public int GetPosX() { return _startPosX; } @@ -28,8 +30,17 @@ public class DrawingTruck { public int GetHeight() { return _truckHeight; } - private int _truckWidth = 80; - private int _truckHeight = 70; + protected int _truckWidth = 80; + protected int _truckHeight = 70; + public DrawingTruck(EntityTruck truck, IDrawingWheels _wheelDrawing, int width, int height, int wheelNumber ){ + if (height < _truckHeight || width < _truckWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + entityTruck = truck; + drawingWheels = _wheelDrawing; + drawingWheels.SetWheelNumber(wheelNumber); + } public DrawingTruck(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) { if (width < _truckWidth || height < _truckHeight) { return; diff --git a/src/RoadTrain/Extra/GenericDopClass.java b/src/RoadTrain/Extra/GenericDopClass.java new file mode 100644 index 0000000..024429f --- /dev/null +++ b/src/RoadTrain/Extra/GenericDopClass.java @@ -0,0 +1,71 @@ +package RoadTrain.Extra; + +import java.util.ArrayList; +import java.util.Random; +import RoadTrain.Entities.*; +import RoadTrain.DrawingObjects.*; + +public class GenericDopClass { + + private ArrayList Trucks; + private ArrayList Wheels; + private int maxCountTrucks; + private int countTrucks; + private int maxCountWheels; + private int countWheels; + private Random random; + private int _pictureWidth; + private int _pictureHeight; + public GenericDopClass(int _maxCountTrucks, int _maxCountWheels, int pictureWidth, int pictureHeight){ + maxCountTrucks = _maxCountTrucks; + maxCountWheels = _maxCountWheels; + Trucks = new ArrayList(maxCountTrucks); + Wheels = new ArrayList(maxCountWheels); + countTrucks = 0; + countWheels = 0; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + random = new Random(); + } + + public boolean add(T train){ + if (train == null) + return false; + if (countTrucks > maxCountTrucks) + return false; + Trucks.add(countTrucks++, train); + return true; + } + + public boolean add(U wheel){ + if (wheel == null) + return false; + if (countWheels > maxCountWheels) + return false; + Wheels.add(countWheels++, wheel); + return true; + } + + public DrawingTruck getDrawingObject(){ + if (countTrucks == 0 || countWheels == 0) + return null; + if(Trucks.isEmpty()) { + return null; + } + + T truck = Trucks.get(random.nextInt(Trucks.size())); + U wheel = null; + + if(!Wheels.isEmpty()){ + wheel = Wheels.get(random.nextInt(Wheels.size())); + } + DrawingTruck drawingTruck; + if (truck instanceof EntityRoadTrain){ + drawingTruck = new DrawingRoadTrain((EntityRoadTrain)truck, wheel, _pictureWidth, _pictureHeight, random.nextInt(3)); + } + else{ + drawingTruck = new DrawingTruck(truck, wheel, _pictureWidth, _pictureHeight, random.nextInt(3)); + } + return drawingTruck; + } +} diff --git a/src/RoadTrain/FormGenericDopClass.java b/src/RoadTrain/FormGenericDopClass.java new file mode 100644 index 0000000..f5a7f3c --- /dev/null +++ b/src/RoadTrain/FormGenericDopClass.java @@ -0,0 +1,65 @@ +package RoadTrain; +import RoadTrain.DrawingObjects.*; +import RoadTrain.Entities.*; +import RoadTrain.Extra.*; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; + +public class FormGenericDopClass extends JFrame { + static int pictureBoxWidth = 980; + static int pictureBoxHeight = 560; + public DrawingTruck _drawingTruck; + private class Canvas extends JComponent{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingTruck == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingTruck.SetPosition(50, 50); + _drawingTruck.DrawTransport(g2d); + super.repaint(); + } + } + GenericDopClass genericDopClass; + public FormGenericDopClass(){ + _drawingTruck = null; + Canvas canv = new Canvas(); + setSize (1000, 600); + setLayout(null); + canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight); + + genericDopClass = new GenericDopClass<>(100, 100, pictureBoxWidth, pictureBoxHeight); + genericDopClass.add(new EntityTruck(100, 100, Color.BLUE)); + genericDopClass.add(new EntityTruck(100, 100, Color.RED)); + genericDopClass.add(new EntityTruck(100, 100, Color.GRAY)); + genericDopClass.add(new EntityRoadTrain(100, 100, Color.BLUE, Color.ORANGE, true, true)); + genericDopClass.add(new DrawingOneLineWheels()); + genericDopClass.add(new DrawingTwoLineWheels()); + + JButton createButton = new JButton("Создать"); + createButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + _drawingTruck = genericDopClass.getDrawingObject(); + canv.repaint(); + } + } + ); + createButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-20, 120, 20); + + add(canv); + add(createButton); + setVisible(true); + } +} \ No newline at end of file diff --git a/src/RoadTrain/FormTruckCollection.java b/src/RoadTrain/FormTruckCollection.java new file mode 100644 index 0000000..b6b520b --- /dev/null +++ b/src/RoadTrain/FormTruckCollection.java @@ -0,0 +1,125 @@ +package RoadTrain; + +import RoadTrain.MovementStraregy.*; +import RoadTrain.Generics.*; +import RoadTrain.DrawingObjects.*; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class FormTruckCollection { + private class Canvas extends JComponent { + public TrucksGenericCollection _truck; + public Canvas() { + } + public void paintComponent (Graphics g) { + super.paintComponent(g); + if (_truck.ShowTrucks() != null) { + g.drawImage(_truck.ShowTrucks(), 0, 10, this); + } + super.repaint(); + } + } + Canvas canv; + static int pictureBoxWidth = 700; + static int pictureBoxHeight = 480; + private TrucksGenericCollection _truck; + public void Draw(){ + canv.repaint(); + } + FormTruckCollection() { + canv = new Canvas(); + JFrame Frame = new JFrame ("TrucksCollecltion"); + _truck = new TrucksGenericCollection(pictureBoxWidth, pictureBoxHeight); + canv._truck = _truck; + + JButton ButtonAddTruck = new JButton("Добавить технику"); + + ButtonAddTruck.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + RoadTrainForm form = new RoadTrainForm(); + form.buttonSelectTruck.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (_truck.Add(form._drawingTruck) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + form.frame.dispose(); + } + } + ); + } + } + ); + + JTextField TextBoxNumber = new JTextField(); + JButton ButtonRemoveTruck = new JButton("Удалить технику"); + ButtonRemoveTruck.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { + return; + } + for (char it : TextBoxNumber.getText().toCharArray()) + if (it < '0' || it > '9') { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + if (TextBoxNumber.getText().length() == 0) { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + + int pos = Integer.parseInt(TextBoxNumber.getText()); + if (_truck.remove(pos) != null) { + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + } + } + ); + + JButton ButtonRefreshCollection = new JButton("Обновить коллекцию"); + ButtonRefreshCollection.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + Draw(); + } + } + ); + + JButton FormTruckGenerate = new JButton("Генерировать"); + FormTruckGenerate.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + FormGenericDopClass formGenericDopClass = new FormGenericDopClass(); + } + } + ); + + Frame.setSize (880, 520); + Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); + Frame.setLayout(null); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); + ButtonAddTruck.setBounds(pictureBoxWidth - 50, 10, 170, 30); + TextBoxNumber.setBounds(pictureBoxWidth - 50, 50, 170, 20); + ButtonRemoveTruck.setBounds(pictureBoxWidth - 50, 80, 170, 30); + ButtonRefreshCollection.setBounds(pictureBoxWidth - 50, 120, 170, 30); + FormTruckGenerate.setBounds(pictureBoxWidth - 50, 160, 170, 30); + Frame.add(canv); + Frame.add(ButtonAddTruck); + Frame.add(ButtonRemoveTruck); + Frame.add(ButtonRefreshCollection); + Frame.add(TextBoxNumber); + Frame.add(FormTruckGenerate); + Frame.setVisible(true); + } +} diff --git a/src/RoadTrain/Generics/SetGeneric.java b/src/RoadTrain/Generics/SetGeneric.java new file mode 100644 index 0000000..0e18662 --- /dev/null +++ b/src/RoadTrain/Generics/SetGeneric.java @@ -0,0 +1,63 @@ +package RoadTrain.Generics; + +public class SetGeneric { + // Массив объектов, которые храним + private Object[] _places; + + // Количество объектов в массиве + public int Count; + + // Конструктор + public SetGeneric(int count) { + _places = new Object[count]; + Count = _places.length; + } + + // Добавление объекта в набор + public int Insert(T truck) { + int i = 0; + for (; i < _places.length; i++) { + if (_places[i] == null) + break; + } + if (i == _places.length) + return -1; + for (; i > 0; i--) { + _places[i] = _places[i - 1]; + } + _places[i] = truck; + return i; + } + + // Добавление объекта в набор на конкретную позицию + public boolean Insert(T truck, int position) { + if (position < 0 || position >= _places.length) + return false; + for (; position < _places.length; position++) { + if (_places[position] == null) + break; + } + if (position == _places.length) + return false; + for (; position > 0; position--) { + _places[position] = _places[position - 1]; + } + _places[position] = truck; + return true; + } + + // Удаление объекта из набора с конкретной позиции + public boolean Remove(int position) { + if (position < 0 || position >= _places.length) + return false; + _places[position] = null; + return true; + } + + // Получение объекта из набора по позиции + public T Get(int position) { + if (position < 0 || position >= _places.length) + return null; + return (T) _places[position]; + } +} diff --git a/src/RoadTrain/Generics/TrucksGenericCollection.java b/src/RoadTrain/Generics/TrucksGenericCollection.java new file mode 100644 index 0000000..86776fd --- /dev/null +++ b/src/RoadTrain/Generics/TrucksGenericCollection.java @@ -0,0 +1,85 @@ +package RoadTrain.Generics; +import RoadTrain.DrawingObjects.*; +import RoadTrain.MovementStraregy.*; + +import java.awt.*; +import java.awt.image.BufferedImage; + +public class TrucksGenericCollection { + // Высота и Ширина окна прорисовки + private int _pictureWidth; + private int _pictureHeight; + + // Размер занимаемого объектом места (ширина и высота) + private int _placeSizeWidth = 180; + private int _placeSizeHeight = 90; + + // Набор объектов + private SetGeneric _collection; + + // Конструктор + public TrucksGenericCollection(int pictureWidth, int pictureHeight) { + int width = pictureWidth / _placeSizeWidth; + int height = pictureHeight / _placeSizeHeight; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + _collection = new SetGeneric(width * height); + } + + // Перегрузка оператора сложения + public int Add(T obj) { + if (obj == null) { + return -1; + } + return _collection.Insert(obj); + } + + // Перегрузка оператора вычитания + public T remove(int pos) { + T obj = _collection.Get(pos); + if (obj != null) { + _collection.Remove(pos); + } + return obj; + } + + // Получение объекта IMoveableObject + public U GetU(int pos) { + return (U) _collection.Get(pos).GetMoveableObject(); + } + + // Вывод всего набора объектов + public BufferedImage ShowTrucks() { + BufferedImage bitmap = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = bitmap.createGraphics(); + DrawBackground(g); + DrawObjects(g); + g.dispose(); + return bitmap; + } + + // Метод отрисовки фона + 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) { + for (int i = 0; i < _collection.Count; i++) { + T t = _collection.Get(i); + if (t != null) { + t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (t instanceof DrawingRoadTrain) + ((DrawingRoadTrain) t).DrawTransport((Graphics2D) g); + else + t.DrawTransport((Graphics2D) g); + } + } + } +} diff --git a/src/RoadTrain/Main.java b/src/RoadTrain/Main.java index 2882f49..30c5529 100644 --- a/src/RoadTrain/Main.java +++ b/src/RoadTrain/Main.java @@ -1,6 +1,7 @@ package RoadTrain; + public class Main { public static void main(String[] args){ - RoadTrainForm RoadTrainForm = new RoadTrainForm(); + FormTruckCollection formTruckCollection = new FormTruckCollection(); } } diff --git a/src/RoadTrain/RoadTrainForm.java b/src/RoadTrain/RoadTrainForm.java index c518169..270830c 100644 --- a/src/RoadTrain/RoadTrainForm.java +++ b/src/RoadTrain/RoadTrainForm.java @@ -1,25 +1,31 @@ package RoadTrain; -import javax.swing.*; -import java.awt.*; -import java.awt.Graphics; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.*; + import RoadTrain.DrawingObjects.*; import RoadTrain.MovementStraregy.*; +import javax.swing.*; +import java.awt.*; +import java.awt.Graphics; +import java.awt.event.ActionListener; +import java.util.*; + public class RoadTrainForm{ - private DrawingTruck _drawingTruck; + public DrawingTruck _drawingTruck; private AbstractStrategy _abstractStrategy; + public JButton buttonSelectTruck; Canvas canv; + public JFrame frame; public void Draw(){ canv.repaint(); } public RoadTrainForm(){ - JFrame frame = new JFrame("RoadTrain"); + frame = new JFrame("RoadTrain"); JButton buttonCreateTruck = new JButton("Создать грузовик"); buttonCreateTruck.setFocusPainted(false); buttonCreateTruck.setContentAreaFilled(false); + buttonSelectTruck = new JButton("Добавить грузовик"); + buttonSelectTruck.setFocusPainted(false); + buttonSelectTruck.setContentAreaFilled(false); JButton buttonCreateRoadTrain = new JButton("Создать очистительную машину"); buttonCreateRoadTrain.setFocusPainted(false); buttonCreateRoadTrain.setContentAreaFilled(false); @@ -54,10 +60,15 @@ public class RoadTrainForm{ buttonCreateTruck.addActionListener( e -> { Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + color = selectedColor; + } _drawingTruck = new DrawingTruck(random.nextInt(200) + 100, random.nextInt(2000) + 1000, - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3)); + color, this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3)); _drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90)); canv._drawingTruck = _drawingTruck; Draw(); @@ -66,9 +77,21 @@ public class RoadTrainForm{ buttonCreateRoadTrain.addActionListener( e -> { Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + color = selectedColor; + } + Color color2 = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + color2 = selectedColor; + } _drawingTruck = new DrawingRoadTrain(random.nextInt(200) + 100, random.nextInt(2000) + 1000, - new Color(random.nextInt(256)), new Color(random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), + color, color2, random.nextBoolean(), random.nextBoolean(), this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3)); _drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90)); canv._drawingTruck = _drawingTruck; @@ -141,6 +164,7 @@ public class RoadTrainForm{ buttonRight.setBounds(840, 420, 40, 40); comboBoxStrategy.setBounds(800,10,100,50); buttonStep.setBounds(800,80,100,40); + buttonSelectTruck.setBounds(740, 140, 150, 20); frame.add(canv); frame.add(buttonCreateTruck); frame.add(buttonCreateRoadTrain); @@ -150,6 +174,7 @@ public class RoadTrainForm{ frame.add(buttonRight); frame.add(comboBoxStrategy); frame.add(buttonStep); + frame.add(buttonSelectTruck); frame.setVisible(true); } class Canvas extends JComponent{