124 lines
5.7 KiB
Java
124 lines
5.7 KiB
Java
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
import java.awt.event.ActionEvent;
|
||
|
import java.awt.event.ActionListener;
|
||
|
import java.awt.event.ComponentAdapter;
|
||
|
import java.awt.event.ComponentEvent;
|
||
|
import java.util.Random;
|
||
|
|
||
|
public class FormExcavator extends JFrame {
|
||
|
private String title;
|
||
|
private Dimension dimension;
|
||
|
private int Width, Height;
|
||
|
private CanvasExcavator canvasExcavator = new CanvasExcavator();
|
||
|
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 FormExcavator(String title, Dimension dimension) {
|
||
|
this.title = title;
|
||
|
this.dimension = dimension;
|
||
|
}
|
||
|
public void Init() {
|
||
|
setTitle(title);
|
||
|
setMinimumSize(dimension);
|
||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
|
Width = getWidth() - 15;
|
||
|
Height = getHeight() - 35;
|
||
|
|
||
|
CreateButton.setName("Create");
|
||
|
Icon iconUp = new ImageIcon("res/up.png");
|
||
|
UpButton.setIcon(iconUp);
|
||
|
UpButton.setName("Up");
|
||
|
DownButton.setName("Down");
|
||
|
Icon iconDown = new ImageIcon("res/down.png");
|
||
|
DownButton.setIcon(iconDown);
|
||
|
LeftButton.setName("Left");
|
||
|
Icon iconLeft = new ImageIcon("res/left.png");
|
||
|
LeftButton.setIcon(iconLeft);
|
||
|
RightButton.setName("Right");
|
||
|
Icon iconRight = new ImageIcon("res/right.png");
|
||
|
RightButton.setIcon(iconRight);
|
||
|
|
||
|
CreateButton.addActionListener(new ActionListener() {
|
||
|
@Override
|
||
|
public void actionPerformed(ActionEvent e) {
|
||
|
int StartPositionX = (int)(Math.random() * 90 + 10);
|
||
|
int StartPositionY = (int)(Math.random() * 90 + 10);
|
||
|
int speed = (int)(Math.random() * 300 + 100);
|
||
|
double weight = (Math.random() * 3000 + 1000);
|
||
|
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||
|
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||
|
boolean sheepPipes = new Random().nextBoolean();
|
||
|
boolean fuelTank = new Random().nextBoolean();
|
||
|
canvasExcavator._drawingExcavator = new DrawingExcavator();
|
||
|
canvasExcavator._drawingExcavator.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
|
||
|
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
|
||
|
canvasExcavator._drawingExcavator.SetPosition( StartPositionX, StartPositionY);
|
||
|
canvasExcavator.repaint();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
ActionListener actionListener = new ActionListener() {
|
||
|
@Override
|
||
|
public void actionPerformed(ActionEvent event) {
|
||
|
if (canvasExcavator._drawingExcavator == null) return;
|
||
|
boolean result = false;
|
||
|
switch ((((JButton)(event.getSource())).getName())) {
|
||
|
case "Up":
|
||
|
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Up);
|
||
|
break;
|
||
|
case "Down":
|
||
|
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Down);
|
||
|
break;
|
||
|
case "Left":
|
||
|
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Left);
|
||
|
break;
|
||
|
case "Right":
|
||
|
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Right);
|
||
|
break;
|
||
|
}
|
||
|
if (result) {
|
||
|
canvasExcavator.repaint();
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
UpButton.addActionListener(actionListener);
|
||
|
DownButton.addActionListener(actionListener);
|
||
|
LeftButton.addActionListener(actionListener);
|
||
|
RightButton.addActionListener(actionListener);
|
||
|
|
||
|
setSize(dimension.width,dimension.height);
|
||
|
setLayout(null);
|
||
|
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
|
||
|
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
|
||
|
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
||
|
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
||
|
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
||
|
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
||
|
add(CreateButton);
|
||
|
add(UpButton);
|
||
|
add(DownButton);
|
||
|
add(RightButton);
|
||
|
add(LeftButton);
|
||
|
add(canvasExcavator);
|
||
|
setVisible(true);
|
||
|
//обработка события изменения размеров окна
|
||
|
addComponentListener(new ComponentAdapter() {
|
||
|
public void componentResized(ComponentEvent e) {
|
||
|
Width = getWidth() - 15;
|
||
|
Height = getHeight() - 35;
|
||
|
if (canvasExcavator._drawingExcavator != null)
|
||
|
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
|
||
|
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
|
||
|
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
|
||
|
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
||
|
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
||
|
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
||
|
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|