PIbd-23_Vrazhkin_S_A_Electr.../Main.java

96 lines
3.8 KiB
Java
Raw Normal View History

2023-12-12 12:31:03 +04:00
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
JFrame LocomotiveFrame = new JFrame();
JPanel LocomotivePanel = new JPanel();
LocomotiveFrame.setLayout(new BorderLayout());
LocomotiveFrame.setSize(900, 500);
LocomotiveFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LocomotiveFrame.setLayout(new BorderLayout(1,1));
DrawningLocomotive DrawningLocomotive = new DrawningLocomotive();
LocomotivePanel.setLayout(null);
JButton RightButton = new JButton();
JButton LeftButton = new JButton();
JButton UpButton = new JButton();
JButton DownButton = new JButton();
JButton CreateButton = new JButton();
RightButton.setText("Вправо");
LeftButton.setText("Влево");
UpButton.setText("Вверх");
DownButton.setText("Вниз");
CreateButton.setText("Создать");
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
LocomotivePanel.add(CreateButton);
LocomotivePanel.add(RightButton);
LocomotivePanel.add(LeftButton);
LocomotivePanel.add(UpButton);
LocomotivePanel.add(DownButton);
LocomotiveFrame.add(LocomotivePanel, BorderLayout.CENTER);
Random random = new Random();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningLocomotive.Init(random.nextInt(300) + 100, random.nextDouble() * 3000.0 + 1000.0,
LocomotivePanel.getWidth(), LocomotivePanel.getHeight(), random.nextInt(5) + 2,
Color.BLACK,
Color.GREEN,
Color.BLACK,
LocomotivePanel);
DrawningLocomotive.DrawLoco();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Right);
DrawningLocomotive.DrawLoco();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Left);
DrawningLocomotive.DrawLoco();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Up);
DrawningLocomotive.DrawLoco();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Down);
DrawningLocomotive.DrawLoco();
}
});
LocomotiveFrame.setVisible(true);
}
}