Laba3 KozyrevSS GasolineTanker JAVA HARD

This commit is contained in:
Sergey Kozyrev 2023-11-04 14:33:13 +04:00
parent 5815f489b0
commit 264bf6c6af
10 changed files with 486 additions and 27 deletions

View File

@ -1,4 +1,3 @@
import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -6,14 +5,16 @@ import java.awt.event.ActionListener;
import java.util.Random; import java.util.Random;
class DrawGasoline extends JComponent { class DrawGasoline extends JComponent {
private DrawingTanker _drawingTanker; private DrawTanker _drawTanker;
private AbstractStrategy _abstractStrategy; private AbstractStrategy _abstractStrategy;
private DrawTanker SelectedCar;
public DrawTanker GetSelectedCar() {return SelectedCar;}
public void paintComponent(Graphics g) public void paintComponent(Graphics g)
{ {
super.paintComponent(g); super.paintComponent(g);
if (_drawingTanker == null) if (_drawTanker == null)
return; return;
_drawingTanker.DrawTransport(g); _drawTanker.DrawTransport(g);
super.repaint(); super.repaint();
} }
protected void CreateGasolineTankerButton_Click() protected void CreateGasolineTankerButton_Click()
@ -21,10 +22,17 @@ class DrawGasoline extends JComponent {
Random rnd = new Random(); Random rnd = new Random();
Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
_drawingTanker = new DrawGasolineTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), Color newColor = JColorChooser.showDialog(null, "Choose a color", null);
if (newColor != null)
addColor = newColor;
newColor = JColorChooser.showDialog(null, "Choose a color", null);
if (newColor != null)
bodyColor = newColor;
_drawTanker = new DrawGasolineTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000),
bodyColor, addColor, IntToBool(rnd.nextInt(0, 2)), IntToBool(rnd.nextInt(0, 2)), bodyColor, addColor, IntToBool(rnd.nextInt(0, 2)), IntToBool(rnd.nextInt(0, 2)),
IntToBool(rnd.nextInt(0, 2)), Frame.Width, Frame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); IntToBool(rnd.nextInt(0, 2)), BaseFrame.Width, BaseFrame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200));
_drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); _drawTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
SelectedCar = _drawTanker;
super.repaint(); super.repaint();
} }
@ -32,20 +40,23 @@ class DrawGasoline extends JComponent {
{ {
Random rnd = new Random(); Random rnd = new Random();
Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
_drawingTanker = new DrawingTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), bodyColor, Frame.Width, Frame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); Color newColor = JColorChooser.showDialog(null, "Choose a color", null);
Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); if (newColor != null)
_drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); bodyColor = newColor;
_drawTanker = new DrawTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), bodyColor, BaseFrame.Width, BaseFrame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200));
_drawTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
SelectedCar = _drawTanker;
super.repaint(); super.repaint();
} }
protected void ButtonMove_Click(String tag) protected void ButtonMove_Click(String tag)
{ {
if (_drawingTanker == null) if (_drawTanker == null)
return; return;
switch (tag) { switch (tag) {
case "U" -> _drawingTanker.MoveTransport(Direction.Up); case "U" -> _drawTanker.MoveTransport(Direction.Up);
case "D" -> _drawingTanker.MoveTransport(Direction.Down); case "D" -> _drawTanker.MoveTransport(Direction.Down);
case "L" -> _drawingTanker.MoveTransport(Direction.Left); case "L" -> _drawTanker.MoveTransport(Direction.Left);
case "R" -> _drawingTanker.MoveTransport(Direction.Right); case "R" -> _drawTanker.MoveTransport(Direction.Right);
} }
super.repaint(); super.repaint();
} }
@ -57,7 +68,7 @@ class DrawGasoline extends JComponent {
protected void ButtonStep_Click(JComboBox<String> JCB, int width, int height) protected void ButtonStep_Click(JComboBox<String> JCB, int width, int height)
{ {
if (_drawingTanker == null) if (_drawTanker == null)
return; return;
if (JCB.isEnabled()) if (JCB.isEnabled())
{ {
@ -70,7 +81,7 @@ class DrawGasoline extends JComponent {
}; };
if (_abstractStrategy == null) if (_abstractStrategy == null)
return; return;
_abstractStrategy.SetData(new DrawingObjectTanker(_drawingTanker), width, height); _abstractStrategy.SetData(_drawTanker.GetMoveableObject(), width, height);
JCB.setEnabled(false); JCB.setEnabled(false);
} }
if (_abstractStrategy == null) if (_abstractStrategy == null)
@ -85,8 +96,10 @@ class DrawGasoline extends JComponent {
} }
} }
class Frame extends JFrame { class BaseFrame extends JFrame {
public Frame()
public JButton buttonSelect;
public BaseFrame()
{ {
initUI(); initUI();
} }
@ -158,6 +171,11 @@ class Frame extends JFrame {
} }
}); });
buttonSelect = new JButton("Select tanker");
buttonSelect.setBounds(Width - 120, Height - 150, 80, 30);
buttonSelect.setLayout(null);
add(buttonSelect);
buttonUp.setIcon(new ImageIcon("Arrows/Up.png")); buttonUp.setIcon(new ImageIcon("Arrows/Up.png"));
buttonDown.setIcon(new ImageIcon("Arrows/Down.png")); buttonDown.setIcon(new ImageIcon("Arrows/Down.png"));
buttonLeft.setIcon(new ImageIcon("Arrows/Left.png")); buttonLeft.setIcon(new ImageIcon("Arrows/Left.png"));

