Compare commits
11 Commits
main
...
Laba5Magic
Author | SHA1 | Date | |
---|---|---|---|
875e293169 | |||
88e0ccbe5e | |||
7181ff36c0 | |||
141994f533 | |||
e2ad2fda8d | |||
58ecedcf4e | |||
f7843bddbe | |||
4d7b7e95a9 | |||
264bf6c6af | |||
5815f489b0 | |||
c6fc3c0b37 |
69
AbstractStrategy.java
Normal file
69
AbstractStrategy.java
Normal file
@ -0,0 +1,69 @@
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _moveableObject;
|
||||
private Status _state = Status.NotInit;
|
||||
|
||||
private int FieldWidth;
|
||||
private void SetFieldWidth(int width) {FieldWidth = width;}
|
||||
protected int GetFieldWidth() {return FieldWidth;}
|
||||
private int FieldHeight;
|
||||
private void SetFieldHeight(int height) {FieldHeight = height;}
|
||||
protected int GetFieldHeight() {return FieldHeight;}
|
||||
|
||||
public Status GetStatus() {return _state;}
|
||||
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = Status.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
SetFieldWidth(width);
|
||||
SetFieldHeight(height);
|
||||
}
|
||||
|
||||
public void MakeStep()
|
||||
{
|
||||
if(_state != Status.InProgress)
|
||||
return;
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
protected boolean MoveLeft() {return MoveTo(Direction.Left); }
|
||||
protected boolean MoveRight() {return MoveTo(Direction.Right); }
|
||||
protected boolean MoveUp() {return MoveTo(Direction.Up); }
|
||||
protected boolean MoveDown() {return MoveTo(Direction.Down); }
|
||||
protected ObjectParameters GetObjectParameters() {return _moveableObject.GetObjectParameters(); }
|
||||
|
||||
protected Integer GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean IsTargetDestination();
|
||||
private boolean MoveTo(Direction direction)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
return false;
|
||||
if (!_moveableObject.CheckCanMove(direction))
|
||||
{
|
||||
_moveableObject.MoveObject(direction);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
BIN
Arrows/Down.png
Normal file
BIN
Arrows/Down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 810 B |
BIN
Arrows/Left.png
Normal file
BIN
Arrows/Left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 802 B |
BIN
Arrows/Right.png
Normal file
BIN
Arrows/Right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 817 B |
BIN
Arrows/Up.png
Normal file
BIN
Arrows/Up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 821 B |
313
BaseFrame.java
Normal file
313
BaseFrame.java
Normal file
@ -0,0 +1,313 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
class DrawGasoline extends JComponent {
|
||||
private DrawTanker _drawTanker;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
private DrawTanker SelectedCar;
|
||||
public DrawTanker GetSelectedCar() {return SelectedCar;}
|
||||
protected void SetTanker(DrawTanker tanker) {_drawTanker = tanker;}
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
if (_drawTanker == null)
|
||||
return;
|
||||
_drawTanker.DrawTransport(g);
|
||||
super.repaint();
|
||||
}
|
||||
protected void CreateGasolineTankerButton_Click()
|
||||
{
|
||||
Random rnd = new Random();
|
||||
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 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)),
|
||||
IntToBool(rnd.nextInt(0, 2)), 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();
|
||||
}
|
||||
|
||||
protected void CreateBaseTankerButton_Click()
|
||||
{
|
||||
Random rnd = new Random();
|
||||
Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
Color newColor = JColorChooser.showDialog(null, "Choose a color", null);
|
||||
if (newColor != null)
|
||||
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();
|
||||
}
|
||||
protected void ButtonMove_Click(String tag)
|
||||
{
|
||||
if (_drawTanker == null)
|
||||
return;
|
||||
switch (tag) {
|
||||
case "U" -> _drawTanker.MoveTransport(Direction.Up);
|
||||
case "D" -> _drawTanker.MoveTransport(Direction.Down);
|
||||
case "L" -> _drawTanker.MoveTransport(Direction.Left);
|
||||
case "R" -> _drawTanker.MoveTransport(Direction.Right);
|
||||
}
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
private boolean IntToBool(int n)
|
||||
{
|
||||
return n > 0;
|
||||
}
|
||||
|
||||
protected void ButtonStep_Click(JComboBox<String> JCB, int width, int height)
|
||||
{
|
||||
if (_drawTanker == null)
|
||||
return;
|
||||
if (JCB.isEnabled())
|
||||
{
|
||||
String strat = String.valueOf(JCB.getSelectedItem());
|
||||
switch (strat)
|
||||
{
|
||||
case "0" -> {_abstractStrategy = new MoveToCenter();}
|
||||
case "1" -> {_abstractStrategy = new MoveToBorder();}
|
||||
default -> {_abstractStrategy = null;}
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
return;
|
||||
_abstractStrategy.SetData(_drawTanker.GetMoveableObject(), width, height);
|
||||
JCB.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
return;
|
||||
_abstractStrategy.MakeStep();
|
||||
super.repaint();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
JCB.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BaseFrame extends JFrame {
|
||||
|
||||
public JButton buttonSelect;
|
||||
public BaseFrame()
|
||||
{
|
||||
initUI();
|
||||
}
|
||||
|
||||
public BaseFrame(DrawTanker gas)
|
||||
{
|
||||
initUIFrom();
|
||||
Gasoline.SetTanker(gas);
|
||||
}
|
||||
protected static final int Width = 1000;
|
||||
protected static final int Height = 600;
|
||||
DrawGasoline Gasoline;
|
||||
private void initUI()
|
||||
{
|
||||
setSize(Width, Height);
|
||||
setTitle("TankGasoline");
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setResizable(false);
|
||||
Gasoline = new DrawGasoline();
|
||||
Gasoline.setBounds(0, 0, Width, Height);
|
||||
add(Gasoline);
|
||||
|
||||
MoveAL moving = new MoveAL();
|
||||
|
||||
JButton buttonUp = new JButton();
|
||||
buttonUp.setActionCommand("U");
|
||||
buttonUp.setBounds(Width - 90, Height - 120,30,30);
|
||||
buttonUp.setLayout(null);
|
||||
add(buttonUp);
|
||||
buttonUp.addActionListener(moving);
|
||||
|
||||
JButton buttonRight = new JButton();
|
||||
buttonRight.setActionCommand("R");
|
||||
buttonRight.setBounds(Width - 60, Height - 90,30,30);
|
||||
buttonRight.setLayout(null);
|
||||
add(buttonRight);
|
||||
buttonRight.addActionListener(moving);
|
||||
|
||||
JButton buttonLeft = new JButton();
|
||||
buttonLeft.setActionCommand("L");
|
||||
buttonLeft.setBounds(Width - 120, Height - 90,30,30);
|
||||
buttonLeft.setLayout(null);
|
||||
add(buttonLeft);
|
||||
buttonLeft.addActionListener(moving);
|
||||
|
||||
JButton buttonDown = new JButton();
|
||||
buttonDown.setActionCommand("D");
|
||||
buttonDown.setBounds(Width - 90, Height - 90,30,30);
|
||||
buttonDown.setLayout(null);
|
||||
add(buttonDown);
|
||||
buttonDown.addActionListener(moving);
|
||||
|
||||
String[] items = {
|
||||
"0",
|
||||
"1"
|
||||
};
|
||||
JComboBox<String> strategy = new JComboBox<String>(items);
|
||||
|
||||
strategy.setBounds(Width - 100, 100, 80, 20);
|
||||
strategy.setLayout(null);
|
||||
strategy.setEnabled(true);
|
||||
add(strategy);
|
||||
|
||||
JButton buttonStrategy = new JButton();
|
||||
buttonStrategy.setBounds(Width - 60, 120, 40, 20);
|
||||
buttonStrategy.setLayout(null);
|
||||
add(buttonStrategy);
|
||||
buttonStrategy.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Gasoline.ButtonStep_Click(strategy, Width, Height);
|
||||
}
|
||||
});
|
||||
|
||||
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"));
|
||||
buttonDown.setIcon(new ImageIcon("Arrows/Down.png"));
|
||||
buttonLeft.setIcon(new ImageIcon("Arrows/Left.png"));
|
||||
buttonRight.setIcon(new ImageIcon("Arrows/Right.png"));
|
||||
|
||||
JButton buttonCreateTanker = new JButton("Create Base Tanker");
|
||||
buttonCreateTanker.setBounds(60, Height - 120, 200, 50);
|
||||
buttonCreateTanker.setLayout(null);
|
||||
add(buttonCreateTanker);
|
||||
buttonCreateTanker.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Gasoline.CreateBaseTankerButton_Click();
|
||||
}
|
||||
});
|
||||
JButton buttonCreateGasolineTanker = new JButton("Create Update Tanker");
|
||||
buttonCreateGasolineTanker.setBounds(260, Height - 120, 200, 50);
|
||||
buttonCreateGasolineTanker.setLayout(null);
|
||||
add(buttonCreateGasolineTanker);
|
||||
buttonCreateGasolineTanker.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Gasoline.CreateGasolineTankerButton_Click();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
private void initUIFrom()
|
||||
{
|
||||
setSize(Width, Height);
|
||||
setTitle("TankGasoline");
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(null);
|
||||
setResizable(false);
|
||||
Gasoline = new DrawGasoline();
|
||||
Gasoline.setBounds(0, 0, Width, Height);
|
||||
add(Gasoline);
|
||||
|
||||
MoveAL moving = new MoveAL();
|
||||
|
||||
JButton buttonUp = new JButton();
|
||||
buttonUp.setActionCommand("U");
|
||||
buttonUp.setBounds(Width - 90, Height - 120,30,30);
|
||||
buttonUp.setLayout(null);
|
||||
add(buttonUp);
|
||||
buttonUp.addActionListener(moving);
|
||||
|
||||
JButton buttonRight = new JButton();
|
||||
buttonRight.setActionCommand("R");
|
||||
buttonRight.setBounds(Width - 60, Height - 90,30,30);
|
||||
buttonRight.setLayout(null);
|
||||
add(buttonRight);
|
||||
buttonRight.addActionListener(moving);
|
||||
|
||||
JButton buttonLeft = new JButton();
|
||||
buttonLeft.setActionCommand("L");
|
||||
buttonLeft.setBounds(Width - 120, Height - 90,30,30);
|
||||
buttonLeft.setLayout(null);
|
||||
add(buttonLeft);
|
||||
buttonLeft.addActionListener(moving);
|
||||
|
||||
JButton buttonDown = new JButton();
|
||||
buttonDown.setActionCommand("D");
|
||||
buttonDown.setBounds(Width - 90, Height - 90,30,30);
|
||||
buttonDown.setLayout(null);
|
||||
add(buttonDown);
|
||||
buttonDown.addActionListener(moving);
|
||||
|
||||
String[] items = {
|
||||
"0",
|
||||
"1"
|
||||
};
|
||||
JComboBox<String> strategy = new JComboBox<String>(items);
|
||||
|
||||
strategy.setBounds(Width - 100, 100, 80, 20);
|
||||
strategy.setLayout(null);
|
||||
strategy.setEnabled(true);
|
||||
add(strategy);
|
||||
|
||||
JButton buttonStrategy = new JButton();
|
||||
buttonStrategy.setBounds(Width - 60, 120, 40, 20);
|
||||
buttonStrategy.setLayout(null);
|
||||
add(buttonStrategy);
|
||||
buttonStrategy.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Gasoline.ButtonStep_Click(strategy, Width, Height);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
buttonUp.setIcon(new ImageIcon("Arrows/Up.png"));
|
||||
buttonDown.setIcon(new ImageIcon("Arrows/Down.png"));
|
||||
buttonLeft.setIcon(new ImageIcon("Arrows/Left.png"));
|
||||
buttonRight.setIcon(new ImageIcon("Arrows/Right.png"));
|
||||
|
||||
JButton buttonCreateTanker = new JButton("Create Base Tanker");
|
||||
buttonCreateTanker.setBounds(60, Height - 120, 200, 50);
|
||||
buttonCreateTanker.setLayout(null);
|
||||
add(buttonCreateTanker);
|
||||
buttonCreateTanker.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Gasoline.CreateBaseTankerButton_Click();
|
||||
}
|
||||
});
|
||||
JButton buttonCreateGasolineTanker = new JButton("Create Update Tanker");
|
||||
buttonCreateGasolineTanker.setBounds(260, Height - 120, 200, 50);
|
||||
buttonCreateGasolineTanker.setLayout(null);
|
||||
add(buttonCreateGasolineTanker);
|
||||
buttonCreateGasolineTanker.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Gasoline.CreateGasolineTankerButton_Click();
|
||||
}
|
||||
});
|
||||
}
|
||||
private void SetTanker(DrawTanker tanker)
|
||||
{
|
||||
Gasoline.SetTanker(tanker);
|
||||
}
|
||||
public class MoveAL implements ActionListener {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Gasoline.ButtonMove_Click(e.getActionCommand());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
39
BaseTanker.java
Normal file
39
BaseTanker.java
Normal file
@ -0,0 +1,39 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class BaseTanker {
|
||||
private int Speed;
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
private void setSpeed(int speed)
|
||||
{
|
||||
Speed = speed;
|
||||
}
|
||||
private double Weight;
|
||||
public double getWeight()
|
||||
{
|
||||
return Weight;
|
||||
}
|
||||
private void setWeight(double weight)
|
||||
{
|
||||
Weight = weight;
|
||||
}
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor()
|
||||
{
|
||||
return BodyColor;
|
||||
}
|
||||
public void setBodyColor(Color bodyColor)
|
||||
{
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
public double Step;
|
||||
|
||||
public BaseTanker(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
Step = (double)Speed * 100 / Weight;
|
||||
}
|
||||
}
|
67
CarsGenericCollection.java
Normal file
67
CarsGenericCollection.java
Normal 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 T 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
46
CarsGenericStorage.java
Normal file
46
CarsGenericStorage.java
Normal file
@ -0,0 +1,46 @@
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CarsGenericStorage {
|
||||
final HashMap<String, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> _carStorages;
|
||||
public ArrayList<String> Keys()
|
||||
{
|
||||
return new ArrayList<>(_carStorages.keySet());
|
||||
}
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
public CarsGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_pictureHeight = pictureHeight;
|
||||
_pictureWidth = pictureWidth;
|
||||
_carStorages = new HashMap<>();
|
||||
}
|
||||
public void AddSet(String name)
|
||||
{
|
||||
if (Keys().contains(name))
|
||||
return;
|
||||
_carStorages.put(name, new CarsGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
public void DelSet(String name)
|
||||
{
|
||||
if (!Keys().contains(name))
|
||||
return;
|
||||
_carStorages.remove(name);
|
||||
}
|
||||
|
||||
public CarsGenericCollection<DrawTanker, DrawingObjectTanker> get(String ind)
|
||||
{
|
||||
if (Keys().contains(ind))
|
||||
return _carStorages.get(ind);
|
||||
return null;
|
||||
}
|
||||
public DrawingObjectTanker get(String name, int ind)
|
||||
{
|
||||
if (!Keys().contains(name))
|
||||
return null;
|
||||
return _carStorages.get(name).GetU(ind);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
282
CollectionFrame.java
Normal file
282
CollectionFrame.java
Normal file
@ -0,0 +1,282 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.CropImageFilter;
|
||||
import java.util.LinkedList;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
|
||||
class CollectionFrame extends JComponent {
|
||||
private final CarsGenericStorage _tanksStorage;
|
||||
private final GarageFrame frame;
|
||||
private Queue<DrawTanker> removedTankers;
|
||||
protected final int Width;
|
||||
protected final int Height;
|
||||
public CollectionFrame(int width, int height, GarageFrame fr)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
_tanksStorage = new CarsGenericStorage(Width, Height);
|
||||
frame = fr;
|
||||
removedTankers = new LinkedList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (frame.listStorage.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
if (obj.ShowCars() == null)
|
||||
return;
|
||||
g.drawImage(obj.ShowCars(), 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
protected void ReloadObjects()
|
||||
{
|
||||
int index = frame.listStorage.getSelectedIndex();
|
||||
frame.listModel.clear();
|
||||
for (String key : _tanksStorage.Keys())
|
||||
{
|
||||
frame.listModel.addElement(key);
|
||||
}
|
||||
if (!frame.listModel.isEmpty() && (index == -1 || index >= frame.listModel.size()))
|
||||
{
|
||||
frame.listStorage.setSelectedIndex(0);
|
||||
}
|
||||
else if (!frame.listModel.isEmpty() && index > -1 && index < frame.listModel.size())
|
||||
{
|
||||
frame.listStorage.setSelectedIndex(index);
|
||||
}
|
||||
|
||||
}
|
||||
protected void ButtonAddObject_Click()
|
||||
{
|
||||
String name = frame.nameStorageField.getText();
|
||||
if (name.length() == 0)
|
||||
return;
|
||||
_tanksStorage.AddSet(name);
|
||||
ReloadObjects();
|
||||
}
|
||||
|
||||
protected void ButtonRemoveObject_Click()
|
||||
{
|
||||
if (frame.listStorage.getSelectedIndex() == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (JOptionPane.showConfirmDialog(null, "Delete object " + frame.listStorage.getSelectedValue() + "?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_tanksStorage.DelSet(frame.listStorage.getSelectedValue());
|
||||
ReloadObjects();
|
||||
}
|
||||
|
||||
protected void ButtonAddTank_Click()
|
||||
{
|
||||
if (frame.listStorage.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
ConfigFrame form = new ConfigFrame();
|
||||
form.setVisible(true);
|
||||
form.buttonAdd.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
DrawTanker dT = form.Gasoline._tanker;
|
||||
if (dT != null && obj.plus(dT) != -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 (frame.listStorage.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
if (JOptionPane.showConfirmDialog(null, "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
|
||||
{
|
||||
int index = -1;
|
||||
DrawTanker deleted = null;
|
||||
try {
|
||||
deleted = obj.minus(Integer.parseInt(text));
|
||||
} catch(NumberFormatException e) {
|
||||
}
|
||||
if (deleted != null)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
removedTankers.add(deleted);
|
||||
super.repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
protected void ButtonUpdate_Click() {super.repaint();}
|
||||
|
||||
protected void ButtonCreateDeleted_Click()
|
||||
{
|
||||
if (removedTankers.isEmpty())
|
||||
return;
|
||||
var tank = removedTankers.poll();
|
||||
BaseFrame f = new BaseFrame(tank);
|
||||
f.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
class GarageFrame extends JFrame {
|
||||
public GarageFrame()
|
||||
{
|
||||
initUI();
|
||||
}
|
||||
|
||||
protected static final int Width = 1000;
|
||||
protected static final int Height = 600;
|
||||
CollectionFrame Collection;
|
||||
|
||||
JButton addTankerButton;
|
||||
JTextField indexTankerField;
|
||||
JButton deleteTankerButton;
|
||||
JButton updateCollectionButton;
|
||||
JList<String> listStorage;
|
||||
DefaultListModel<String> listModel;
|
||||
JButton addCollectionButton;
|
||||
JTextField nameStorageField;
|
||||
JButton deleteCollectionbutton;
|
||||
JButton createDeletedButton;
|
||||
|
||||
private void initUI()
|
||||
{
|
||||
setSize(Width, Height);
|
||||
setTitle("TankGasoline");
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setResizable(false);
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
indexTankerField = new JTextField();
|
||||
indexTankerField.setBounds(Width- 200, Height-500, 200, 30);
|
||||
indexTankerField.setLayout(null);
|
||||
add(indexTankerField);
|
||||
|
||||
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());
|
||||
}
|
||||
});
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
listModel = new DefaultListModel<>();
|
||||
listStorage = new JList<String>(listModel);
|
||||
listStorage.setLayout(null);
|
||||
listStorage.setBounds(Width - 190, Height - 250, 180, 130);
|
||||
add(listStorage);
|
||||
listStorage.addListSelectionListener(new ListSelectionListener() {
|
||||
@Override
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
Collection.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
addCollectionButton = new JButton("Add Collection");
|
||||
addCollectionButton.setLayout(null);
|
||||
addCollectionButton.setBounds(Width-190, Height - 350, 180, 30);
|
||||
add(addCollectionButton);
|
||||
addCollectionButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Collection.ButtonAddObject_Click();
|
||||
}
|
||||
});
|
||||
|
||||
nameStorageField = new JTextField();
|
||||
nameStorageField.setBounds(Width- 190, Height- 300, 180, 30);
|
||||
nameStorageField.setLayout(null);
|
||||
add(nameStorageField);
|
||||
|
||||
|
||||
deleteCollectionbutton = new JButton("Remove Collection");
|
||||
deleteCollectionbutton.setBounds(Width-190, Height - 100, 180, 30);
|
||||
deleteCollectionbutton.setLayout(null);
|
||||
add(deleteCollectionbutton);
|
||||
deleteCollectionbutton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Collection.ButtonRemoveObject_Click();
|
||||
}
|
||||
});
|
||||
|
||||
createDeletedButton = new JButton("Create deleted");
|
||||
createDeletedButton.setLayout(null);
|
||||
createDeletedButton.setBounds(Width - 200, 10, 200, 30);
|
||||
add(createDeletedButton);
|
||||
createDeletedButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Collection.ButtonCreateDeleted_Click();
|
||||
}
|
||||
});
|
||||
|
||||
Collection = new CollectionFrame(Width-200, Height, this);
|
||||
Collection.setLayout(null);
|
||||
Collection.setBounds(0, 0, Width-200, Height);
|
||||
add(Collection);
|
||||
|
||||
}
|
||||
|
||||
}
|
105
ComboFrame.java
Normal file
105
ComboFrame.java
Normal file
@ -0,0 +1,105 @@
|
||||
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(GenerateTanker());
|
||||
GeneratorTanker.Add(GenerateWheel());
|
||||
GeneratorTanker.Add(GenerateTanker());
|
||||
GeneratorTanker.Add(GenerateWheel());
|
||||
GeneratorTanker.Add(GenerateTanker());
|
||||
GeneratorTanker.Add(GenerateWheel());
|
||||
}
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
if (_drawTanker == null)
|
||||
return;
|
||||
_drawTanker.DrawTransport(g);
|
||||
super.repaint();
|
||||
}
|
||||
private boolean IntToBool(int n) {return n % 2 == 0;}
|
||||
public BaseTanker GenerateTanker()
|
||||
{
|
||||
Random random = new Random();
|
||||
int mode = random.nextInt(0, 100) % 2;
|
||||
if (mode == 0)
|
||||
{
|
||||
return 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 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 IWheelDraw GenerateWheel()
|
||||
{
|
||||
Random random = new Random();
|
||||
int mode = random.nextInt(0, 100) % 3;
|
||||
IWheelDraw Wheels = new DrawWheelSquare();
|
||||
switch (mode)
|
||||
{
|
||||
case 0 -> {Wheels = new DrawWheelCircle();}
|
||||
case 1 -> {Wheels = new DrawWheelClassic();}
|
||||
case 2 -> {Wheels = new DrawWheelSquare();}
|
||||
}
|
||||
int count = random.nextInt(0, 100);
|
||||
Wheels.setWheelCount(count);
|
||||
return Wheels;
|
||||
}
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
66
ComboGenericCollection.java
Normal file
66
ComboGenericCollection.java
Normal file
@ -0,0 +1,66 @@
|
||||
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 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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)]);
|
||||
}
|
||||
}
|
429
ConfigFrame.java
Normal file
429
ConfigFrame.java
Normal file
@ -0,0 +1,429 @@
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.Border;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.io.IOException;
|
||||
|
||||
class DrawConfig extends JComponent {
|
||||
public DrawTanker _tanker;
|
||||
public DrawConfig()
|
||||
{
|
||||
_tanker = null;
|
||||
}
|
||||
private boolean IntToBool(int n)
|
||||
{
|
||||
return n > 0;
|
||||
}
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
if (_tanker == null)
|
||||
return;
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_tanker.DrawTransport(g);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class LabelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new StringSelection(((JLabel)c).getText());
|
||||
}
|
||||
}
|
||||
|
||||
class ColorTransferable implements Transferable {
|
||||
private Color color;
|
||||
static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
|
||||
|
||||
public ColorTransferable(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{colorDataFlavor};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return colorDataFlavor.equals(flavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor))
|
||||
return color;
|
||||
else
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
|
||||
class PanelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new ColorTransferable(((JPanel)c).getBackground());
|
||||
}
|
||||
}
|
||||
|
||||
class LabelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
class PanelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
class WheelTransferable implements Transferable {
|
||||
private IWheelDraw wheelDraw;
|
||||
static final DataFlavor wheelDrawingDataFlavor = new DataFlavor(IWheelDraw.class, "Wheel Drawing");
|
||||
|
||||
public WheelTransferable(IWheelDraw wheelDrawing) {
|
||||
wheelDraw = wheelDrawing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{wheelDrawingDataFlavor};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return flavor.equals(wheelDrawingDataFlavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return wheelDraw;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ComponentWheel extends JComponent{
|
||||
public IWheelDraw wheelDraw;
|
||||
|
||||
public ComponentWheel(IWheelDraw _wheelDrawing){
|
||||
wheelDraw = _wheelDrawing;
|
||||
this.addMouseListener(
|
||||
new MouseAdapter(){
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((ComponentWheel)e.getComponent()).getTransferHandler().exportAsDrag(((ComponentWheel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
);
|
||||
this.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new WheelTransferable(((ComponentWheel)c).wheelDraw);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
@Override
|
||||
public void paintComponent (Graphics g){
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
wheelDraw.DrawWheels(20,-40,Color.BLACK,g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public class ConfigFrame extends JFrame {
|
||||
|
||||
public ConfigFrame()
|
||||
{
|
||||
initUI();
|
||||
}
|
||||
|
||||
public JButton buttonAdd;
|
||||
protected static final int Width = 1000;
|
||||
protected static final int Height = 400;
|
||||
DrawConfig Gasoline;
|
||||
private void initUI()
|
||||
{
|
||||
Border border = BorderFactory.createLineBorder(Color.GRAY);
|
||||
|
||||
JLabel labelSpeed = new JLabel("Speed");
|
||||
labelSpeed.setBounds(50, 10, 50, 50);
|
||||
add(labelSpeed);
|
||||
|
||||
JLabel labelWeight = new JLabel("Weight");
|
||||
labelWeight.setBounds(50, 60, 50, 50);
|
||||
add(labelWeight);
|
||||
|
||||
JLabel labelWheel = new JLabel("Wheel");
|
||||
labelWheel.setBounds(50, 110, 50, 50);
|
||||
add(labelWheel);
|
||||
|
||||
SpinnerModel spinnerModel = new SpinnerNumberModel(100, 100, 1000, 1);
|
||||
SpinnerModel spinnerModel2 = new SpinnerNumberModel(100, 100, 1000, 1);
|
||||
SpinnerModel spinnerModel3 = new SpinnerNumberModel(2, 2, 4, 1);
|
||||
|
||||
JSpinner numericSpeed = new JSpinner(spinnerModel);
|
||||
numericSpeed.setBounds(100, 20, 50, 30);
|
||||
add(numericSpeed);
|
||||
|
||||
JSpinner numericWeight = new JSpinner(spinnerModel2);
|
||||
numericWeight.setBounds(100, 70, 50, 30);
|
||||
add(numericWeight);
|
||||
|
||||
JSpinner numericWheels = new JSpinner(spinnerModel3);
|
||||
numericWheels.setBounds(100, 120, 50, 30);
|
||||
add(numericWheels);
|
||||
|
||||
JCheckBox checkBoxLight = new JCheckBox("Light");
|
||||
checkBoxLight.setBounds(50, 200, 100, 50);
|
||||
add(checkBoxLight);
|
||||
|
||||
JCheckBox checkBoxFuel = new JCheckBox("Fuel");
|
||||
checkBoxFuel.setBounds(50, 250, 100, 50);
|
||||
add(checkBoxFuel);
|
||||
|
||||
JCheckBox checkBoxSpeedLine = new JCheckBox("Speed Line");
|
||||
checkBoxSpeedLine.setBounds(50, 300, 100, 50);
|
||||
add(checkBoxSpeedLine);
|
||||
|
||||
JPanel[] colorPanels = {
|
||||
new JPanel(), new JPanel(), new JPanel(), new JPanel(),
|
||||
new JPanel(), new JPanel(), new JPanel(), new JPanel()
|
||||
};
|
||||
|
||||
colorPanels[0].setBackground(Color.BLACK);
|
||||
colorPanels[1].setBackground(Color.BLUE);
|
||||
colorPanels[2].setBackground(Color.GRAY);
|
||||
colorPanels[3].setBackground(Color.YELLOW);
|
||||
colorPanels[4].setBackground(Color.RED);
|
||||
colorPanels[5].setBackground(Color.GREEN);
|
||||
colorPanels[6].setBackground(Color.WHITE);
|
||||
colorPanels[7].setBackground(Color.MAGENTA);
|
||||
for (var i : colorPanels)
|
||||
{
|
||||
i.setTransferHandler(new PanelTransferHandler());
|
||||
i.addMouseListener(new PanelMouseAdapter());
|
||||
}
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
colorPanels[i].setBounds(300 + 50 * (i % 4), 50 + i / 4 * 50, 50, 50);
|
||||
add(colorPanels[i]);
|
||||
}
|
||||
|
||||
JLabel labelBase = new JLabel("Base");
|
||||
labelBase.setTransferHandler(new LabelTransferHandler());
|
||||
labelBase.addMouseListener(new LabelMouseAdapter());
|
||||
labelBase.setBorder(border);
|
||||
labelBase.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelBase.setVerticalAlignment(SwingConstants.CENTER);
|
||||
labelBase.setBounds(300, 200, 100, 50);
|
||||
add(labelBase);
|
||||
|
||||
JLabel labelUpdate = new JLabel("Updated");
|
||||
labelUpdate.setTransferHandler(new LabelTransferHandler());
|
||||
labelUpdate.addMouseListener(new LabelMouseAdapter());
|
||||
labelUpdate.setBorder(border);
|
||||
labelUpdate.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelUpdate.setVerticalAlignment(SwingConstants.CENTER);
|
||||
labelUpdate.setBounds(400, 200, 100, 50);
|
||||
add(labelUpdate);
|
||||
|
||||
JLabel labelColor = new JLabel("BaseColor");
|
||||
labelColor.setBorder(border);
|
||||
labelColor.setVerticalAlignment(SwingConstants.CENTER);
|
||||
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelColor.setBounds(600, 50, 100, 50);
|
||||
add(labelColor);
|
||||
labelColor.setTransferHandler(new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support))
|
||||
{
|
||||
try
|
||||
{
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (Gasoline._tanker == null)
|
||||
return false;
|
||||
Gasoline._tanker.GasolineTanker.setBodyColor(color);
|
||||
Gasoline.repaint();
|
||||
return true;
|
||||
}
|
||||
catch (UnsupportedFlavorException | IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
JLabel labelAddColor = new JLabel("AddColor");
|
||||
labelAddColor.setBorder(border);
|
||||
labelAddColor.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelAddColor.setVerticalAlignment(SwingConstants.CENTER);
|
||||
labelAddColor.setBounds(700, 50, 100, 50);
|
||||
add(labelAddColor);
|
||||
labelAddColor.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (Gasoline._tanker == null)
|
||||
return false;
|
||||
if (!(Gasoline._tanker instanceof DrawGasolineTanker))
|
||||
return false;
|
||||
((GasolineTanker)Gasoline._tanker.GasolineTanker).setAdditionalColor(color);
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
JLabel labelWheels = new JLabel("Wheels");
|
||||
labelWheels.setBorder(border);
|
||||
labelWheels.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelWheels.setVerticalAlignment(SwingConstants.CENTER);
|
||||
labelWheels.setBounds(800, 50, 100, 50);
|
||||
add(labelWheels);
|
||||
labelWheels.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(WheelTransferable.wheelDrawingDataFlavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
IWheelDraw wheelDrawing = (IWheelDraw) support.getTransferable().getTransferData(WheelTransferable.wheelDrawingDataFlavor);
|
||||
if (Gasoline._tanker == null)
|
||||
return false;
|
||||
wheelDrawing.setWheelCount(Gasoline._tanker.wheelsDrawing.getWheelCount());
|
||||
Gasoline._tanker.wheelsDrawing = wheelDrawing;
|
||||
Gasoline.repaint();
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
setSize(Width, Height);
|
||||
setTitle("TankGasoline");
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setResizable(false);
|
||||
Gasoline = new DrawConfig();
|
||||
Gasoline.setBounds(600, 100, 300, 200);
|
||||
add(Gasoline);
|
||||
Gasoline.setTransferHandler(
|
||||
new TransferHandler() {
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
|
||||
switch (data) {
|
||||
case "Base":
|
||||
Gasoline._tanker = new DrawTanker((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, Width,Height, (int)numericWheels.getValue());
|
||||
break;
|
||||
case "Updated":
|
||||
Gasoline._tanker = new DrawGasolineTanker((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, Color.BLACK, checkBoxFuel.isSelected(), checkBoxLight.isSelected(), checkBoxSpeedLine.isSelected(), Width, Height, (int)numericWheels.getValue());
|
||||
break;
|
||||
}
|
||||
Gasoline.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
ComponentWheel componentWheelCircle = new ComponentWheel(new DrawWheelCircle());
|
||||
ComponentWheel componentWheelClassic = new ComponentWheel(new DrawWheelClassic());
|
||||
ComponentWheel componentWheelSquare = new ComponentWheel(new DrawWheelSquare());
|
||||
componentWheelCircle.setBounds(300, 250, 50, 50);
|
||||
add(componentWheelCircle);
|
||||
componentWheelClassic.setBounds(350, 250, 50, 50);
|
||||
add(componentWheelClassic);
|
||||
componentWheelSquare.setBounds(400, 250, 50, 50);
|
||||
add(componentWheelSquare);
|
||||
|
||||
buttonAdd = new JButton("Add");
|
||||
buttonAdd.setBounds(600, 300, 100, 50);
|
||||
add(buttonAdd);
|
||||
|
||||
JButton buttonCancel = new JButton("Cancel");
|
||||
buttonCancel.setBounds(700, 300, 100, 50);
|
||||
add(buttonCancel);
|
||||
|
||||
buttonCancel.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
16
Direction.java
Normal file
16
Direction.java
Normal file
@ -0,0 +1,16 @@
|
||||
public enum Direction {
|
||||
Up ("U"),
|
||||
Down ("D"),
|
||||
Left ("L"),
|
||||
Right ("R");
|
||||
private String direct;
|
||||
Direction(String d)
|
||||
{
|
||||
direct = d;
|
||||
}
|
||||
public String getDirect()
|
||||
{
|
||||
return direct;
|
||||
}
|
||||
|
||||
}
|
41
DrawGasolineTanker.java
Normal file
41
DrawGasolineTanker.java
Normal file
@ -0,0 +1,41 @@
|
||||
import java.awt.*;
|
||||
|
||||
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) {
|
||||
super(speed, weight, bodyColor, width, height, wheelCount, wheelMode);
|
||||
if (GasolineTanker != null) {
|
||||
GasolineTanker = new GasolineTanker(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
|
||||
}
|
||||
}
|
||||
public DrawGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount) {
|
||||
super(speed, weight, bodyColor, width, height, wheelCount);
|
||||
if (GasolineTanker != null) {
|
||||
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
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (!(GasolineTanker instanceof GasolineTanker)) {
|
||||
return;
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
g.setColor(((GasolineTanker) GasolineTanker).GetAdditionalColor());
|
||||
if (((GasolineTanker) GasolineTanker).GetBodyKit()) {
|
||||
g.fillOval(10 + _startPosX, 10 + _startPosY, 70, 30);
|
||||
}
|
||||
if (((GasolineTanker) GasolineTanker).GetSportLine()) {
|
||||
g.fillRect(15 + _startPosX, 45 + _startPosY, 20, 5);
|
||||
g.fillRect(40 + _startPosX, 45 + _startPosY, 20, 5);
|
||||
g.fillRect(65 + _startPosX, 45 + _startPosY, 20, 5);
|
||||
}
|
||||
if (((GasolineTanker) GasolineTanker).GetWing()) {
|
||||
g.fillRect(87 + _startPosX, 5 + _startPosY, 5, 5);
|
||||
}
|
||||
}
|
||||
}
|
121
DrawTanker.java
Normal file
121
DrawTanker.java
Normal file
@ -0,0 +1,121 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawTanker {
|
||||
protected BaseTanker GasolineTanker;
|
||||
public BaseTanker GetGasolineTanker() {return GasolineTanker;}
|
||||
protected IWheelDraw wheelsDrawing;
|
||||
protected int _pictureWidth;
|
||||
protected int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected final int _carWidth = 110;
|
||||
protected final int _carHeight = 120;
|
||||
public int GetPosX() {return _startPosX;}
|
||||
public int GetPosY() {return _startPosY;}
|
||||
public int GetWidth() {return _carWidth;}
|
||||
public int GetHeight() {return _carHeight;}
|
||||
|
||||
public boolean CanMove(Direction direction)
|
||||
{
|
||||
if (GasolineTanker == null)
|
||||
return false;
|
||||
switch (direction)
|
||||
{
|
||||
case Left -> {
|
||||
return _startPosX - GasolineTanker.Step > 0; }
|
||||
case Up -> {
|
||||
return _startPosY - GasolineTanker.Step > 0; }
|
||||
case Right -> {
|
||||
return _startPosX + _carWidth + GasolineTanker.Step < _pictureWidth; }
|
||||
case Down -> {
|
||||
return _startPosY + _carHeight + GasolineTanker.Step < _pictureHeight; }
|
||||
default -> {
|
||||
return false; }
|
||||
|
||||
}
|
||||
}
|
||||
public DrawTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount, int wheelMode)
|
||||
{
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
GasolineTanker = new BaseTanker(speed, weight, bodyColor);
|
||||
int mode = wheelMode % 3;
|
||||
switch (mode)
|
||||
{
|
||||
case 0 -> {wheelsDrawing = new DrawWheelCircle();}
|
||||
case 1 -> {wheelsDrawing = new DrawWheelSquare();}
|
||||
case 2 -> {wheelsDrawing = new DrawWheelClassic();}
|
||||
}
|
||||
wheelsDrawing.setWheelCount(wheelCount);
|
||||
}
|
||||
|
||||
public DrawTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount)
|
||||
{
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
GasolineTanker = new BaseTanker(speed, weight, bodyColor);
|
||||
wheelsDrawing = new DrawWheelClassic();
|
||||
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)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (!CanMove(direction) || GasolineTanker == null)
|
||||
return;
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
{
|
||||
_startPosX -= (int)GasolineTanker.Step;
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
{
|
||||
_startPosY -= (int)GasolineTanker.Step;
|
||||
}
|
||||
break;
|
||||
case Right:
|
||||
{
|
||||
_startPosX += (int)GasolineTanker.Step;
|
||||
}
|
||||
break;
|
||||
case Down:
|
||||
{
|
||||
_startPosY += (int)GasolineTanker.Step;
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (GasolineTanker == null)
|
||||
return;
|
||||
var g2d = (Graphics2D) g;
|
||||
g2d.setColor(GasolineTanker.getBodyColor());
|
||||
// Отрисовка корпуса
|
||||
g2d.fillRect(10 + _startPosX, 40 + _startPosY, 90, 20);
|
||||
g2d.fillRect(80 + _startPosX, 10 + _startPosY, 20, 40);
|
||||
// Отрисовка колесиков
|
||||
wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d);
|
||||
|
||||
}
|
||||
|
||||
public IMoveableObject GetMoveableObject() {return new DrawingObjectTanker(this);}
|
||||
}
|
55
DrawWheelCircle.java
Normal file
55
DrawWheelCircle.java
Normal file
@ -0,0 +1,55 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawWheelCircle implements IWheelDraw {
|
||||
private WheelCounter wheelCounter;
|
||||
public WheelCounter getWheelCounter()
|
||||
{
|
||||
return wheelCounter;
|
||||
}
|
||||
public void setWheelCount(int count)
|
||||
{
|
||||
if (count % 3 == 0)
|
||||
wheelCounter = WheelCounter.THREE;
|
||||
else if (count % 3 == 1)
|
||||
wheelCounter = WheelCounter.FOUR;
|
||||
else if (count % 3 == 2)
|
||||
wheelCounter = WheelCounter.TWO;
|
||||
}
|
||||
|
||||
public int getWheelCount()
|
||||
{
|
||||
switch (wheelCounter)
|
||||
{
|
||||
case TWO -> {return 2;}
|
||||
case THREE -> {return 3;}
|
||||
case FOUR -> {return 4;}
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
public void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d)
|
||||
{
|
||||
g2d.setColor(bodyColor);
|
||||
Random rnd = new Random();
|
||||
g2d.fillOval(10 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g2d.fillOval(80 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
Color stringColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
g2d.setColor(stringColor);
|
||||
g2d.drawString("S",15 + _startPosX, 75 + _startPosY);
|
||||
g2d.drawString("S",85 + _startPosX, 75 + _startPosY);
|
||||
if (wheelCounter == null)
|
||||
return;
|
||||
g2d.setColor(bodyColor);
|
||||
switch (wheelCounter)
|
||||
{
|
||||
case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);}
|
||||
case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);}
|
||||
}
|
||||
g2d.setColor(stringColor);
|
||||
switch (wheelCounter)
|
||||
{
|
||||
case THREE -> {g2d.drawString("S",50 + _startPosX, 75 + _startPosY);}
|
||||
case FOUR -> {g2d.drawString("S",35 + _startPosX, 75 + _startPosY); g2d.drawString("S",65 + _startPosX, 75 + _startPosY);}
|
||||
}
|
||||
}
|
||||
}
|
42
DrawWheelClassic.java
Normal file
42
DrawWheelClassic.java
Normal file
@ -0,0 +1,42 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawWheelClassic implements IWheelDraw{
|
||||
private WheelCounter wheelCounter;
|
||||
public WheelCounter getWheelCounter()
|
||||
{
|
||||
return wheelCounter;
|
||||
}
|
||||
public void setWheelCount(int count)
|
||||
{
|
||||
if (count % 3 == 0)
|
||||
wheelCounter = WheelCounter.THREE;
|
||||
else if (count % 3 == 1)
|
||||
wheelCounter = WheelCounter.FOUR;
|
||||
else if (count % 3 == 2)
|
||||
wheelCounter = WheelCounter.TWO;
|
||||
}
|
||||
public int getWheelCount()
|
||||
{
|
||||
switch (wheelCounter)
|
||||
{
|
||||
case TWO -> {return 2;}
|
||||
case THREE -> {return 3;}
|
||||
case FOUR -> {return 4;}
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
public void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d)
|
||||
{
|
||||
g2d.setColor(bodyColor);
|
||||
g2d.fillOval(10 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g2d.fillOval(80 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
if (wheelCounter == null)
|
||||
return;
|
||||
switch (wheelCounter)
|
||||
{
|
||||
case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);}
|
||||
case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
59
DrawWheelSquare.java
Normal file
59
DrawWheelSquare.java
Normal file
@ -0,0 +1,59 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawWheelSquare implements IWheelDraw{
|
||||
private WheelCounter wheelCounter;
|
||||
public WheelCounter getWheelCounter()
|
||||
{
|
||||
return wheelCounter;
|
||||
}
|
||||
public void setWheelCount(int count)
|
||||
{
|
||||
if (count % 3 == 0)
|
||||
wheelCounter = WheelCounter.THREE;
|
||||
else if (count % 3 == 1)
|
||||
wheelCounter = WheelCounter.FOUR;
|
||||
else if (count % 3 == 2)
|
||||
wheelCounter = WheelCounter.TWO;
|
||||
}
|
||||
|
||||
public int getWheelCount()
|
||||
{
|
||||
switch (wheelCounter)
|
||||
{
|
||||
case TWO -> {return 2;}
|
||||
case THREE -> {return 3;}
|
||||
case FOUR -> {return 4;}
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
public void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d)
|
||||
{
|
||||
g2d.setColor(bodyColor);
|
||||
g2d.fillOval(10 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g2d.fillOval(80 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
Random rnd = new Random();
|
||||
Color stringColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
g2d.setColor(stringColor);
|
||||
g2d.fillRect(15 + _startPosX, 65 + _startPosY, 10, 10);
|
||||
g2d.fillRect(85 + _startPosX, 65 + _startPosY, 10, 10);
|
||||
if (wheelCounter == null)
|
||||
return;
|
||||
g2d.setColor(bodyColor);
|
||||
switch (wheelCounter) {
|
||||
case THREE -> {
|
||||
g2d.fillOval(45 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
}
|
||||
case FOUR -> {
|
||||
g2d.fillOval(30 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g2d.fillOval(60 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
}
|
||||
}
|
||||
g2d.setColor(stringColor);
|
||||
switch (wheelCounter)
|
||||
{
|
||||
case THREE -> {g2d.fillRect(50 + _startPosX, 65 + _startPosY, 10, 10);}
|
||||
case FOUR -> {g2d.fillRect(35 + _startPosX, 65 + _startPosY, 10, 10); g2d.fillRect(65 + _startPosX, 65 + _startPosY, 10, 10);}
|
||||
}
|
||||
}
|
||||
}
|
25
DrawingObjectTanker.java
Normal file
25
DrawingObjectTanker.java
Normal file
@ -0,0 +1,25 @@
|
||||
public class DrawingObjectTanker implements IMoveableObject {
|
||||
private final DrawTanker _drawTanker;
|
||||
|
||||
public DrawingObjectTanker(DrawTanker drawTanker) {
|
||||
_drawTanker = drawTanker;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectParameters() {
|
||||
if (_drawTanker == null || _drawTanker.GetGasolineTanker() == null) {
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawTanker.GetPosX(), _drawTanker.GetPosY(), _drawTanker.GetWidth(), _drawTanker.GetHeight());
|
||||
}
|
||||
|
||||
public int GetStep() {
|
||||
int s = (int) _drawTanker.GasolineTanker.Step;
|
||||
if (s != 0)
|
||||
return s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean CheckCanMove(Direction direction) {return !_drawTanker.CanMove(direction); }
|
||||
public void MoveObject(Direction direction) { _drawTanker.MoveTransport(direction); }
|
||||
|
||||
}
|
21
GasolineTanker.java
Normal file
21
GasolineTanker.java
Normal file
@ -0,0 +1,21 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class GasolineTanker extends BaseTanker{
|
||||
private Color AdditionalColor;
|
||||
public Color GetAdditionalColor() {return AdditionalColor;}
|
||||
public void setAdditionalColor(Color color) {AdditionalColor = color;}
|
||||
private boolean BodyKit;
|
||||
public boolean GetBodyKit() {return BodyKit;}
|
||||
private boolean Wing;
|
||||
public boolean GetWing() {return Wing;}
|
||||
private boolean SportLine;
|
||||
public boolean GetSportLine() {return SportLine;}
|
||||
public GasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine)
|
||||
{
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
BodyKit = bodyKit;
|
||||
Wing = wing;
|
||||
SportLine = sportLine;
|
||||
}
|
||||
}
|
6
IMoveableObject.java
Normal file
6
IMoveableObject.java
Normal file
@ -0,0 +1,6 @@
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectParameters();
|
||||
int GetStep();
|
||||
boolean CheckCanMove(Direction direction);
|
||||
void MoveObject(Direction direction);
|
||||
}
|
8
IWheelDraw.java
Normal file
8
IWheelDraw.java
Normal file
@ -0,0 +1,8 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IWheelDraw {
|
||||
void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d);
|
||||
void setWheelCount(int wheelCount);
|
||||
int getWheelCount();
|
||||
|
||||
}
|
6
Main.java
Normal file
6
Main.java
Normal file
@ -0,0 +1,6 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
GarageFrame a = new GarageFrame();
|
||||
a.setVisible(true);
|
||||
}
|
||||
}
|
30
MoveToBorder.java
Normal file
30
MoveToBorder.java
Normal file
@ -0,0 +1,30 @@
|
||||
public class MoveToBorder extends AbstractStrategy
|
||||
{
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder() + GetStep() >= GetFieldWidth() && objParams.DownBorder() + GetStep() >= GetFieldHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.RightBorder() - GetFieldWidth();
|
||||
int diffY = objParams.DownBorder() - GetFieldHeight();
|
||||
if (diffX >= 0) {
|
||||
MoveDown();
|
||||
} else if (diffY >= 0) {
|
||||
MoveRight();
|
||||
} else if (Math.abs(diffX) > Math.abs(diffY)) {
|
||||
MoveRight();
|
||||
} else {
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
35
MoveToCenter.java
Normal file
35
MoveToCenter.java
Normal file
@ -0,0 +1,35 @@
|
||||
public class MoveToCenter extends AbstractStrategy {
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() <= GetFieldWidth() / 2 && objParams.ObjectMiddleHorizontal() + GetStep() >= GetFieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= GetFieldHeight() / 2 && objParams.ObjectMiddleVertical() + GetStep() >= GetFieldHeight() / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2;
|
||||
if (Math.abs(diffX) > GetStep()) {
|
||||
if (diffX > 0) {
|
||||
MoveLeft();
|
||||
} else {
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMiddleVertical() - GetFieldHeight() / 2;
|
||||
if (Math.abs(diffY) > GetStep()) {
|
||||
if (diffY > 0) {
|
||||
MoveUp();
|
||||
} else {
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
ObjectParameters.java
Normal file
19
ObjectParameters.java
Normal file
@ -0,0 +1,19 @@
|
||||
public class ObjectParameters {
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
private final int _width;
|
||||
private final int _height;
|
||||
|
||||
public int LeftBorder() { return _x; }
|
||||
public int RightBorder() { return _x + _width; }
|
||||
public int TopBorder() { return _y; }
|
||||
public int DownBorder() { return _y + _height; }
|
||||
public int ObjectMiddleHorizontal() { return _x + _width / 2; }
|
||||
public int ObjectMiddleVertical() { return _y + _height / 2; }
|
||||
public ObjectParameters(int x, int y, int width, int height) {
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
74
SetGeneric.java
Normal file
74
SetGeneric.java
Normal file
@ -0,0 +1,74 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class SetGeneric <T extends Object>{
|
||||
private final ArrayList<T> _places;
|
||||
public int Count() {return _places.size();}
|
||||
private final int _maxCount;
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_maxCount = count;
|
||||
_places = new ArrayList<>();
|
||||
}
|
||||
public int Insert(T tanker)
|
||||
{
|
||||
return Insert(tanker, 0);
|
||||
}
|
||||
public int Insert(T tanker, int position)
|
||||
{
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return -1;
|
||||
_places.add(position, tanker);
|
||||
return position;
|
||||
}
|
||||
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= Count())
|
||||
return null;
|
||||
var returning = _places.get(position);
|
||||
_places.remove(position);
|
||||
return returning;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= Count())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _places.get(position);
|
||||
}
|
||||
|
||||
public Iterable<T> GetTankers(final Integer maxTankers) {
|
||||
return new Iterable<T>() {
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
private int currentIndex = 0;
|
||||
private int count = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex < _places.size() && (maxTankers == null || count < maxTankers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (hasNext()) {
|
||||
count++;
|
||||
return _places.get(currentIndex++);
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
14
Status.java
Normal file
14
Status.java
Normal file
@ -0,0 +1,14 @@
|
||||
public enum Status {
|
||||
NotInit ("NI"),
|
||||
InProgress ("IP"),
|
||||
Finish ("F");
|
||||
private String status;
|
||||
Status(String d)
|
||||
{
|
||||
status = d;
|
||||
}
|
||||
public String getDirect()
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
14
WheelCounter.java
Normal file
14
WheelCounter.java
Normal file
@ -0,0 +1,14 @@
|
||||
public enum WheelCounter {
|
||||
TWO (2),
|
||||
THREE (3),
|
||||
FOUR (4);
|
||||
private int count;
|
||||
WheelCounter(int c)
|
||||
{
|
||||
count = c;
|
||||
}
|
||||
public int getCount()
|
||||
{
|
||||
return count;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user