2023-12-06 00:23:52 +04:00

172 lines
6.0 KiB
Java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
import Directions.Direction;
import DrawningObjects.DrawningMonorail;
import java.util.Random;
class Form extends JFrame{
private JLabel pictureBoxMonorail;
private JPanel mainPanel;
private JButton btUp;
private JButton btDown;
private JButton btRight;
private JButton btLeft;
private JButton btCreate;
private JLabel wheelsLabel;
private JTextField wheelsTextField;
Random rd = new Random();
private DrawningMonorail _drawningMonorail;
public Form(){
initComponents();
}
private void Draw(){
if(_drawningMonorail == null){
return;
}
BufferedImage bmp = new BufferedImage(pictureBoxMonorail.getWidth(),
pictureBoxMonorail.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
_drawningMonorail.drawMonorail(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
pictureBoxMonorail.setIcon(imageIcon);
}
private void initComponents(){
mainPanel = new JPanel();
btCreate = new JButton("Создать");
btUp = new JButton(new ImageIcon("resources/upper-arrow.png"));
btDown = new JButton(new ImageIcon("resources/down-arrow.png"));
btLeft = new JButton(new ImageIcon("resources/left-arrow.png"));
btRight = new JButton(new ImageIcon("resources/right-arrow.png"));
pictureBoxMonorail = new JLabel();
wheelsLabel = new JLabel("Кол-во колёс: ");
wheelsTextField = new JTextField();
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(null);
pictureBoxMonorail.setBounds(0, 0, 882, 453);
mainPanel.add(pictureBoxMonorail);
wheelsLabel.setBounds(22, 410, 150, 29);
mainPanel.add(wheelsLabel);
wheelsTextField.setBounds(150, 410, 80, 29);
mainPanel.add(wheelsTextField);
btCreate.setBounds(250, 410, 100, 29);
mainPanel.add(btCreate);
btCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonCreateActionPerformed(e);
}
});
btUp.setBounds(790, 355, 40, 40);
mainPanel.add(btUp);
ImageIcon iconUp = new ImageIcon("resources/upper-arrow.png");
btUp.setIcon(iconUp);
btUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btUp, e);
}
});
btDown.setBounds(790, 401, 40, 40);
mainPanel.add(btDown);
ImageIcon iconDown = new ImageIcon("resources/down-arrow.png");
btDown.setIcon(iconDown);
btDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btDown, e);
}
});
btLeft.setBounds(744, 401, 40, 40);
mainPanel.add(btLeft);
ImageIcon iconLeft = new ImageIcon("resources/left-arrow.png");
btLeft.setIcon(iconLeft);
btLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btLeft, e);
}
});
btRight.setBounds(836, 401, 40, 40);
mainPanel.add(btRight);
ImageIcon iconRight = new ImageIcon("resources/right-arrow.png");
btRight.setIcon(iconRight);
btRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btRight, e);
}
});
btUp.setName("buttonUp");
btDown.setName("buttonDown");
btLeft.setName("buttonLeft");
btRight.setName("buttonRight");
setPreferredSize(new Dimension(900, 500));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
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();
_drawningMonorail = new DrawningMonorail();
_drawningMonorail.init(wheelCount, random.nextInt(500,800),
random.nextInt(1000,3000),
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(),
pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
_drawningMonorail.setPosition(random.nextInt(90) + 10,
random.nextInt(90) + 10); Draw();
}
}
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
if (_drawningMonorail == null) {
return;
}
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
switch (name) {
case "buttonUp":
_drawningMonorail.moveTransport(Direction.UP);
break;
case "buttonDown":
_drawningMonorail.moveTransport(Direction.DOWN);
break;
case "buttonLeft":
_drawningMonorail.moveTransport(Direction.LEFT);
break;
case "buttonRight":
_drawningMonorail.moveTransport(Direction.RIGHT);
break;
}
Draw();
}
}
class MonoFrame{
public static void main(String[] args) {
Form fr = new Form();
}
}