PIbd-14_Antonova_A.A.__Hard/FormDumpTruck.java
2024-05-09 20:19:53 +04:00

130 lines
5.5 KiB
Java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import java.util.Random;
public class FormDumpTruck extends JFrame{
private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck();
// Размер формы
private Dimension dimension;
// Название формы
private String title;
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 FormDumpTruck(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Form() {
CreateButton.setName("CREATE");
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UpButton.setName("UP");
Icon iconUp = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowUp.png");
UpButton.setIcon(iconUp);
DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowDown.png");
DownButton.setIcon(iconDown);
LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowLeft.png");
LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_canvasDumpTruck._drawningDumpTruck = new DrawningDumpTruck();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
_canvasDumpTruck._drawningDumpTruck.Init(random.nextInt(1000, 3000), random.nextInt(1000, 3000),
bodyColor, additionalColor,
random.nextBoolean(), random.nextBoolean());
_canvasDumpTruck._drawningDumpTruck.SetPictureSize(getWidth(), getHeight());
_canvasDumpTruck._drawningDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
_canvasDumpTruck.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (_canvasDumpTruck._drawningDumpTruck == null) return;
boolean result = false;
switch ((((JButton) (event.getSource())).getName())) {
case "UP":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Right);
break;
}
if (result) {
_canvasDumpTruck.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width, dimension.height);
setLayout(null);
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 160, 40);
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 60, 60);
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(_canvasDumpTruck);
pack();
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
int Width = getWidth();
int Height = getHeight();
if (_canvasDumpTruck._drawningDumpTruck != null)
_canvasDumpTruck._drawningDumpTruck.SetPictureSize(Width, Height);
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 160, 40);
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
DownButton.setBounds(getWidth() - 150, getHeight() - 100, 60, 60);
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
}
});
}
}