View File

@ -0,0 +1,67 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class CarsGenericCollection <T extends DrawTanker, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 110;
private final int _placeSizeHeight = 80;
private final SetGeneric<T> _collection;
public CarsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int plus(T gasTanker)
{
return _collection.Insert(gasTanker);
}
public boolean minus(int position)
{
return _collection.Remove(position);
}
public U GetU(int pos)
{
return (U)_collection.Get(pos).GetMoveableObject();
}
public Image ShowCars()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = bmp.getGraphics();
DrawBackground(g);
DrawObjects(g);
return bmp;
}
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) {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count(); i++) {
DrawTanker tank = _collection.Get(i);
if (tank == null)
continue;
tank.SetPosition(i % width * _placeSizeWidth, (height - i / height - 1) * _placeSizeHeight);
tank.DrawTransport(g);
}
}
}

133
CollectionFrame.java Normal file
View File

@ -0,0 +1,133 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class CollectionFrame extends JComponent {
private final CarsGenericCollection<DrawTanker, DrawingObjectTanker> _tanks;
protected final int Width;
protected final int Height;
public CollectionFrame(int width, int height)
{
Width = width;
Height = height;
_tanks = new CarsGenericCollection<>(Width, Height);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(_tanks.ShowCars(), 0, 0, this);
super.repaint();
}
protected void ButtonAddTank_Click()
{
BaseFrame form = new BaseFrame();
form.setVisible(true);
form.buttonSelect.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_tanks.plus(form.Gasoline.GetSelectedCar()) != -1)
{
form.dispose();
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен");
}
else
{
form.dispose();
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось добавить объект");
}
}
}
);
}
protected void ButtonRemoveTank_Click(String text)
{
if (JOptionPane.showConfirmDialog(null, "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
{
if (_tanks.minus(Integer.parseInt(text)))
{
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
super.repaint();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
super.repaint();
}
protected void ButtonUpdate_Click() {super.repaint();}
}
class GarageFrame extends JFrame {
public GarageFrame()
{
initUI();
}
protected static final int Width = 1000;
protected static final int Height = 600;
CollectionFrame Collection;
private void initUI()
{
setSize(Width, Height);
setTitle("TankGasoline");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
JButton addTankerButton = new JButton("Add tanker");
addTankerButton.setLayout(null);
addTankerButton.setBounds(Width-200, Height-550, 200, 30);
add(addTankerButton);
addTankerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collection.ButtonAddTank_Click();
}
});
JTextField indexTankerField = new JTextField();
indexTankerField.setBounds(Width- 200, Height-500, 200, 30);
indexTankerField.setLayout(null);
add(indexTankerField);
JButton deleteTankerButton = new JButton("Delete tanker");
deleteTankerButton.setLayout(null);
deleteTankerButton.setBounds(Width-200, Height-450, 200, 30);
add(deleteTankerButton);
deleteTankerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collection.ButtonRemoveTank_Click(indexTankerField.getText());
}
});
JButton updateCollectionButton = new JButton("Update collection");
updateCollectionButton.setLayout(null);
updateCollectionButton.setBounds(Width-200, Height-400, 200, 30);
add(updateCollectionButton);
updateCollectionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collection.ButtonUpdate_Click();
}
});
Collection = new CollectionFrame(Width-200, Height);
Collection.setLayout(null);
Collection.setBounds(0, 0, Width-200, Height);
add(Collection);
}
}

