Лабораторная работа 3

This commit is contained in:
Esenia12 2024-05-12 17:20:10 +04:00
parent 2f785292e3
commit 00b64fdcdd
25 changed files with 458 additions and 51 deletions

View File

@ -1,6 +1,6 @@
package DrawingObjects;
import Entities.*;
package DumpTruck.DrawingObjects;
import DumpTruck.Entities.*;
import DumpTruck.Wheels.IDrawingWheels;
import java.awt.*;
public class DrawingDumpTruck extends DrawingTruck {
@ -12,7 +12,14 @@ public class DrawingDumpTruck extends DrawingTruck {
setEntityTruck(new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor));
}
}
public DrawingDumpTruck(EntityDumpTruck entityDumpTruck, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels)
{
super(entityDumpTruck, width, height, wheelNumber, iDrawingWheels);
if (getEntityTruck() != null)
{
setEntityTruck(entityDumpTruck);
}
}
@Override
public void DrawTransport(Graphics2D g2D) {
if (!(getEntityTruck() instanceof EntityDumpTruck dumpTruck)) {

View File

@ -1,12 +1,12 @@
package DrawingObjects;
package DumpTruck.DrawingObjects;
import java.awt.*;
import java.util.Random;
import Entities.*;
import DumpTruck.Entities.*;
import DumpTruck.DirectionType;
import Wheels.*;
import DumpTruck.Wheels.*;
import DumpTruck.MovementStrategy.*;
public class DrawingTruck {
private EntityTruck entityTruck;
@ -18,8 +18,7 @@ public class DrawingTruck {
this.entityTruck = entityTruck;
}
private IDrawingWheels drawingWheels;
private IDrawingWheels drawingWheels; /// объявление объекта от интерфейса
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
@ -42,8 +41,10 @@ public class DrawingTruck {
public int getHeight() {
return _truckHeight;
}
public DrawingTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber)
public IMoveableObject GetMoveableObject() {
return new DrawingObjectTruck(this);
}
public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int wheelNumber)
{
if (height < _truckHeight || width < _truckWidth) {
return;
@ -53,7 +54,7 @@ public class DrawingTruck {
entityTruck = new EntityTruck(speed, weight, bodyColor);
Random random = new Random();
drawingWheels = switch (random.nextInt(3)) {
case 0 -> new DrawingWheels();
case 0 -> new DrawingWheels();///инициализация
case 1 -> new DrawingWheelsStar();
case 2 -> new DrawingWheelsCircles();
default -> new DrawingWheels();
@ -78,6 +79,18 @@ public class DrawingTruck {
};
drawingWheels.setWheelNumber(wheelNumber);
}
public DrawingTruck(EntityTruck entityTruck, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels)
{
if (height < _truckHeight || width < _truckWidth) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
this.entityTruck = entityTruck;
Random random = new Random();
drawingWheels = iDrawingWheels;
drawingWheels.setWheelNumber(wheelNumber);
}
public void SetPosition(int x, int y)
{

View File

@ -1,4 +1,4 @@
package Entities;
package DumpTruck.Entities;
import java.awt.*;

View File

@ -1,4 +1,4 @@
package Entities;
package DumpTruck.Entities;
import java.awt.*;

View File

@ -0,0 +1,17 @@
package DumpTruck;
import javax.swing.*;
public class FrameDop extends JFrame {
private PictureBoxDop pictureBoxDop;
public FrameDop() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxDop = new PictureBoxDop();
add(pictureBoxDop);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

@ -1,8 +1,10 @@
package DumpTruck;
import javax.swing.*;
public class FrameDumpTruck extends JFrame {
private PictureBoxDumpTruck pictureBoxDumpTruck;
public PictureBoxDumpTruck pictureBoxDumpTruck;
public FrameDumpTruck() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

View File

@ -0,0 +1,20 @@
package DumpTruck;
import DumpTruck.PictureBoxCollection;
import javax.swing.*;
public class FrameTruckCollection extends JFrame {
private PictureBoxCollection pictureBoxCollection;
public FrameTruckCollection() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxCollection = new PictureBoxCollection();
add(pictureBoxCollection);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

@ -0,0 +1,61 @@
package DumpTruck.Generics;
import DumpTruck.Entities.*;
import DumpTruck.Wheels.*;
import DumpTruck.DrawingObjects.*;
import java.util.Random;
public class GenericDop<T extends EntityTruck, U extends IDrawingWheels> {
private T[] Trucks;
private U[] Wheels;
private int maxTruckAmount;
private int truckAmount;
private int maxWheelAmount ;
private int wheelAmount;
private Random random;
private int _pictureWidth;
private int _pictureHeight;
public GenericDop(int maxTruckAmount, int maxWheelAmount, int pictureWidth, int pictureHeight){
this.maxTruckAmount = maxTruckAmount;
this.maxWheelAmount = maxWheelAmount;
Trucks = (T[]) new EntityTruck[maxTruckAmount];
Wheels = (U[]) new IDrawingWheels[maxWheelAmount];
truckAmount = 0;
wheelAmount = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
random = new Random();
}
public boolean add(T truck){
if (truck == null || truckAmount > maxTruckAmount)
return false;
Trucks[truckAmount] = truck;
truckAmount++;
return true;
}
public boolean add(U wheel){
if (wheel == null || wheelAmount > maxWheelAmount)
return false;
Wheels[wheelAmount] = wheel;
wheelAmount++;
return true;
}
public DrawingTruck DrawingTruckDop(){
if (truckAmount == 0 || wheelAmount == 0)
return null;
T truck = Trucks[random.nextInt(truckAmount)];
DrawingTruck drawingTruck;
if (truck instanceof EntityDumpTruck){
drawingTruck = new DrawingDumpTruck((EntityDumpTruck)truck, _pictureWidth, _pictureHeight, random.nextInt(5), Wheels[random.nextInt(wheelAmount)]);
}
else{
drawingTruck = new DrawingTruck(truck, _pictureWidth, _pictureHeight, random.nextInt(5), Wheels[random.nextInt(wheelAmount)]);
}
return drawingTruck;
}
}

View File

@ -0,0 +1,54 @@
package DumpTruck.Generics;
public class SetGeneric<T extends Object> {
private Object[] _places;
public int Count() {return _places.length;}
public SetGeneric(int count) {
_places = new Object[count];
}
public int Insert(T truck) {
return Insert(truck, 0);
}
public int Insert(T truck, int position) {
if (position < 0 || position >= Count())
return -1;
if (_places[position] == null)
{
_places[position] = truck;
return position;
}
int index = -1;
for (int i = position; i < Count(); i++)
{
if (_places[i] == null)
{
index = i;
break;
}
}
if (index < 0) return -1;
for (int i = index; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = truck;
return position;
}
public boolean Remove(int position) {
if (position < 0 || position >= Count())
return false;
_places[position] = null;
return true;
}
public T Get(int position) {
if (position < 0 || position >= Count())
return null;
return (T) _places[position];
}
}

View File

@ -0,0 +1,70 @@
package DumpTruck.Generics;
import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
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 = 100;
private DumpTruck.Generics.SetGeneric<T> _collection;
public TrucksGenericCollection(int pictureWidth, int pictureHeight) {
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new DumpTruck.Generics.SetGeneric<T>(width * height);
}
public int Add(T obj) {
if (obj == null) {
return -1;
}
return _collection.Insert(obj);
}
public boolean remove(int pos) {
T obj = _collection.Get(pos);
if (obj != null) {
_collection.Remove(pos);
}
return false;
}
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 obj = _collection.Get(i);
if (obj != null)
{
obj.SetPosition(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, i / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
obj.DrawTransport((Graphics2D) g);
}
}
}
}

View File

@ -1,4 +1,4 @@
package MovementStrategy;
package DumpTruck.MovementStrategy;
import DumpTruck.DirectionType;

View File

@ -1,7 +1,7 @@
package MovementStrategy;
package DumpTruck.MovementStrategy;
import DumpTruck.DirectionType;
import DrawingObjects.DrawingTruck;
import DumpTruck.DrawingObjects.DrawingTruck;
public class DrawingObjectTruck implements IMoveableObject {

View File

@ -1,4 +1,4 @@
package MovementStrategy;
package DumpTruck.MovementStrategy;
import DumpTruck.DirectionType;
public interface IMoveableObject {

View File

@ -1,4 +1,4 @@
package MovementStrategy;
package DumpTruck.MovementStrategy;
public class MoveToBorder extends AbstractStrategy {

View File

@ -1,4 +1,4 @@
package MovementStrategy;
package DumpTruck.MovementStrategy;
public class MoveToCenter extends AbstractStrategy {

View File

@ -1,4 +1,4 @@
package MovementStrategy;
package DumpTruck.MovementStrategy;
public class ObjectParameters {

View File

@ -1,4 +1,4 @@
package MovementStrategy;
package DumpTruck.MovementStrategy;
public enum Status {
NotInit,

View File

@ -0,0 +1,92 @@
package DumpTruck;
import javax.swing.*;
import java.awt.*;
import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
public class PictureBoxCollection extends JPanel {
public DumpTruck.Generics.TrucksGenericCollection<DrawingTruck, DrawingObjectTruck> _trucks;
private JLabel labelTools;
private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop;
private JTextField textFieldNumber;
public PictureBoxCollection() {
setLayout(null);
setBounds(0, 0, 800, 450);
_trucks = new DumpTruck.Generics.TrucksGenericCollection<>(this.getWidth() - 200, this.getHeight());
labelTools = new JLabel("Инструменты");
labelTools.setBounds(660, 10, 150, 30);
add(labelTools);
buttonAddTruck = new JButton("Добавить грузовик");
buttonAddTruck.setFocusable(false);
buttonAddTruck.setBounds(620, 50, 150, 30);
buttonAddTruck.addActionListener(e -> {
FrameDumpTruck frameDumpTruck = new FrameDumpTruck();
frameDumpTruck.pictureBoxDumpTruck.buttonSelectTruck.addActionListener(e1 -> {
if (_trucks.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
frameDumpTruck.dispose();
repaint();
}
);
});
add(buttonAddTruck);
textFieldNumber = new JTextField();
textFieldNumber.setBounds(620, 100, 150, 30);
add(textFieldNumber);
buttonDeleteTruck = new JButton("Удалить грузовик");
buttonDeleteTruck.setFocusable(false);
buttonDeleteTruck.setBounds(620, 150, 150, 30);
buttonDeleteTruck.addActionListener(e -> {
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
for (char it : textFieldNumber.getText().toCharArray())
if (it < '0' || it > '9') {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (textFieldNumber.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
int pos = Integer.parseInt(textFieldNumber.getText());
if (_trucks.remove(pos)) {
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
repaint();
});
add(buttonDeleteTruck);
buttonRefreshCollection = new JButton("Обновить коллекцию");
buttonRefreshCollection.setFocusable(false);
buttonRefreshCollection.setBounds(620, 200, 150, 30);
buttonRefreshCollection.addActionListener(e -> repaint());
add(buttonRefreshCollection);
buttonShowDop = new JButton("Показать доп");
buttonShowDop.setFocusable(false);
buttonShowDop.setBounds(620, 250, 150, 30);
buttonShowDop.addActionListener(e -> new FrameDop());
add(buttonShowDop);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(_trucks.ShowTrucks(), 0, 0, null);
}
}

View File

@ -0,0 +1,50 @@
package DumpTruck;
import DumpTruck.DrawingObjects.DrawingTruck;
import DumpTruck.Entities.*;
import DumpTruck.Generics.*;
import DumpTruck.Wheels.*;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class PictureBoxDop extends JPanel {
GenericDop<EntityTruck, IDrawingWheels> genericDop;
private JButton buttonCreate;
public DrawingTruck drawingTruck;
public PictureBoxDop(){
setLayout(null);
setBounds(0, 0, 800, 450);
genericDop = new GenericDop<>(100, 100, this.getWidth(), this.getHeight());
genericDop.add(new EntityTruck(100, 100, Color.BLUE));
genericDop.add(new EntityTruck(100, 100, Color.RED));
genericDop.add(new EntityTruck(100, 100, Color.GREEN));
genericDop.add(new EntityDumpTruck(100, 100, Color.BLUE, true, true, Color.BLACK, Color.RED));
genericDop.add(new EntityDumpTruck(100, 100, Color.GREEN, true, true, Color.MAGENTA, Color.YELLOW));
genericDop.add(new DrawingWheels());
genericDop.add(new DrawingWheelsCircles());
genericDop.add(new DrawingWheelsStar());
buttonCreate = new JButton("Создать грузовик");
buttonCreate.setFocusable(false);
buttonCreate.setBounds(12, 415, 150, 30);
buttonCreate.addActionListener(e -> {
drawingTruck = genericDop.DrawingTruckDop();
repaint();
});
add(buttonCreate);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
if (drawingTruck == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingTruck.SetPosition(100, 100);
drawingTruck.DrawTransport(g2d);
}
}

View File

@ -1,13 +1,14 @@
package DumpTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
import DrawingObjects.*;
import DumpTruck.DirectionType;
import MovementStrategy.*;
import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
public class PictureBoxDumpTruck extends JPanel {
private DrawingTruck drawingTruck;
public DrawingTruck drawingTruck;
private AbstractStrategy abstractStrategy;
@ -23,6 +24,7 @@ public class PictureBoxDumpTruck extends JPanel {
private JButton buttonCreateTruck;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public JButton buttonSelectTruck;
public PictureBoxDumpTruck() {
setLayout(null);
@ -34,15 +36,15 @@ public class PictureBoxDumpTruck extends JPanel {
buttonCreateTruck.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt( 256), random.nextInt(256), random.nextInt(256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
drawingTruck = new DrawingTruck((random.nextInt((200-300)+300)),
random.nextInt((1000-3000)+ 3000),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(),
new Color(random.nextInt(256), random.nextInt( 256),
random.nextInt(256)),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt( 256)),
bodyColor,
this.getWidth(), this.getHeight(), random.nextInt(5));
drawingTruck.SetPosition(random.nextInt((-90)+ 100), random.nextInt((-90)+ 100));
repaint();
@ -55,20 +57,39 @@ public class PictureBoxDumpTruck extends JPanel {
buttonCreateDumpTruck.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
Color dumpBoxColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
dumpBoxColor = selectedColor;
}
Color tentColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
tentColor = selectedColor;
}
drawingTruck = new DrawingDumpTruck((random.nextInt((200-300)+300)),
random.nextInt((1000-3000)+ 3000),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(),
new Color(random.nextInt(256), random.nextInt( 256),
random.nextInt(256)),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt( 256)),
bodyColor,
random.nextBoolean(), true,
tentColor, dumpBoxColor,
this.getWidth(), this.getHeight(), random.nextInt(5));
drawingTruck.SetPosition(random.nextInt((-90)+ 100), random.nextInt((-90)+ 100));
repaint();
});
buttonSelectTruck = new JButton("Выбрать");
buttonSelectTruck.setFocusable(false);
buttonSelectTruck.setBounds(350, 415, 150, 30);
add(buttonSelectTruck);
ActionListener buttonMoveListener = e -> {
if (drawingTruck == null)
{
@ -97,7 +118,7 @@ public class PictureBoxDumpTruck extends JPanel {
buttonLeft.setName("buttonLeft");
buttonLeft.setFocusable(false);
buttonLeft.setPreferredSize(new Dimension(30, 30));
buttonLeft.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\left.jpg"));
buttonLeft.setIcon(new ImageIcon("H:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\left.jpg"));
buttonLeft.addActionListener(buttonMoveListener);
buttonLeft.setBounds(686, 408, 30, 30);
@ -107,7 +128,7 @@ public class PictureBoxDumpTruck extends JPanel {
buttonRight.setName("buttonRight");
buttonRight.setFocusable(false);
buttonRight.setPreferredSize(new Dimension(30, 30));
buttonRight.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\right.jpg"));
buttonRight.setIcon(new ImageIcon("H:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\right.jpg"));
buttonRight.addActionListener(buttonMoveListener);
buttonRight.setBounds(758, 408, 30, 30);
@ -117,7 +138,7 @@ public class PictureBoxDumpTruck extends JPanel {
buttonDown.setName("buttonDown");
buttonDown.setFocusable(false);
buttonDown.setPreferredSize(new Dimension(30, 30));
buttonDown.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\down.jpg"));
buttonDown.setIcon(new ImageIcon("H:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\down.jpg"));
buttonDown.addActionListener(buttonMoveListener);
buttonDown.setBounds(722, 408, 30, 30);
@ -127,7 +148,7 @@ public class PictureBoxDumpTruck extends JPanel {
buttonUp.setName("buttonUp");
buttonUp.setFocusable(false);
buttonUp.setPreferredSize(new Dimension(30, 30));
buttonUp.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\up.jpg"));
buttonUp.setIcon(new ImageIcon("H:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\up.jpg"));
buttonUp.addActionListener(buttonMoveListener);
buttonUp.setBounds(722, 372, 30, 30);

View File

@ -1,4 +1,4 @@
package Wheels;
package DumpTruck.Wheels;
import java.awt.*;

View File

@ -1,4 +1,4 @@
package Wheels;
package DumpTruck.Wheels;
import java.awt.*;

View File

@ -1,4 +1,4 @@
package Wheels;
package DumpTruck.Wheels;
import java.awt.*;

View File

@ -1,4 +1,4 @@
package Wheels;
package DumpTruck.Wheels;
import java.awt.*;

View File

@ -1,4 +1,4 @@
package Wheels;
package DumpTruck.Wheels;
public enum WheelNumber {
Two,