PIbd-21. Anisin R.S. Lab work 03 Hard #3

Closed
RuslanAnisin wants to merge 1 commits from lab3 into lab2
12 changed files with 431 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package DumpTruck.DrawingObjects;
import DumpTruck.Entities.*;
import DumpTruck.Wheels.IDrawingWheels;
import java.awt.*;
@ -12,6 +13,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) {

View File

@ -5,6 +5,7 @@ import java.util.Random;
import DumpTruck.Entities.*;
import DumpTruck.DirectionType;
import DumpTruck.MovementStrategy.*;
import DumpTruck.Wheels.*;
public class DrawingTruck {
@ -42,8 +43,11 @@ public class DrawingTruck {
public int getHeight() {
return _truckHeight;
}
public IMoveableObject GetMoveableObject() {
return new DrawingObjectTruck(this);
}
public DrawingTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber)
public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int wheelNumber)
{
if (height < _truckHeight || width < _truckWidth) {
return;
@ -78,6 +82,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

@ -0,0 +1,22 @@
package DumpTruck;
import DumpTruck.Entities.*;
import DumpTruck.Generics.*;
import DumpTruck.Wheels.*;
import javax.swing.*;
import java.awt.*;
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

@ -4,7 +4,7 @@ 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,17 @@
package DumpTruck;
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(2, 5), Wheels[random.nextInt(wheelAmount)]);
}
else{
drawingTruck = new DrawingTruck(truck, _pictureWidth, _pictureHeight, random.nextInt(2, 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 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 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

@ -2,6 +2,6 @@ package DumpTruck;
public class Main {
public static void main(String[] args) {
new FrameDumpTruck();
new FrameTruckCollection();
}
}

View File

@ -0,0 +1,94 @@
package DumpTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import DumpTruck.DrawingObjects.*;
import DumpTruck.Generics.*;
import DumpTruck.MovementStrategy.*;
public class PictureBoxCollection extends JPanel {
public 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 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

@ -8,7 +8,7 @@ import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
public class PictureBoxDumpTruck extends JPanel {
private DrawingTruck drawingTruck;
public DrawingTruck drawingTruck;
private AbstractStrategy abstractStrategy;
@ -24,6 +24,7 @@ public class PictureBoxDumpTruck extends JPanel {
private JButton buttonCreateTruck;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public JButton buttonSelectTruck;
public PictureBoxDumpTruck() {
setLayout(null);
@ -35,15 +36,15 @@ public class PictureBoxDumpTruck extends JPanel {
buttonCreateTruck.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
drawingTruck = new DrawingTruck((random.nextInt(200, 300)),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean(),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
bodyColor,
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
@ -56,20 +57,39 @@ public class PictureBoxDumpTruck extends JPanel {
buttonCreateDumpTruck.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
Color dumpBoxColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
dumpBoxColor = selectedColor;
}
Color tentColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
tentColor = selectedColor;
}
drawingTruck = new DrawingDumpTruck((random.nextInt(200, 300)),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean(),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
bodyColor,
random.nextBoolean(), true,
tentColor, dumpBoxColor,
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
});
buttonSelectTruck = new JButton("Выбрать");
buttonSelectTruck.setFocusable(false);
buttonSelectTruck.setBounds(350, 415, 150, 30);
add(buttonSelectTruck);
ActionListener buttonMoveListener = e -> {
if (drawingTruck == null)
{