74
ComboFrame.java Normal file
View File

@ -0,0 +1,74 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
class DrawRandomTanker extends JComponent {
private DrawTanker _drawTanker;
private final ComboGenericCollection<BaseTanker, IWheelDraw> GeneratorTanker;
public DrawRandomTanker() {
GeneratorTanker = new ComboGenericCollection<>(25, 1000, 600);
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (_drawTanker == null)
return;
_drawTanker.DrawTransport(g);
super.repaint();
}
protected void GenerateTanker_Click()
{
_drawTanker = GeneratorTanker.CreateDraw();
super.repaint();
}
}
class ComboFrame extends JFrame {
public ComboFrame()
{
initUI();
}
protected static final int Width = 1000;
protected static final int Height = 600;
DrawRandomTanker Tanker;
private void initUI()
{
setSize(Width, Height);
setTitle("TankGasoline");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
Tanker = new DrawRandomTanker();
Tanker.setBounds(0, 0, Width, Height);
add(Tanker);
JButton UpdateButton = new JButton("Update tanker");
UpdateButton.setLayout(null);
UpdateButton.setBounds(Width-200, Height- 200, 100, 100);
add(UpdateButton);
UpdateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Tanker.GenerateTanker_Click();
}
});
}
}

View File

@ -0,0 +1,96 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class ComboGenericCollection <T extends BaseTanker, U extends IWheelDraw> {
private final T[] _tankers;
private final U[] _wheels;
public int MaxSize() {return _tankers.length;}
private int CountTankers = 0;
private int CountWheels = 0;
private final Random random;
private final int Width;
private final int Height;
public ComboGenericCollection(int count, int width, int height)
{
_tankers = (T[]) new BaseTanker[count];
_wheels = (U[]) new IWheelDraw[count];
random = new Random();
Width = width;
Height = height;
}
public T GenerateTanker()
{
int mode = random.nextInt(0, 100) % 2;
if (mode == 0)
{
return (T) new BaseTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
}
else
{
return (T) new GasolineTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
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)),
IntToBool(random.nextInt(0, 100)),IntToBool(random.nextInt(0, 100)), IntToBool(random.nextInt(0, 100)));
}
}
public U GenerateWheel()
{
int mode = random.nextInt(0, 100) % 3;
U Wheels = (U) new DrawWheelSquare();
switch (mode)
{
case 0 -> {Wheels = (U) new DrawWheelCircle();}
case 1 -> {Wheels = (U) new DrawWheelClassic();}
case 2 -> {Wheels = (U) new DrawWheelSquare();}
}
int count = random.nextInt(0, 100);
Wheels.setWheelCount(count);
return Wheels;
}
public boolean Add(T tanker)
{
for (int i = 0; i < MaxSize(); i++)
{
if (_tankers[i] == null)
{
_tankers[i] = tanker;
CountTankers++;
return true;
}
}
return false;
}
public boolean Add(U wheel)
{
for (int i = 0; i < MaxSize(); i++)
{
if (_wheels[i] == null)
{
_wheels[i] = wheel;
CountWheels++;
return true;
}
}
return false;
}
private boolean IntToBool(int n) {return n % 2 == 0;}
public DrawTanker CreateDraw()
{
T tanker = _tankers[random.nextInt(0, CountTankers)];
if (tanker instanceof GasolineTanker) {
return new DrawGasolineTanker((GasolineTanker) tanker, Width, Height, _wheels[random.nextInt(0, CountWheels)]);
}
return new DrawTanker(tanker, Width, Height, _wheels[random.nextInt(0, CountWheels)]);
}
}

