PIbd-22_Petrushin_E.A._Self.../Form.java
2023-11-06 15:52:02 +04:00

164 lines
5.8 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Form extends JFrame {
private JPanel mainPanel;
private JButton buttonCreate;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JLabel pictureBoxSPAU;
private DrawingSPAU drawingSPAU;
private JTextField wheelsTextField;
private JLabel wheelsLabel;
public Form() {
initComponents();
}
private void Draw() {
if (drawingSPAU == null) {
return;
}
BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
drawingSPAU.DrawTransport(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
pictureBoxSPAU.setIcon(imageIcon);
}
private void initComponents() {
mainPanel = new JPanel();
buttonCreate = new JButton("Создать");
buttonUp = new JButton(new ImageIcon("resources/upper_arrow.png"));
buttonDown = new JButton(new ImageIcon("resources/down_arrow.png"));
buttonLeft = new JButton(new ImageIcon("resources/left_arrow.png"));
buttonRight = new JButton(new ImageIcon("resources/right_arrow.png"));
pictureBoxSPAU = new JLabel();
wheelsLabel = new JLabel("Количество колёс:");
wheelsTextField = new JTextField();
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(null);
pictureBoxSPAU.setBounds(0, 0, 882, 453);
mainPanel.add(pictureBoxSPAU);
wheelsLabel.setBounds(22, 410, 150, 29);
mainPanel.add(wheelsLabel);
wheelsTextField.setBounds(150, 410, 80, 29);
mainPanel.add(wheelsTextField);
buttonCreate.setBounds(250, 410, 100, 29);
mainPanel.add(buttonCreate);
buttonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonCreateActionPerformed(e);
}
});
buttonUp.setBounds(804, 336, 30, 30);
mainPanel.add(buttonUp);
ImageIcon iconUp = new ImageIcon("Arrows/upper-arrow.png");
buttonUp.setIcon(iconUp);
buttonUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonUp, e);
}
});
buttonDown.setBounds(804, 409, 30, 30);
mainPanel.add(buttonDown);
ImageIcon iconDown = new ImageIcon("Arrows/down-arrow.png");
buttonDown.setIcon(iconDown);
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonDown, e);
}
});
buttonLeft.setBounds(768, 372, 30, 30);
mainPanel.add(buttonLeft);
ImageIcon iconLeft = new ImageIcon("Arrows/left-arrow.png");
buttonLeft.setIcon(iconLeft);
buttonLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonLeft, e);
}
});
buttonRight.setBounds(840, 372, 30, 30);
mainPanel.add(buttonRight);
ImageIcon iconRight = new ImageIcon("Arrows/right-arrow.png");
buttonRight.setIcon(iconRight);
buttonRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonRight, e);
}
});
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
setPreferredSize(new Dimension(882, 453));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
protected void buttonCreateActionPerformed(ActionEvent e) {
int wheelCount;
try {
wheelCount = Integer.parseInt(wheelsTextField.getText());
} catch (NumberFormatException ex) {
wheelCount = 0;
}
if (wheelCount > 0) {
Random random = new Random();
drawingSPAU = new DrawingSPAU();
drawingSPAU.Init(wheelCount, random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(),
random.nextBoolean(),
random.nextBoolean(),
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawingSPAU.SetPosition(random.nextInt(90) + 10,
random.nextInt(90) + 10);
Draw();
}
}
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
if (drawingSPAU == null) {
return;
}
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
switch (name) {
case "buttonUp":
drawingSPAU.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
drawingSPAU.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
drawingSPAU.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
drawingSPAU.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
}