diff --git a/.idea/.name b/.idea/.name index 002da1d..f5fff8a 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -Main.java \ No newline at end of file +DrawingEmptyWheels.java \ No newline at end of file diff --git a/src/DopClassParameters.java b/src/DopClassParameters.java new file mode 100644 index 0000000..eeb1ff2 --- /dev/null +++ b/src/DopClassParameters.java @@ -0,0 +1,62 @@ +import java.util.Random; + +public class DopClassParameters { + public T[] _entityUsta; + public U[] _idrawningWheels; + + int _ustaCount; + int _idrawningWheelsCount; + + private int _pictureWidth; + private int _pictureHeight; + + public DopClassParameters(int count, int width, int height) { + _entityUsta = (T[]) new EntityUsta[count]; + _idrawningWheels = (U[]) new IDrawingWheels[count]; + _pictureWidth = width; + _pictureHeight = height; + _ustaCount = 0; + _idrawningWheelsCount = 0; + } + + public void Add(T entityUstaObj) { + if (_ustaCount < _entityUsta.length) { + _entityUsta[_ustaCount] = entityUstaObj; + _ustaCount++; + } + } + + public void Add(U idrawingWheels) { + if (_idrawningWheelsCount < _idrawningWheels.length) { + _idrawningWheels[_idrawningWheelsCount] = idrawingWheels; + _idrawningWheelsCount++; + } + } + + public DrawingUsta RandomUsta(int pictureWidth, int pictureHeight) { + Random rnd = new Random(); + if (_entityUsta == null || _idrawningWheels == null) { + return null; + } + T entityUsta = _entityUsta[rnd.nextInt(_ustaCount)]; + U idrawingWheels = _idrawningWheels[rnd.nextInt(_idrawningWheelsCount)]; + + DrawingUsta drawUsta; + if (entityUsta instanceof EntityUstaBat) { + drawUsta = new DrawingUstaBat( + (EntityUstaBat) entityUsta, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } else { + drawUsta = new DrawingUsta( + entityUsta, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } + return drawUsta; + } +} diff --git a/src/DrawingEmptyWheels.java b/src/DrawingEmptyWheels.java index 427452d..2e5718b 100644 --- a/src/DrawingEmptyWheels.java +++ b/src/DrawingEmptyWheels.java @@ -10,7 +10,7 @@ public class DrawingEmptyWheels implements IDrawingWheels{ return; } } - this._wheelsCount = WheelsCount.Five; + this._wheelsCount = WheelsCount.Four; } @Override diff --git a/src/DrawingUsta.java b/src/DrawingUsta.java index 2c279fd..a375fd8 100644 --- a/src/DrawingUsta.java +++ b/src/DrawingUsta.java @@ -1,6 +1,6 @@ import java.awt.*; -import java.awt.geom.Path2D; import java.awt.geom.Arc2D; +import java.awt.geom.Path2D; import java.util.Random; public class DrawingUsta { @@ -62,6 +62,21 @@ public class DrawingUsta { _ustaHeight = ustaHeight; } + protected DrawingUsta(EntityUsta entityUsta, IDrawingWheels iDrawingWheels, + int width, int height){ + EntityUsta = entityUsta; + _drawingWheels = iDrawingWheels; + _pictureWidth = width; + _pictureHeight = height; + } + + protected DrawingUsta(EntityUsta entityUsta, IDrawingWheels iDrawingWheels, + int width, int height, int ustaWidth, int ustaHeight){ + this(entityUsta, iDrawingWheels, width, height); + _ustaWidth = ustaWidth; + _ustaHeight = ustaHeight; + } + public void SetPosition(int x, int y) { @@ -103,6 +118,9 @@ public class DrawingUsta { if (EntityUsta == null) { return; } + + + Color bodyColor = EntityUsta.BodyColor; Color blackBrush = Color.BLACK; @@ -128,7 +146,6 @@ public class DrawingUsta { g.draw(path); _drawingWheels.DrawWheels(g, blackBrush, _startPosX, _startPosY, 9,9); - g.setColor(bodyColor); g.fillRect(_startPosX + 45, _startPosY + 20, 40, 20); g.setColor(Color.BLACK); @@ -164,4 +181,8 @@ public class DrawingUsta { } return false; } + + public IMoveableObject GetMoveableObject() { + return new DrawingObjectUsta(this); + } } diff --git a/src/DrawingUstaBat.java b/src/DrawingUstaBat.java index d29a735..198bd63 100644 --- a/src/DrawingUstaBat.java +++ b/src/DrawingUstaBat.java @@ -11,6 +11,12 @@ public class DrawingUstaBat extends DrawingUsta { EntityUsta = new EntityUstaBat(speed, width, bodyColor, additionalColor, pushka, bodyKit); } } + + public DrawingUstaBat(EntityUstaBat entityUstaBat, IDrawingWheels iDrawingWheels, + int width, int height) + { + super(entityUstaBat,iDrawingWheels, width, height, 150, 50); + } @Override public void DrawTransport(Graphics2D g) { diff --git a/src/DrawingWheel.java b/src/DrawingWheel.java index 6ec2a06..69b8999 100644 --- a/src/DrawingWheel.java +++ b/src/DrawingWheel.java @@ -1,6 +1,4 @@ import java.awt.*; -import java.awt.geom.Arc2D; -import java.awt.geom.Path2D; public class DrawingWheel implements IDrawingWheels { private WheelsCount _wheelsCount; @@ -11,7 +9,7 @@ public class DrawingWheel implements IDrawingWheels { return; } } - this._wheelsCount = WheelsCount.Five; + this._wheelsCount = WheelsCount.Six; } @Override diff --git a/src/DrawingWheelsBlueCrom.java b/src/DrawingWheelsBlueCrom.java index 02e96ed..7d4a52a 100644 --- a/src/DrawingWheelsBlueCrom.java +++ b/src/DrawingWheelsBlueCrom.java @@ -10,7 +10,7 @@ public class DrawingWheelsBlueCrom implements IDrawingWheels{ return; } } - this._wheelsCount = WheelsCount.Five; + this._wheelsCount = WheelsCount.Six; } @Override diff --git a/src/FormDopClassParameters.form b/src/FormDopClassParameters.form new file mode 100644 index 0000000..64c45c2 --- /dev/null +++ b/src/FormDopClassParameters.form @@ -0,0 +1,38 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormDopClassParameters.java b/src/FormDopClassParameters.java new file mode 100644 index 0000000..c8f94fa --- /dev/null +++ b/src/FormDopClassParameters.java @@ -0,0 +1,85 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormDopClassParameters { + private JButton ButtonGenerationRandomUsta; + private JPanel pictureBoxGenerated; + Random rnd; + DrawingUsta drawingUsta; + DopClassParameters _dopClassParameters; + + public JPanel getPictureBoxGenerated() { + return pictureBoxGenerated; + } + + public FormDopClassParameters() { + pictureBoxGenerated.setPreferredSize(new Dimension(400, 300)); + _dopClassParameters = new DopClassParameters<>( + 10, + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + + ButtonGenerationRandomUsta.addActionListener(e -> { + AddEntityUsta(); + AddRandomWheels(); + + drawingUsta = _dopClassParameters.RandomUsta( + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + drawingUsta.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + Draw(); + }); + } + + public void AddEntityUsta() { + rnd = new Random(); + EntityUsta entityLocomotive; + Color color = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); + Color color2 = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); + if (rnd.nextBoolean()) { + entityLocomotive = new EntityUsta( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color + ); + } else { + entityLocomotive = new EntityUstaBat( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color, + color2, + rnd.nextBoolean(), + rnd.nextBoolean() + ); + } + _dopClassParameters.Add(entityLocomotive); + } + + public void AddRandomWheels() { + rnd = new Random(); + IDrawingWheels iDrawingWheels; + int wheelsChoice = rnd.nextInt(0, 3); + if (wheelsChoice == 0) { + iDrawingWheels = new DrawingWheel(); + } else if (wheelsChoice == 1) { + iDrawingWheels = new DrawingEmptyWheels(); + } else { + iDrawingWheels = new DrawingWheelsBlueCrom(); + } + iDrawingWheels.SetWheelsCount(rnd.nextInt(3, 7)); + _dopClassParameters.Add(iDrawingWheels); + } + + public void Draw() { + if (drawingUsta.EntityUsta == null) { + return; + } + Graphics g = pictureBoxGenerated.getGraphics(); + pictureBoxGenerated.paint(g); + drawingUsta.DrawTransport((Graphics2D) g); + } + +} diff --git a/src/FormUstaBat.form b/src/FormUstaBat.form index 327f8a8..acd5c60 100644 --- a/src/FormUstaBat.form +++ b/src/FormUstaBat.form @@ -1,6 +1,6 @@
- + @@ -15,12 +15,12 @@ - + - + @@ -28,7 +28,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -70,7 +70,7 @@ - + @@ -84,7 +84,7 @@ - + @@ -102,7 +102,7 @@ - + @@ -117,6 +117,14 @@ + + + + + + + + diff --git a/src/FormUstaBat.java b/src/FormUstaBat.java index 7723294..fea29b5 100644 --- a/src/FormUstaBat.java +++ b/src/FormUstaBat.java @@ -16,6 +16,10 @@ public class FormUstaBat { public JComboBox comboBoxStrategy; private JButton buttonStep; private JButton buttonCreateUsta; + public JButton ButtonSelectUsta; + public DrawingUsta SelectedUsta; + public boolean IsSelect = false; + public JPanel getPictureBox() { return pictureBox; } @@ -28,25 +32,27 @@ public class FormUstaBat { buttonCreateUsta.addActionListener(e -> { Random rnd = new Random(); - + Color color = JColorChooser.showDialog(null, "Цвет", null); _drawingUsta = new DrawingUsta(rnd.nextInt(100, 300), - rnd.nextInt(1000, 3000), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextInt(1000, 3000), color, pictureBox.getWidth(), pictureBox.getHeight()); - _drawingUsta.SetWheelsCount(rnd.nextInt(4, 7)); + _drawingUsta.SetWheelsCount(rnd.nextInt(3, 7)); _drawingUsta.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); Draw(); }); buttonCreateUstaBat.addActionListener(e -> { Random random = new Random(); + Color color = JColorChooser.showDialog(null, "Цвет", null); + Color addColor = JColorChooser.showDialog(null, "Цвет2", null); _drawingUsta = new DrawingUstaBat( random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + color, + addColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), @@ -58,6 +64,12 @@ public class FormUstaBat { Draw(); }); + ButtonSelectUsta.addActionListener(e->{ + SelectedUsta = _drawingUsta; + IsSelect = true; +// DialogResult = DialogResult.OK; + }); + buttonStep.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/src/FormUstaCollections.form b/src/FormUstaCollections.form new file mode 100644 index 0000000..8626716 --- /dev/null +++ b/src/FormUstaCollections.form @@ -0,0 +1,91 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormUstaCollections.java b/src/FormUstaCollections.java new file mode 100644 index 0000000..5bdcfb4 --- /dev/null +++ b/src/FormUstaCollections.java @@ -0,0 +1,84 @@ +import javax.swing.*; +import java.awt.*; + +//готовая лаба 3 + +public class FormUstaCollections { + FrameDopClassParameters frameDopClassParameters; + private JPanel MainPanel; + private JPanel pictureBoxCollections; + private JPanel Instruments; + private JButton ButtonAddUsta; + private JTextField textFieldNumber; + private JButton ButtonRefreshCollection; + private JButton ButtonRemoveUsta; + private JButton ButtonCreateRandomUsta; + public DrawingUsta usta; + UstaGenericCollection _usta; + + public JPanel getPictureBoxCollections() { + return MainPanel; + } + + public FormUstaCollections() { + _usta = new UstaGenericCollection<>(700, 430); + + + ButtonAddUsta.addActionListener(e -> { + FrameUstaBat frameElectricUsta = new FrameUstaBat(); + frameElectricUsta.setVisible(true); + frameElectricUsta._formUstaCollection.ButtonSelectUsta.addActionListener(e2 -> { + usta = frameElectricUsta._formUstaCollection._drawingUsta; + frameElectricUsta.dispose(); + if (usta != null) { + //проверяем, удалось ли нам загрузить объект + if (_usta.AddOverload(usta) != -1) { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); + Refresh(); + } else { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); + } + } + }); + }); + + ButtonCreateRandomUsta.addActionListener(e->{ + if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); + frameDopClassParameters = new FrameDopClassParameters(); + frameDopClassParameters.setVisible(true); + }); + + ButtonRemoveUsta.addActionListener(e -> { + try { + int pos = Integer.parseInt(textFieldNumber.getText()); + if (_usta.SubOverload(pos) != null) { + Refresh(); + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Не удалось удалить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + } catch (Exception ex) { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Неверное значение", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + }); + + ButtonRefreshCollection.addActionListener(e -> { + Refresh(); + }); + + } + public void Refresh() { + Graphics g = pictureBoxCollections.getGraphics(); + pictureBoxCollections.paint(g); + _usta.ShowLocomotives((Graphics2D) g); + } +} diff --git a/src/FrameDopClassParameters.java b/src/FrameDopClassParameters.java new file mode 100644 index 0000000..498005d --- /dev/null +++ b/src/FrameDopClassParameters.java @@ -0,0 +1,17 @@ +import javax.swing.*; + +public class FrameDopClassParameters extends JFrame { + public FormDopClassParameters _formDopClassParameters; + + public FrameDopClassParameters(){ + super(); + setTitle("Random usta"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + _formDopClassParameters = new FormDopClassParameters(); + setContentPane(_formDopClassParameters.getPictureBoxGenerated()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 200); + pack(); + setVisible(true); + } +} diff --git a/src/FrameUstaCollection.java b/src/FrameUstaCollection.java new file mode 100644 index 0000000..9aa0337 --- /dev/null +++ b/src/FrameUstaCollection.java @@ -0,0 +1,16 @@ +import javax.swing.*; + +public class FrameUstaCollection extends JFrame { + public FormUstaCollections _formUstaCollections; + public FrameUstaCollection(){ + super(); + setTitle("UstaCollection"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formUstaCollections = new FormUstaCollections(); + setContentPane(_formUstaCollections.getPictureBoxCollections()); + setDefaultLookAndFeelDecorated(false); + setLocation(400, 50); + pack(); + setVisible(true); + } +} diff --git a/src/IDrawingWheels.java b/src/IDrawingWheels.java index 2368042..02587c5 100644 --- a/src/IDrawingWheels.java +++ b/src/IDrawingWheels.java @@ -1,6 +1,4 @@ import java.awt.*; -import java.awt.geom.Arc2D; -import java.awt.geom.Path2D; public interface IDrawingWheels { void SetWheelsCount(int wheelsCount); @@ -56,7 +54,7 @@ public interface IDrawingWheels { g.fillOval(startPosX + 120, startPosY + 50, 25, 24); } - if (wheelsCount.count == wheelsCount.Six.count) { + if (wheelsCount.count >= wheelsCount.Six.count) { /* наверху гусеницы*/ diff --git a/src/Main.java b/src/Main.java index a153400..d92ad98 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - FrameUstaBat mainFrame = new FrameUstaBat(); + FrameUstaCollection mainFrame = new FrameUstaCollection(); } } diff --git a/src/SetGeneric.java b/src/SetGeneric.java new file mode 100644 index 0000000..dc4ee8c --- /dev/null +++ b/src/SetGeneric.java @@ -0,0 +1,61 @@ +public class SetGeneric{ + private T[] _places; + public int Count(){ + return _places.length; + } + public SetGeneric(int count) { + _places = (T[]) new DrawingUsta[count]; + } + + public int Insert(T loco) + { + return Insert(loco, 0); + } + + public int Insert(T loco, int position) + { + int NoEmpty = 0, temp = 0; + for (int i = position; i < Count(); i++) + { + if (_places[i] != null) NoEmpty++; + } + if (NoEmpty == Count() - position - 1) return -1; + + if (position < Count() && position >= 0) + { + for (int j = position; j < Count(); j++) + { + if (_places[j] == null) + { + temp = j; + break; + } + } + // shift right + for (int i = temp; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = loco; + return position; + } + return -1; + } + + public T Remove(int position) + { + if (position >= Count() || position < 0) + return null; + + T tmp = _places[position]; + _places[position] = null; + return tmp; + } + + public T Get(int position) + { + // TODO проверка позиции + if (position < 0 || position >= Count()) return null; + return _places[position]; + } +} diff --git a/src/UstaGenericCollection.java b/src/UstaGenericCollection.java new file mode 100644 index 0000000..dee3020 --- /dev/null +++ b/src/UstaGenericCollection.java @@ -0,0 +1,89 @@ +import java.awt.*; + +public class UstaGenericCollection +{ + //ширина/высота окна + private final int _pictureWidth; + private final int _pictureHeight; + //ширина/высота занимаемого места + private final int _placeSizeWidth = 210; + private final int _placeSizeHeight = 90; + + /// Набор объектов + private final SetGeneric _collection; + + public UstaGenericCollection(int picWidth, int picHeight) + { + // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... + // высчитываем размер массива для setgeneric + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width*height); + } + + /// Перегрузка оператора сложения + //да емае, почему в яве все по-другому?... + + public int AddOverload(T loco){ + if(loco == null){ + return -1; + } + return _collection.Insert(loco); + } + + public T SubOverload(int pos){ + return _collection.Remove(pos); + } + + // получение объекта imoveableObj + public U GetU(int pos) +{ + return (U)_collection.Get(pos).GetMoveableObject(); +} + + /// Вывод всего набора объектов + public void ShowLocomotives(Graphics2D gr) + { + DrawBackground(gr); + DrawObjects(gr); + } + private void DrawBackground(Graphics g) + { + Color blackColor = Color.BLACK; + g.setColor(blackColor); + 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(Graphics2D g) + { + int c = 11; + + for (int i = 0; i < _collection.Count(); i++) + { + if (i % 3 == 0 && i != 0) + { + c = c - 3; + + } + T type = _collection.Get(i); + if (type != null) + { + type.SetPosition( + (int)(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth + 3), + (c / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight + 10) + ); + type.DrawTransport(g); + } + } + } +}