import Drawings.CanvasExcavator; import Drawings.DirectionType; import Drawings.DrawingBulldozer; import Drawings.DrawingExcavator; import MovementStrategy.*; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Random; public class FormExcavator extends JFrame { private String title; private Dimension dimension; private int Width, Height; private CanvasExcavator canvasExcavator = new CanvasExcavator(); private JButton CreateButton = new JButton("Создать экскаватор"); private JButton CreateBullButton = new JButton("Создать бульдозер"); private JButton UpButton = new JButton(); private JButton DownButton = new JButton(); private JButton LeftButton = new JButton(); private JButton RightButton = new JButton(); private AbstractStrategy _strategy; private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"}); private JButton ButtonStrategy = new JButton("Шаг"); public FormExcavator(String title, Dimension dimension) { this.title = title; this.dimension = dimension; } private void CreateObject(String typeOfClass) { int StartPositionX = (int)(Math.random() * 90 + 10); int StartPositionY = (int)(Math.random() * 90 + 10); int speed = (int)(Math.random() * 300 + 100); double weight = (double)(Math.random() * 3000 + 1000); Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); switch (typeOfClass) { case "DrawingBulldozer": canvasExcavator._drawingBulldozer = new DrawingBulldozer(speed, weight, bodyColor); canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height); canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY); canvasExcavator.repaint(); break; case "DrawingExcavator": Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); boolean prop = new Random().nextBoolean(); boolean ladle = new Random().nextBoolean(); canvasExcavator._drawingBulldozer = new DrawingExcavator(speed, weight, bodyColor, additionalColor, prop, ladle); canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height); canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY); canvasExcavator.repaint(); break; default: return; } _strategy = null; ComboBoxStrategy.setEnabled(true); } public void Init() { setTitle(title); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Width = getWidth() - 15; Height = getHeight() - 35; _strategy = null; CreateButton.setName("Create"); Icon iconUp = new ImageIcon("res/up.png"); UpButton.setIcon(iconUp); UpButton.setName("Up"); DownButton.setName("Down"); Icon iconDown = new ImageIcon("res/down.png"); DownButton.setIcon(iconDown); LeftButton.setName("Left"); Icon iconLeft = new ImageIcon("res/left.png"); LeftButton.setIcon(iconLeft); RightButton.setName("Right"); Icon iconRight = new ImageIcon("res/right.png"); RightButton.setIcon(iconRight); CreateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CreateObject("DrawingExcavator"); } }); CreateBullButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CreateObject("DrawingBulldozer"); } }); ButtonStrategy.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (canvasExcavator._drawingBulldozer == null) return; if (ComboBoxStrategy.isEnabled()) { int index = ComboBoxStrategy.getSelectedIndex(); switch(index) { case 0: _strategy = new MoveToCenter(); break; case 1: _strategy = new MoveToBorder(); break; default: _strategy = null; break; } if (_strategy == null) { return; } _strategy.SetData(new MoveableExcavator(canvasExcavator._drawingBulldozer), Width, Height); } if (_strategy == null) { return; } ComboBoxStrategy.setEnabled(false); _strategy.MakeStep(); if (_strategy.GetStatus() == StrategyStatus.Finish) { ComboBoxStrategy.setEnabled(true); _strategy = null; } } }); ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { if (canvasExcavator._drawingBulldozer == null) return; boolean result = false; switch ((((JButton)(event.getSource())).getName())) { case "Up": result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Up); break; case "Down": result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Down); break; case "Left": result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Left); break; case "Right": result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Right); break; } if (result) { canvasExcavator.repaint(); } } }; UpButton.addActionListener(actionListener); DownButton.addActionListener(actionListener); LeftButton.addActionListener(actionListener); RightButton.addActionListener(actionListener); setSize(dimension.width,dimension.height); setLayout(null); canvasExcavator.setBounds(0,0, getWidth(), getHeight()); CreateButton.setBounds(10, getHeight() - 85, 160, 35); CreateBullButton.setBounds(180, getHeight() - 85, 160, 35); UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35); DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35); RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35); LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35); ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25); ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25); add(CreateButton); add(CreateBullButton); add(UpButton); add(DownButton); add(RightButton); add(LeftButton); add(ButtonStrategy); add(ComboBoxStrategy); add(canvasExcavator); setVisible(true); } }