усложнённая работа 4

This commit is contained in:
bekodeg 2023-12-12 15:21:05 +04:00
parent 1924451a63
commit 5b1b1f2284
9 changed files with 201 additions and 6 deletions

View File

@ -10,12 +10,14 @@ public class DrawningElectricLocomotive extends DrawningLocomotive {
Color bodyColor, Color additionalColor,
int typeWheels, int countWheels,
boolean horns, boolean battery,
int width, int height){
int width, int height) {
super(speed, weight, bodyColor, typeWheels, countWheels, width, height, 120, 70);
if (entityLocomotive != null){
entityLocomotive = new EntityElectricLocomotive(
speed, weight, bodyColor, additionalColor, horns, battery);
}
entityLocomotive = new EntityElectricLocomotive(
speed, weight, bodyColor, additionalColor, horns, battery);
}
public DrawningElectricLocomotive(EntityLocomotive entity, IDrawWheels drawingWheels, int width, int height) {
super(entity, drawingWheels, width, height);
}
@Override
public void DrawTransport(Graphics g) {

View File

@ -85,6 +85,12 @@ public class DrawningLocomotive {
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
public DrawningLocomotive(EntityLocomotive entity, IDrawWheels drawWheels, int width, int height) {
entityLocomotive = entity;
wheels = drawWheels;
_pictureWidth = width;
_pictureHeight = height;
}
public void SetPosition(int x, int y) {
x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth);

View File

@ -13,6 +13,7 @@ public class EntityElectricLocomotive extends EntityLocomotive {
Color bodyColor, Color additionalColor,
boolean horns, boolean battery) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Horns = horns;
Battery = battery;
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Forms.FormGenerateLocomotiveDop">
<grid id="27dc6" binding="PictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="378" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4e6e1" class="javax.swing.JButton" binding="generateLocomotiveButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Сгенерировать"/>
</properties>
</component>
<vspacer id="a5af5">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>

View File

@ -0,0 +1,79 @@
package Forms;
import DrawningObjects.*;
import Entities.EntityElectricLocomotive;
import Entities.EntityLocomotive;
import Generics.DopLocomotiveGenericCollection;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
public class FormGenerateLocomotiveDop extends JFrame {
private Random _random;
private DrawningLocomotive _drawingLocomotive;
private DopLocomotiveGenericCollection<EntityLocomotive, IDrawWheels> _genericDop;
private JButton generateLocomotiveButton;
private JPanel PictureBox;
public JPanel getPictureBox() {
return PictureBox;
}
public FormGenerateLocomotiveDop(){
_random = new Random();
_genericDop = new DopLocomotiveGenericCollection<>();
PictureBox.setPreferredSize(new Dimension(400, 400));
generateLocomotiveButton.addActionListener(this::generateLocomotive);
}
private void addRandomEntity(){
EntityLocomotive entityLocomotive;
if (_random.nextBoolean()){
entityLocomotive = new EntityLocomotive(
_random.nextInt(100, 300),
_random.nextInt(1000, 3000),
new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256))
);
} else {
entityLocomotive = new EntityElectricLocomotive(
_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)),
_random.nextBoolean(),
_random.nextBoolean()
);
}
_genericDop.add(entityLocomotive);
}
private void addRandomWheels(){
int choice = _random.nextInt(3);
IDrawWheels drawWheels;
if (choice == 0){
drawWheels = new DrawningWheels();
} else if (choice == 1){
drawWheels = new DrawningWheelsStar();
} else {
drawWheels = new DrawningWheelsPolygon();
}
drawWheels.setWheelsCount(_random.nextInt(2, 4));
_genericDop.add(drawWheels);
}
private void generateLocomotive(ActionEvent e){
addRandomEntity();
addRandomWheels();
_drawingLocomotive = _genericDop.GenerateLocomotive(PictureBox.getWidth(), PictureBox.getHeight());
_drawingLocomotive.SetPosition((PictureBox.getWidth() - _drawingLocomotive.GetWidth()) / 2,
(PictureBox.getHeight() - _drawingLocomotive.GetHeight()) / 2);
Draw();
}
public void Draw() {
if (_drawingLocomotive == null) {
return;
}
Graphics g = PictureBox.getGraphics();
PictureBox.paint(g);
_drawingLocomotive.DrawTransport(g);
}
}

View File

