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 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 strategy = new JComboBox(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 strategy = new JComboBox(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()); } } }