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 FormLiner extends JFrame { private String title; private Dimension dimension; private int Width, Height; private CanvasLiner canvasLiner = new CanvasLiner(); private JButton CreateButton = new JButton("Создать");; private JButton UpButton = new JButton(); private JButton DownButton = new JButton();; private JButton LeftButton = new JButton();; private JButton RightButton = new JButton(); public FormLiner(String title, Dimension dimension) { this.title = title; this.dimension = dimension; } public void Init() { setTitle(title); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Width = getWidth() - 15; Height = getHeight() - 35; CreateButton.setName("CREATE"); Icon iconUp = new ImageIcon("src\\imges\\up.jpg"); UpButton.setIcon(iconUp); UpButton.setName("UP"); DownButton.setName("DOWN"); Icon iconDown = new ImageIcon("src\\images\\down.jpg"); DownButton.setIcon(iconDown); LeftButton.setName("LEFT"); Icon iconLeft = new ImageIcon("src\\images\\left.jpg"); LeftButton.setIcon(iconLeft); RightButton.setName("RIGHT"); Icon iconRight = new ImageIcon("src\\images\\right.jpg"); RightButton.setIcon(iconRight); CreateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { 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)); Color additionalColor = new Color((int) (Math.random() * 255 + 0), (int) (Math.random() * 255 + 0), (int) (Math.random() * 255 + 0)); ; boolean tower = new Random().nextBoolean(); boolean radar = new Random().nextBoolean(); ; canvasLiner._drawingLiner = new DrawningLiner(); canvasLiner._drawingLiner.Init(speed, weight, bodyColor, additionalColor, tower, radar); canvasLiner._drawingLiner.SetPictureSize(Width, Height); canvasLiner._drawingLiner.SetPosition(StartPositionX, StartPositionY); canvasLiner.repaint(); } }); ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { if (canvasLiner._drawingLiner == null) return; boolean result = false; switch ((((JButton) (event.getSource())).getName())) { case "UP": result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Up); break; case "DOWN": result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Down); break; case "LEFT": result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Left); break; case "RIGHT": result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Right); break; } if (result) { canvasLiner.repaint(); } } }; UpButton.addActionListener(actionListener); DownButton.addActionListener(actionListener); LeftButton.addActionListener(actionListener); RightButton.addActionListener(actionListener); setSize(dimension.width, dimension.height); setLayout(null); canvasLiner.setBounds(0, 0, getWidth(), getHeight()); CreateButton.setBounds(10, getHeight() - 90, 100, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); add(CreateButton); add(UpButton); add(DownButton); add(RightButton); add(LeftButton); add(canvasLiner); setVisible(true); // обработка события изменения размеров окна addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { Width = getWidth() - 15; Height = getHeight() - 35; if (canvasLiner._drawingLiner != null) canvasLiner._drawingLiner.SetPictureSize(Width, Height); canvasLiner.setBounds(0, 0, getWidth(), getHeight()); CreateButton.setBounds(10, getHeight() - 90, 100, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); } }); } }