@ -12,9 +12,11 @@ import java.awt.event.ActionListener;
public class FormLocomotiveCollection extends JFrame {
private final LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive> _locomotives;
private Canvas canvas;
private FrameGenerateLocomotiveDop _formGenerateLocomotiveDop;
private JButton buttonAddLocomotive;
private JButton buttonRemoveLocomotive;
private JButton buttonRefreshCollection;
private JButton buttonOpenGenerateWindow;
private JTextField textBoxNumber;
private JPanel buttonBox;
@ -42,6 +44,9 @@ public class FormLocomotiveCollection extends JFrame {
buttonRefreshCollection = new JButton("Обновить колекцию");
buttonRefreshCollection.setMargin(new Insets(0, 0, 0, 0));
buttonOpenGenerateWindow = new JButton("Открыть окно генерации");
buttonOpenGenerateWindow.setMargin(new Insets(0, 0, 0, 0));
buttonBox = new JPanel(new GridLayout(0, 1, 0, 10));
JLabel labelBB = new JLabel("Инструменты");
@ -51,14 +56,17 @@ public class FormLocomotiveCollection extends JFrame {
buttonBox.add(buttonRemoveLocomotive);
buttonBox.add(buttonRefreshCollection);
buttonBox.setBounds(760, 3, 200, 220);
canvas.setBounds(10, 3, 740, 700);
buttonOpenGenerateWindow.setBounds(760, 600, 200, 40);
setSize(1000, 700);
setTitle("Колекция локомотивов");
setLayout(null);
add(buttonBox);
add(buttonOpenGenerateWindow);
add(canvas);
}
void InitializeLogic(){
@ -122,6 +130,17 @@ public class FormLocomotiveCollection extends JFrame {
repaint();
}
});
buttonOpenGenerateWindow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_formGenerateLocomotiveDop != null) {
_formGenerateLocomotiveDop.dispose();
}
_formGenerateLocomotiveDop = new FrameGenerateLocomotiveDop();
_formGenerateLocomotiveDop.setVisible(true);
}
});
}
public FormLocomotiveCollection(){
InitializeComponent();

View File

@ -0,0 +1,17 @@
package Forms;
import javax.swing.*;
public class FrameGenerateLocomotiveDop extends JFrame {
public FormGenerateLocomotiveDop formGenerateLocomotiveDop;
public FrameGenerateLocomotiveDop(){
super();
setTitle("Генерация локомотивов");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
formGenerateLocomotiveDop = new FormGenerateLocomotiveDop();
setContentPane(formGenerateLocomotiveDop.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
}
}

View File

@ -0,0 +1,45 @@
package Generics;
import DrawningObjects.DrawningElectricLocomotive;
import DrawningObjects.DrawningLocomotive;
import DrawningObjects.IDrawWheels;
import Entities.EntityElectricLocomotive;
import Entities.EntityLocomotive;
import java.util.ArrayList;
import java.util.Random;
public class DopLocomotiveGenericCollection<T extends EntityLocomotive, U extends IDrawWheels> {
private ArrayList<T> _entitiesStorage;
private ArrayList<U> _drawWheelsStorage;
public DopLocomotiveGenericCollection(){
_entitiesStorage = new ArrayList<>();
_drawWheelsStorage = new ArrayList<>();
}
public void add(T obj){
_entitiesStorage.add(obj);
}
public void add(U obj){
_drawWheelsStorage.add(obj);
}
public DrawningLocomotive GenerateLocomotive(int pictureWidth, int pictureHeight) {
Random random = new Random();
if (_entitiesStorage.isEmpty()) {
return null;
}
T entityLocomotive = _entitiesStorage.get(random.nextInt(_entitiesStorage.size()));
U drawingWheels = null;
if (!_drawWheelsStorage.isEmpty()) {
drawingWheels = _drawWheelsStorage.get(random.nextInt(_drawWheelsStorage.size()));
}
DrawningLocomotive drawingLocomotive;
if (entityLocomotive instanceof EntityElectricLocomotive) {
drawingLocomotive = new DrawningElectricLocomotive((EntityLocomotive) entityLocomotive, drawingWheels, pictureWidth, pictureHeight);
} else {
drawingLocomotive = new DrawningLocomotive(entityLocomotive, drawingWheels, pictureWidth, pictureHeight);
}
return drawingLocomotive;
}
}

View File

@ -27,7 +27,7 @@ public class SetGeneric<T extends DrawningLocomotive> {
for (int i = fillLineEnd; i > position; --i){
_places[i] = _places[i - 1];
}
_places[fillLineEnd] = obj;
_places[position] = obj;
return true;
}
boolean Insert(T obj){