From 25a6ddb3bfad7650aaa35eb155925086b1f150c0 Mon Sep 17 00:00:00 2001 From: ekallin Date: Mon, 6 Nov 2023 20:57:19 +0400 Subject: [PATCH] Repair Lab3 --- .../DopClassParameters.java | 65 ++++++++++++++ .../DrawingElectricLocomotive.java | 13 ++- .../DrawingLocomotive.java | 21 ++++- .../FormDopClassParameters.form | 38 ++++++++ .../FormDopClassParameters.java | 86 +++++++++++++++++++ .../FormElectricLocomotive.java | 1 - .../FormLocomotiveCollections.form | 12 ++- .../FormLocomotiveCollections.java | 18 +++- .../FrameDopClassParameters.java | 19 ++++ .../FrameElectricLocomotive.java | 4 +- .../FrameLocomotiveCollection.java | 3 +- .../LocomotiveGenericCollection.java | 3 - ProjectElectricLocomotive/SetGeneric.java | 4 +- 13 files changed, 265 insertions(+), 22 deletions(-) create mode 100644 ProjectElectricLocomotive/DopClassParameters.java create mode 100644 ProjectElectricLocomotive/FormDopClassParameters.form create mode 100644 ProjectElectricLocomotive/FormDopClassParameters.java create mode 100644 ProjectElectricLocomotive/FrameDopClassParameters.java diff --git a/ProjectElectricLocomotive/DopClassParameters.java b/ProjectElectricLocomotive/DopClassParameters.java new file mode 100644 index 0000000..23fb7e8 --- /dev/null +++ b/ProjectElectricLocomotive/DopClassParameters.java @@ -0,0 +1,65 @@ +package ProjectElectricLocomotive; + +import java.util.ArrayList; +import java.util.Random; + +public class DopClassParameters { + public T[] _entityLocomotive; + public U[] _idrawningWheels; + + int _locomotivesCount; + int _idrawningWheelsCount; + + private int _pictureWidth; + private int _pictureHeight; + + public DopClassParameters(int count, int width, int height) { + _entityLocomotive = (T[]) new EntityLocomotive[count]; + _idrawningWheels = (U[]) new IDrawingWheels[count]; + _pictureWidth = width; + _pictureHeight = height; + _locomotivesCount = 0; + _idrawningWheelsCount = 0; + } + + public void Add(T entityLocoObj) { + if (_locomotivesCount < _entityLocomotive.length) { + _entityLocomotive[_locomotivesCount] = entityLocoObj; + _locomotivesCount++; + } + } + + public void Add(U idrawingWheels) { + if (_idrawningWheelsCount < _idrawningWheels.length) { + _idrawningWheels[_idrawningWheelsCount] = idrawingWheels; + _idrawningWheelsCount++; + } + } + + public DrawingLocomotive RandomLocomotive(int pictureWidth, int pictureHeight) { + Random rnd = new Random(); + if (_entityLocomotive == null || _idrawningWheels == null) { + return null; + } + T entityLocomotive = _entityLocomotive[rnd.nextInt(_locomotivesCount)]; + U idrawingWheels = _idrawningWheels[rnd.nextInt(_idrawningWheelsCount)]; + + DrawingLocomotive drawLocomotive; + if (entityLocomotive instanceof EntityElectricLocomotive) { + drawLocomotive = new DrawingElectricLocomotive( + (EntityElectricLocomotive) entityLocomotive, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } else { + drawLocomotive = new DrawingLocomotive( + entityLocomotive, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } + return drawLocomotive; + } +} diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 004ca7e..19db7f5 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -1,6 +1,7 @@ package ProjectElectricLocomotive; import java.awt.*; public class DrawingElectricLocomotive extends DrawingLocomotive { + public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries, int width, int height) { @@ -10,16 +11,22 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); } } + + public DrawingElectricLocomotive(EntityElectricLocomotive entityElectricLocomotive, IDrawingWheels iDrawingWheels, + int width, int height) + { + super(entityElectricLocomotive,iDrawingWheels, width, height, 150, 50); + } @Override public void DrawTransport(Graphics g) { if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { - Color colorBlack = Color.BLACK; + Color addColor = electricLocomotive.AdditionalColor; if (electricLocomotive.Horns) { //horns - g.setColor(colorBlack); + g.setColor(addColor); g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); @@ -28,7 +35,7 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { } if (electricLocomotive.SeifBatteries) { - g.setColor(colorBlack); + g.setColor(addColor); g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); } super.DrawTransport(g); diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index c794f80..f301d5f 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -26,14 +26,14 @@ public class DrawingLocomotive { return _locoHeight; } - public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth) + public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int height) { - if (width < _locoWidth || heigth < _locoHeight) + if (width < _locoWidth || height < _locoHeight) { return; } _pictureWidth = width; - _pictureHeight = heigth; + _pictureHeight = height; EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); Random rnd = new Random(); @@ -58,6 +58,21 @@ public class DrawingLocomotive { _locoHeight = locoHeight; } + protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, + int width, int height){ + EntityLocomotive = entityLocomotive; + _drawingWheels = iDrawingWheels; + _pictureWidth = width; + _pictureHeight = height; + } + + protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, + int width, int height, int locoWidth, int locoHeight){ + this(entityLocomotive, iDrawingWheels, width, height); + _locoWidth = locoWidth; + _locoHeight = locoHeight; + } + public void SetWheelsCount(int wheelsCount){ _drawingWheels.SetWheelsCount(wheelsCount); } diff --git a/ProjectElectricLocomotive/FormDopClassParameters.form b/ProjectElectricLocomotive/FormDopClassParameters.form new file mode 100644 index 0000000..e0de2f6 --- /dev/null +++ b/ProjectElectricLocomotive/FormDopClassParameters.form @@ -0,0 +1,38 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectElectricLocomotive/FormDopClassParameters.java b/ProjectElectricLocomotive/FormDopClassParameters.java new file mode 100644 index 0000000..1c842d5 --- /dev/null +++ b/ProjectElectricLocomotive/FormDopClassParameters.java @@ -0,0 +1,86 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormDopClassParameters { + private JButton ButtonGenerationRandomLocomotive; + private JPanel pictureBoxGenerated; + Random rnd; + DrawingLocomotive drawingLocomotive; + DopClassParameters _dopClassParameters; + + public JPanel getPictureBoxGenerated() { + return pictureBoxGenerated; + } + + public FormDopClassParameters() { + pictureBoxGenerated.setPreferredSize(new Dimension(400, 300)); + _dopClassParameters = new DopClassParameters<>( + 10, + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + + ButtonGenerationRandomLocomotive.addActionListener(e -> { + AddEntityLocomotive(); + AddRandomWheels(); + + drawingLocomotive = _dopClassParameters.RandomLocomotive( + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + Draw(); + }); + } + + public void AddEntityLocomotive() { + rnd = new Random(); + EntityLocomotive entityLocomotive; + Color color = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); + if (rnd.nextBoolean()) { + entityLocomotive = new EntityLocomotive( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color + ); + } else { + entityLocomotive = new EntityElectricLocomotive( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color, + color, + 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(2, 5)); + _dopClassParameters.Add(iDrawingWheels); + } + + public void Draw() { + if (drawingLocomotive.EntityLocomotive == null) { + return; + } + Graphics g = pictureBoxGenerated.getGraphics(); + pictureBoxGenerated.paint(g); + drawingLocomotive.DrawTransport(g); + } + +} diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index fa27453..1b9ae8b 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -68,7 +68,6 @@ public class FormElectricLocomotive { ButtonSelectLocomotive.addActionListener(e->{ SelectedLocomotive = _drawingLocomotive; IsSelect = true; -// DialogResult = DialogResult.OK; }); buttonStep.addActionListener(new ActionListener() { diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index 28e0da3..b1bcaa2 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -21,7 +21,7 @@ - + @@ -39,7 +39,7 @@ - + @@ -76,6 +76,14 @@ + + + + + + + + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 9b3028c..6f879ae 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -3,7 +3,10 @@ package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; +//готовая лаба 3 + public class FormLocomotiveCollections { + FrameDopClassParameters frameDopClassParameters; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; @@ -11,9 +14,9 @@ public class FormLocomotiveCollections { private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; + private JButton ButtonCreateRandomLoco; public DrawingLocomotive loco; - FormElectricLocomotive _formElectricLocomotive; - private final LocomotiveGenericCollection _locomotives; + LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; @@ -41,7 +44,13 @@ public class FormLocomotiveCollections { }); }); - ButtonRemoveLocomotive.addActionListener(e->{ + ButtonCreateRandomLoco.addActionListener(e->{ + if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); + frameDopClassParameters = new FrameDopClassParameters(); + frameDopClassParameters.setVisible(true); + }); + + ButtonRemoveLocomotive.addActionListener(e -> { try { int pos = Integer.parseInt(textFieldNumber.getText()); if (_locomotives.SubOverload(pos) != null) { @@ -64,9 +73,10 @@ public class FormLocomotiveCollections { } }); - ButtonRefreshCollection.addActionListener(e->{ + ButtonRefreshCollection.addActionListener(e -> { Refresh(); }); + } public void Refresh() { Graphics g = pictureBoxCollections.getGraphics(); diff --git a/ProjectElectricLocomotive/FrameDopClassParameters.java b/ProjectElectricLocomotive/FrameDopClassParameters.java new file mode 100644 index 0000000..e5a06ac --- /dev/null +++ b/ProjectElectricLocomotive/FrameDopClassParameters.java @@ -0,0 +1,19 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class FrameDopClassParameters extends JFrame { + public FormDopClassParameters _formDopClassParameters; + + public FrameDopClassParameters(){ + super(); + setTitle("Рандомный локомотив"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + _formDopClassParameters = new FormDopClassParameters(); + setContentPane(_formDopClassParameters.getPictureBoxGenerated()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 200); + pack(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/FrameElectricLocomotive.java b/ProjectElectricLocomotive/FrameElectricLocomotive.java index abf9d6b..9344738 100644 --- a/ProjectElectricLocomotive/FrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/FrameElectricLocomotive.java @@ -6,8 +6,8 @@ public class FrameElectricLocomotive extends JFrame { public FormElectricLocomotive _formLocomotiveCollection; public FrameElectricLocomotive() { super(); - setTitle("ElectroLoco"); - setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("Электролокомотив"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); _formLocomotiveCollection = new FormElectricLocomotive(); setContentPane(_formLocomotiveCollection.getPictureBox()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/FrameLocomotiveCollection.java b/ProjectElectricLocomotive/FrameLocomotiveCollection.java index 7883880..189aba5 100644 --- a/ProjectElectricLocomotive/FrameLocomotiveCollection.java +++ b/ProjectElectricLocomotive/FrameLocomotiveCollection.java @@ -6,8 +6,7 @@ public class FrameLocomotiveCollection extends JFrame { public FormLocomotiveCollections _formLocomotiveCollections; public FrameLocomotiveCollection(){ super(); - setTitle("LocoCollection"); - setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("Коллекция");setDefaultCloseOperation(EXIT_ON_CLOSE); _formLocomotiveCollections = new FormLocomotiveCollections(); setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index 9abe2b2..cf1207c 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -10,14 +10,11 @@ public class LocomotiveGenericCollection _collection; public LocomotiveGenericCollection(int picWidth, int picHeight) { - // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... - // высчитываем размер массива для setgeneric int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth; diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java index 3d0e778..6aa1ad8 100644 --- a/ProjectElectricLocomotive/SetGeneric.java +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -3,7 +3,7 @@ package ProjectElectricLocomotive; public class SetGeneric{ private T[] _places; public int Count(){ - return _places.length; + return _places.length; } public SetGeneric(int count) { _places = (T[]) new DrawingLocomotive[count]; @@ -60,4 +60,4 @@ public class SetGeneric{ if (position < 0 || position >= Count()) return null; return _places[position]; } -} +} \ No newline at end of file