Готовая 3 усложненная лабораторная

This commit is contained in:
Даниил Путинцев 2023-11-24 23:29:20 +04:00
parent b6e9f62b9c
commit b208126772
10 changed files with 467 additions and 14 deletions

View File

@ -1,4 +1,5 @@
package RoadTrain; package RoadTrain;
public enum DirectionType { public enum DirectionType {
Up, Up,
Down, Down,

View File

@ -1,6 +1,7 @@
package RoadTrain.DrawingObjects; package RoadTrain.DrawingObjects;
import java.awt.*; import java.awt.*;
import RoadTrain.Entities.*; import RoadTrain.Entities.*;
import RoadTrain.Extra.IDrawingWheels;
public class DrawingRoadTrain extends DrawingTruck{ public class DrawingRoadTrain extends DrawingTruck{
@ -14,6 +15,11 @@ public class DrawingRoadTrain extends DrawingTruck{
additionalColor, waterContainer, sweepingBrush); 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 @Override
public void DrawTransport(Graphics g) { public void DrawTransport(Graphics g) {
if (!(entityTruck instanceof EntityRoadTrain RoadTrain)) { if (!(entityTruck instanceof EntityRoadTrain RoadTrain)) {

View File

@ -2,6 +2,7 @@ package RoadTrain.DrawingObjects;
import RoadTrain.DirectionType; import RoadTrain.DirectionType;
import RoadTrain.Entities.*; import RoadTrain.Entities.*;
import RoadTrain.Extra.*; import RoadTrain.Extra.*;
import RoadTrain.MovementStraregy.*;
import java.util.Random; import java.util.Random;
import java.awt.*; import java.awt.*;
@ -13,6 +14,7 @@ public class DrawingTruck {
private int _pictureHeight; private int _pictureHeight;
protected int _startPosX; protected int _startPosX;
protected int _startPosY; protected int _startPosY;
public IMoveableObject GetMoveableObject() { return new DrawingObjectTruck(this);}
public int GetPosX() { public int GetPosX() {
return _startPosX; return _startPosX;
} }
@ -28,8 +30,17 @@ public class DrawingTruck {
public int GetHeight() { public int GetHeight() {
return _truckHeight; return _truckHeight;
} }
private int _truckWidth = 80; protected int _truckWidth = 80;
private int _truckHeight = 70; 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) { public DrawingTruck(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) {
if (width < _truckWidth || height < _truckHeight) { if (width < _truckWidth || height < _truckHeight) {
return; return;

View File

@ -0,0 +1,71 @@
package RoadTrain.Extra;
import java.util.ArrayList;
import java.util.Random;
import RoadTrain.Entities.*;
import RoadTrain.DrawingObjects.*;
public class GenericDopClass<T extends EntityTruck, U extends IDrawingWheels> {
private ArrayList<T> Trucks;
private ArrayList<U> 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<T>(maxCountTrucks);
Wheels = new ArrayList<U>(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;
}
}

View File

@ -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<EntityTruck, IDrawingWheels> 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);
}
}

View File

@ -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<DrawingTruck, DrawingObjectTruck> _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<DrawingTruck, DrawingObjectTruck> _truck;
public void Draw(){
canv.repaint();
}
FormTruckCollection() {
canv = new Canvas();
JFrame Frame = new JFrame ("TrucksCollecltion");
_truck = new TrucksGenericCollection<DrawingTruck, DrawingObjectTruck>(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);
}
}

View File

@ -0,0 +1,63 @@
package RoadTrain.Generics;
public class SetGeneric<T extends Object> {
// Массив объектов, которые храним
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];
}
}

View File

@ -0,0 +1,85 @@
package RoadTrain.Generics;
import RoadTrain.DrawingObjects.*;
import RoadTrain.MovementStraregy.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class TrucksGenericCollection<T extends DrawingTruck, U extends IMoveableObject> {
// Высота и Ширина окна прорисовки
private int _pictureWidth;
private int _pictureHeight;
// Размер занимаемого объектом места (ширина и высота)
private int _placeSizeWidth = 180;
private int _placeSizeHeight = 90;
// Набор объектов
private SetGeneric<T> _collection;
// Конструктор
public TrucksGenericCollection(int pictureWidth, int pictureHeight) {
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new SetGeneric<T>(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);
}
}
}
}

View File

@ -1,6 +1,7 @@
package RoadTrain; package RoadTrain;
public class Main { public class Main {
public static void main(String[] args){ public static void main(String[] args){
RoadTrainForm RoadTrainForm = new RoadTrainForm(); FormTruckCollection formTruckCollection = new FormTruckCollection();
} }
} }

View File

@ -1,25 +1,31 @@
package RoadTrain; 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.DrawingObjects.*;
import RoadTrain.MovementStraregy.*; import RoadTrain.MovementStraregy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.util.*;
public class RoadTrainForm{ public class RoadTrainForm{
private DrawingTruck _drawingTruck; public DrawingTruck _drawingTruck;
private AbstractStrategy _abstractStrategy; private AbstractStrategy _abstractStrategy;
public JButton buttonSelectTruck;
Canvas canv; Canvas canv;
public JFrame frame;
public void Draw(){ public void Draw(){
canv.repaint(); canv.repaint();
} }
public RoadTrainForm(){ public RoadTrainForm(){
JFrame frame = new JFrame("RoadTrain"); frame = new JFrame("RoadTrain");
JButton buttonCreateTruck = new JButton("Создать грузовик"); JButton buttonCreateTruck = new JButton("Создать грузовик");
buttonCreateTruck.setFocusPainted(false); buttonCreateTruck.setFocusPainted(false);
buttonCreateTruck.setContentAreaFilled(false); buttonCreateTruck.setContentAreaFilled(false);
buttonSelectTruck = new JButton("Добавить грузовик");
buttonSelectTruck.setFocusPainted(false);
buttonSelectTruck.setContentAreaFilled(false);
JButton buttonCreateRoadTrain = new JButton("Создать очистительную машину"); JButton buttonCreateRoadTrain = new JButton("Создать очистительную машину");
buttonCreateRoadTrain.setFocusPainted(false); buttonCreateRoadTrain.setFocusPainted(false);
buttonCreateRoadTrain.setContentAreaFilled(false); buttonCreateRoadTrain.setContentAreaFilled(false);
@ -54,10 +60,15 @@ public class RoadTrainForm{
buttonCreateTruck.addActionListener( buttonCreateTruck.addActionListener(
e -> { e -> {
Random random = new Random(); 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, _drawingTruck = new DrawingTruck(random.nextInt(200) + 100,
random.nextInt(2000) + 1000, random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), color, this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3));
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3));
_drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90)); _drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90));
canv._drawingTruck = _drawingTruck; canv._drawingTruck = _drawingTruck;
Draw(); Draw();
@ -66,9 +77,21 @@ public class RoadTrainForm{
buttonCreateRoadTrain.addActionListener( buttonCreateRoadTrain.addActionListener(
e -> { e -> {
Random random = new Random(); 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, _drawingTruck = new DrawingRoadTrain(random.nextInt(200) + 100,
random.nextInt(2000) + 1000, 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)); this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3));
_drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90)); _drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90));
canv._drawingTruck = _drawingTruck; canv._drawingTruck = _drawingTruck;
@ -141,6 +164,7 @@ public class RoadTrainForm{
buttonRight.setBounds(840, 420, 40, 40); buttonRight.setBounds(840, 420, 40, 40);
comboBoxStrategy.setBounds(800,10,100,50); comboBoxStrategy.setBounds(800,10,100,50);
buttonStep.setBounds(800,80,100,40); buttonStep.setBounds(800,80,100,40);
buttonSelectTruck.setBounds(740, 140, 150, 20);
frame.add(canv); frame.add(canv);
frame.add(buttonCreateTruck); frame.add(buttonCreateTruck);
frame.add(buttonCreateRoadTrain); frame.add(buttonCreateRoadTrain);
@ -150,6 +174,7 @@ public class RoadTrainForm{
frame.add(buttonRight); frame.add(buttonRight);
frame.add(comboBoxStrategy); frame.add(comboBoxStrategy);
frame.add(buttonStep); frame.add(buttonStep);
frame.add(buttonSelectTruck);
frame.setVisible(true); frame.setVisible(true);
} }
class Canvas extends JComponent{ class Canvas extends JComponent{