2023-09-26 19:51:49 +04:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
class DrawGasoline extends JComponent {
|
2023-11-04 14:33:13 +04:00
|
|
|
private DrawTanker _drawTanker;
|
2023-10-18 12:33:39 +04:00
|
|
|
private AbstractStrategy _abstractStrategy;
|
2023-11-04 14:33:13 +04:00
|
|
|
private DrawTanker SelectedCar;
|
|
|
|
public DrawTanker GetSelectedCar() {return SelectedCar;}
|
2023-11-04 19:58:26 +04:00
|
|
|
protected void SetTanker(DrawTanker tanker) {_drawTanker = tanker;}
|
2023-09-26 19:51:49 +04:00
|
|
|
public void paintComponent(Graphics g)
|
|
|
|
{
|
|
|
|
super.paintComponent(g);
|
2023-11-04 14:33:13 +04:00
|
|
|
if (_drawTanker == null)
|
2023-09-26 19:51:49 +04:00
|
|
|
return;
|
2023-11-04 14:33:13 +04:00
|
|
|
_drawTanker.DrawTransport(g);
|
2023-09-26 19:51:49 +04:00
|
|
|
super.repaint();
|
|
|
|
}
|
2023-10-18 12:33:39 +04:00
|
|
|
protected void CreateGasolineTankerButton_Click()
|
2023-09-26 19:51:49 +04:00
|
|
|
{
|
|
|
|
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));
|
2023-11-04 14:33:13 +04:00
|
|
|
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),
|
2023-09-26 19:51:49 +04:00
|
|
|
bodyColor, addColor, IntToBool(rnd.nextInt(0, 2)), IntToBool(rnd.nextInt(0, 2)),
|
2023-11-04 14:33:13 +04:00
|
|
|
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;
|
2023-10-18 12:33:39 +04:00
|
|
|
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));
|
2023-11-04 14:33:13 +04:00
|
|
|
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;
|
2023-09-26 19:51:49 +04:00
|
|
|
super.repaint();
|
|
|
|
}
|
|
|
|
protected void ButtonMove_Click(String tag)
|
|
|
|
{
|
2023-11-04 14:33:13 +04:00
|
|
|
if (_drawTanker == null)
|
2023-09-26 19:51:49 +04:00
|
|
|
return;
|
|
|
|
switch (tag) {
|
2023-11-04 14:33:13 +04:00
|
|
|
case "U" -> _drawTanker.MoveTransport(Direction.Up);
|
|
|
|
case "D" -> _drawTanker.MoveTransport(Direction.Down);
|
|
|
|
case "L" -> _drawTanker.MoveTransport(Direction.Left);
|
|
|
|
case "R" -> _drawTanker.MoveTransport(Direction.Right);
|
2023-09-26 19:51:49 +04:00
|
|
|
}
|
|
|
|
super.repaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean IntToBool(int n)
|
|
|
|
{
|
|
|
|
return n > 0;
|
|
|
|
}
|
2023-10-18 12:33:39 +04:00
|
|
|
|
|
|
|
protected void ButtonStep_Click(JComboBox<String> JCB, int width, int height)
|
|
|
|
{
|
2023-11-04 14:33:13 +04:00
|
|
|
if (_drawTanker == null)
|
2023-10-18 12:33:39 +04:00
|
|
|
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;
|
2023-11-04 14:33:13 +04:00
|
|
|
_abstractStrategy.SetData(_drawTanker.GetMoveableObject(), width, height);
|
2023-10-18 12:33:39 +04:00
|
|
|
JCB.setEnabled(false);
|
|
|
|
}
|
|
|
|
if (_abstractStrategy == null)
|
|
|
|
return;
|
|
|
|
_abstractStrategy.MakeStep();
|
|
|
|
super.repaint();
|
|
|
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
|
|
|
{
|
|
|
|
JCB.setEnabled(true);
|
|
|
|
_abstractStrategy = null;
|
|
|
|
}
|
|
|
|
}
|
2023-09-26 19:51:49 +04:00
|
|
|
}
|
|
|
|
|
2023-11-04 14:33:13 +04:00
|
|
|
class BaseFrame extends JFrame {
|
|
|
|
|
|
|
|
public JButton buttonSelect;
|
|
|
|
public BaseFrame()
|
2023-09-26 19:51:49 +04:00
|
|
|
{
|
|
|
|
initUI();
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:58:26 +04:00
|
|
|
public BaseFrame(DrawTanker gas)
|
|
|
|
{
|
|
|
|
initUIFrom();
|
|
|
|
Gasoline.SetTanker(gas);
|
|
|
|
}
|
2023-09-26 19:51:49 +04:00
|
|
|
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);
|
|
|
|
|
2023-10-18 12:33:39 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2023-09-26 19:51:49 +04:00
|
|
|
|
2023-11-04 14:33:13 +04:00
|
|
|
buttonSelect = new JButton("Select tanker");
|
|
|
|
buttonSelect.setBounds(Width - 120, Height - 150, 80, 30);
|
|
|
|
buttonSelect.setLayout(null);
|
|
|
|
add(buttonSelect);
|
|
|
|
|
2023-09-26 19:51:49 +04:00
|
|
|
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"));
|
|
|
|
|
2023-10-18 12:33:39 +04:00
|
|
|
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() {
|
2023-09-26 19:51:49 +04:00
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2023-10-18 12:33:39 +04:00
|
|
|
Gasoline.CreateGasolineTankerButton_Click();
|
2023-09-26 19:51:49 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2023-11-04 19:58:26 +04:00
|
|
|
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);
|
|
|
|
}
|
2023-09-26 19:51:49 +04:00
|
|
|
public class MoveAL implements ActionListener {
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
Gasoline.ButtonMove_Click(e.getActionCommand());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|