import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.*; import java.io.IOException; import java.util.Random; class DrawCrane extends JComponent { private DrawingCrane _drawingCrane; public void paintComponent(Graphics g) { super.paintComponent(g); if (_drawingCrane == null) return; _drawingCrane.DrawTransport(g); super.repaint(); } protected void CreateCraneButton_Click() { Random rnd = new Random(); _drawingCrane = new DrawingCrane(); Color addColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt( 256)); Color bodyColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt(256)); _drawingCrane.Init(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000, ConvertToBoolean(rnd.nextInt( 2)), ConvertToBoolean(rnd.nextInt( 2)), bodyColor, addColor, Frame.Width-10, Frame.Height-38, rnd.nextInt( 3)+4); _drawingCrane.SetPosition(rnd.nextInt( 100), rnd.nextInt(90)+10); super.repaint(); } protected void ButtonMove_Click(String tag) { if (_drawingCrane == null) return; switch (tag) { case "U" -> _drawingCrane.MoveCrane(DirectionType.Up); case "D" -> _drawingCrane.MoveCrane(DirectionType.Down); case "L" -> _drawingCrane.MoveCrane(DirectionType.Left); case "R" -> _drawingCrane.MoveCrane(DirectionType.Right); } super.repaint(); } static boolean ConvertToBoolean(int value) { if(value ==0) return false; return true; } } public class Frame extends JFrame { public Frame() { InitUI(); } protected static final int Width = 900; protected static final int Height = 500; DrawCrane Crane; private void InitUI() { setSize(Width, Height); setTitle("HoistingCrane"); setLocationRelativeTo(null); setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); Crane = new DrawCrane(); Crane.setBounds(0, 0, Width, Height); add(Crane); MoveAL moving = new MoveAL(); JButton left = new JButton(); left.setActionCommand("L"); left.addActionListener(moving); left.setLayout(null); JButton right = new JButton(); right.setActionCommand("R"); right.setLayout(null); right.addActionListener(moving); JButton up = new JButton(); up.setActionCommand("U"); up.addActionListener(moving); up.setLayout(null); JButton down = new JButton(); down.setActionCommand("D"); down.addActionListener(moving); down.setLayout(null); JButton CreateButton = new JButton("Создать"); right.setIcon(new ImageIcon("src/crane/right.png")); left.setIcon(new ImageIcon("src/crane/left.png")); down.setIcon(new ImageIcon("src/crane/down.png")); up.setIcon(new ImageIcon("src/crane/up.png")); JPanel panelMoveButtons = new JPanel(); panelMoveButtons.setLayout(new GridBagLayout()); panelMoveButtons.setBounds(774, 395, 98, 66); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 1; constraints.gridy = 0; constraints.weightx = 4; constraints.weighty = 6; panelMoveButtons.add(up, constraints); constraints.gridy = 1; constraints.gridx = 0; panelMoveButtons.add(left, constraints); constraints.gridx = 1; panelMoveButtons.add(down, constraints); constraints.gridx = 2; panelMoveButtons.add(right, constraints); JPanel panelCreateButton = new JPanel(); panelCreateButton.setLayout(new BorderLayout()); panelCreateButton.setBounds(0, 420, 112, 34); panelCreateButton.add(CreateButton); add(panelMoveButtons); add(panelCreateButton); setVisible(true); CreateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Crane.CreateCraneButton_Click(); } }); } public class MoveAL implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Crane.ButtonMove_Click(e.getActionCommand()); } } }