PIbd-21. Putintsev D.M. Lab work 03. Hard #4

Closed
Danil wants to merge 2 commits from Lab3 into Lab2
12 changed files with 475 additions and 20 deletions

View File

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

View File

@ -1,6 +1,7 @@
package RoadTrain.DrawingObjects;
import java.awt.*;
import RoadTrain.Entities.*;
import RoadTrain.Extra.IDrawingWheels;
public class DrawingRoadTrain extends DrawingTruck{
@ -10,10 +11,15 @@ public class DrawingRoadTrain extends DrawingTruck{
super(speed, weight, bodyColor, width, height, wheelNumber);
if (entityTruck != null)
{
entityTruck = new EntityRoadTrain(speed, weight, bodyColor,
entityTruck = new EntityRoadTrain(speed, weight, bodyColor, wheelNumber,
additionalColor, waterContainer, sweepingBrush);
}
}
public DrawingRoadTrain(EntityRoadTrain truck, IDrawingWheels _wheelDrawing, int width, int height ){
super(truck, _wheelDrawing, width, height);
if (height < _truckHeight || width < _truckWidth)
return;
}
@Override
public void DrawTransport(Graphics g) {
if (!(entityTruck instanceof EntityRoadTrain RoadTrain)) {

View File

@ -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,15 +30,24 @@ 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){
if (height < _truckHeight || width < _truckWidth)
return;
_pictureWidth = width;
_pictureHeight = height;
entityTruck = truck;
drawingWheels = _wheelDrawing;
drawingWheels.SetWheelNumber(entityTruck.Numwheel);
}
public DrawingTruck(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) {
if (width < _truckWidth || height < _truckHeight) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
entityTruck = new EntityTruck(speed, weight, mainColor);
entityTruck = new EntityTruck(speed, weight, mainColor, wheelNumber);
Random rand = new Random();
drawingWheels = switch (rand.nextInt(0, 3)) {
case 0 -> new DrawingWheels();
@ -56,7 +67,7 @@ public class DrawingTruck {
_pictureHeight = height;
_truckWidth = truckWidth;
_truckHeight = truckHeight;
entityTruck = new EntityTruck(speed, weight, mainColor);
entityTruck = new EntityTruck(speed, weight, mainColor, wheelNumber);
Random rand = new Random();
drawingWheels = switch (rand.nextInt(0, 3)) {
case 0 -> new DrawingWheels();

View File

@ -12,9 +12,9 @@ public class EntityRoadTrain extends EntityTruck {
return SweepingBrush;
}
public EntityRoadTrain(int speed, double weight, Color bodyColor, Color
public EntityRoadTrain(int speed, double weight, Color bodyColor, int numwheel, Color
additionalColor, boolean waterContainer, boolean sweepingBrush) {
super(speed, weight, bodyColor);
super(speed, weight, bodyColor, numwheel);
AdditionalColor = additionalColor;
WaterContainer = waterContainer;
SweepingBrush = sweepingBrush;

View File

@ -18,14 +18,16 @@ public class EntityTruck {
public Color GetBodyColor() {
return BodyColor;
}
public int Numwheel;
public double GetStep() {
return (double) Speed * 100 / Weight;
}
public EntityTruck(int speed, double weight, Color bodyColor) {
public EntityTruck(int speed, double weight, Color bodyColor, int numwheel) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Numwheel = numwheel;
}
}

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);
}
else{
drawingTruck = new DrawingTruck(truck, wheel, _pictureWidth, _pictureHeight);
}
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, 1));
genericDopClass.add(new EntityTruck(100, 100, Color.RED, 2));
genericDopClass.add(new EntityTruck(100, 100, Color.GRAY, 1));
genericDopClass.add(new EntityRoadTrain(100, 100, Color.BLUE, 3, 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;
public class Main {
public static void main(String[] args){
RoadTrainForm RoadTrainForm = new RoadTrainForm();
FormTruckCollection formTruckCollection = new FormTruckCollection();
}
}

View File

@ -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{