View File

@ -1,12 +1,18 @@
import java.awt.*; import java.awt.*;
public class DrawGasolineTanker extends DrawingTanker { public class DrawGasolineTanker extends DrawTanker {
public DrawGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount, int wheelMode) { public DrawGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount, int wheelMode) {
super(speed, weight, bodyColor, width, height, wheelCount, wheelMode); super(speed, weight, bodyColor, width, height, wheelCount, wheelMode);
if (GasolineTanker != null) { if (GasolineTanker != null) {
GasolineTanker = new GasolineTanker(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); GasolineTanker = new GasolineTanker(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
} }
} }
public DrawGasolineTanker(GasolineTanker tanker, int width, int height, IWheelDraw wheelMode)
{
super(tanker, width, height, wheelMode);
if (GasolineTanker != null)
GasolineTanker = tanker;
}
@Override @Override
public void DrawTransport(Graphics g) { public void DrawTransport(Graphics g) {
if (!(GasolineTanker instanceof GasolineTanker)) { if (!(GasolineTanker instanceof GasolineTanker)) {

View File

@ -1,6 +1,6 @@
import java.awt.*; import java.awt.*;
public class DrawingTanker { public class DrawTanker {
protected BaseTanker GasolineTanker; protected BaseTanker GasolineTanker;
public BaseTanker GetGasolineTanker() {return GasolineTanker;} public BaseTanker GetGasolineTanker() {return GasolineTanker;}
private IWheelDraw wheelsDrawing; private IWheelDraw wheelsDrawing;
@ -34,7 +34,7 @@ public class DrawingTanker {
} }
} }
public DrawingTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount, int wheelMode) public DrawTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount, int wheelMode)
{ {
_pictureHeight = height; _pictureHeight = height;
_pictureWidth = width; _pictureWidth = width;
@ -49,6 +49,14 @@ public class DrawingTanker {
wheelsDrawing.setWheelCount(wheelCount); wheelsDrawing.setWheelCount(wheelCount);
} }
public DrawTanker(BaseTanker tanker, int width, int height, IWheelDraw wheels)
{
GasolineTanker = tanker;
_pictureHeight = height;
_pictureWidth = width;
wheelsDrawing = wheels;
}
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
_startPosX = x; _startPosX = x;
@ -98,4 +106,6 @@ public class DrawingTanker {
wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d); wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d);
} }
public IMoveableObject GetMoveableObject() {return new DrawingObjectTanker(this);}
} }

View File

@ -1,9 +1,7 @@
import java.util.Optional;
public class DrawingObjectTanker implements IMoveableObject { public class DrawingObjectTanker implements IMoveableObject {
private final DrawingTanker _drawTanker; private final DrawTanker _drawTanker;
public DrawingObjectTanker(DrawingTanker drawTanker) { public DrawingObjectTanker(DrawTanker drawTanker) {
_drawTanker = drawTanker; _drawTanker = drawTanker;
} }

View File

@ -1,6 +1,7 @@
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Frame a = new Frame(); //GarageFrame a = new GarageFrame();
ComboFrame a = new ComboFrame();
a.setVisible(true); a.setVisible(true);
} }
} }

56
SetGeneric.java Normal file
View File

@ -0,0 +1,56 @@
public class SetGeneric <T extends Object>{
private final T[] _places;
public int Count() {return _places.length;}
public SetGeneric(int count)
{
_places = (T[]) new Object[count];
}
public int Insert(T tanker)
{
return Insert(tanker, 0);
}
public int Insert(T tanker, int position)
{
if (position < 0 || position >= Count())
return -1;
if (_places[position] == null)
{
_places[position] = tanker;
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] = tanker;
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];
}
}