98 lines
4.1 KiB
Java
98 lines
4.1 KiB
Java
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
import java.awt.event.ActionListener;
|
||
|
import java.util.Random;
|
||
|
public class PictureBoxBulldozer extends JPanel {
|
||
|
private DrawningBulldozer drawningBulldozer;
|
||
|
private JButton buttonLeft;
|
||
|
private JButton buttonUp;
|
||
|
private JButton buttonRight;
|
||
|
private JButton buttonDown;
|
||
|
private JButton buttonCreateBulldozer;
|
||
|
public PictureBoxBulldozer() {
|
||
|
setLayout(null);
|
||
|
setBounds(0, 0, 800, 450);
|
||
|
buttonCreateBulldozer = new JButton("Создать");
|
||
|
buttonCreateBulldozer.setFocusable(false);
|
||
|
buttonCreateBulldozer.setBounds(12, 415, 100, 30);
|
||
|
add(buttonCreateBulldozer);
|
||
|
buttonCreateBulldozer.addActionListener(e -> {
|
||
|
Random random = new Random();
|
||
|
drawningBulldozer = new DrawningBulldozer();
|
||
|
drawningBulldozer.Init(random.nextInt(100)+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)),
|
||
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||
|
random.nextBoolean(), random.nextBoolean(),
|
||
|
this.getWidth(), this.getHeight(), random.nextInt(3) + 1);
|
||
|
drawningBulldozer.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||
|
repaint();
|
||
|
});
|
||
|
ActionListener buttonMoveListener = e -> {
|
||
|
if (drawningBulldozer == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
String buttonName = ((JButton) e.getSource()).getName();
|
||
|
|
||
|
switch (buttonName) {
|
||
|
case ("buttonUp"):
|
||
|
drawningBulldozer.MoveTransport(DirectionBulldozer.Up);
|
||
|
break;
|
||
|
case ("buttonDown"):
|
||
|
drawningBulldozer.MoveTransport(DirectionBulldozer.Down);
|
||
|
break;
|
||
|
case ("buttonLeft"):
|
||
|
drawningBulldozer.MoveTransport(DirectionBulldozer.Left);
|
||
|
break;
|
||
|
case ("buttonRight"):
|
||
|
drawningBulldozer.MoveTransport(DirectionBulldozer.Right);
|
||
|
break;
|
||
|
}
|
||
|
repaint();
|
||
|
};
|
||
|
buttonLeft = new JButton();
|
||
|
buttonLeft.setName("buttonLeft");
|
||
|
buttonLeft.setFocusable(false);
|
||
|
buttonLeft.setPreferredSize(new Dimension(30, 30));
|
||
|
buttonLeft.setIcon(new ImageIcon("Resources/left.png"));
|
||
|
buttonLeft.addActionListener(buttonMoveListener);
|
||
|
buttonLeft.setBounds(686, 408, 30, 30);
|
||
|
add(buttonLeft);
|
||
|
buttonRight = new JButton();
|
||
|
buttonRight.setName("buttonRight");
|
||
|
buttonRight.setFocusable(false);
|
||
|
buttonRight.setPreferredSize(new Dimension(30, 30));
|
||
|
buttonRight.setIcon(new ImageIcon("Resources/right.png"));
|
||
|
buttonRight.addActionListener(buttonMoveListener);
|
||
|
buttonRight.setBounds(758, 408, 30, 30);
|
||
|
add(buttonRight);
|
||
|
buttonDown = new JButton();
|
||
|
buttonDown.setName("buttonDown");
|
||
|
buttonDown.setFocusable(false);
|
||
|
buttonDown.setPreferredSize(new Dimension(30, 30));
|
||
|
buttonDown.setIcon(new ImageIcon("Resources/down.png"));
|
||
|
buttonDown.addActionListener(buttonMoveListener);
|
||
|
buttonDown.setBounds(722, 408, 30, 30);
|
||
|
add(buttonDown);
|
||
|
buttonUp = new JButton();
|
||
|
buttonUp.setName("buttonUp");
|
||
|
buttonUp.setFocusable(false);
|
||
|
buttonUp.setPreferredSize(new Dimension(30, 30));
|
||
|
buttonUp.setIcon(new ImageIcon("Resources/up.png"));
|
||
|
buttonUp.addActionListener(buttonMoveListener);
|
||
|
buttonUp.setBounds(722, 372, 30, 30);
|
||
|
add(buttonUp);
|
||
|
setPreferredSize(new Dimension(800, 450));
|
||
|
}
|
||
|
@Override
|
||
|
protected void paintComponent(Graphics g) {
|
||
|
if (drawningBulldozer == null) {
|
||
|
return;
|
||
|
}
|
||
|
super.paintComponent(g);
|
||
|
Graphics2D g2d = (Graphics2D) g;
|
||
|
drawningBulldozer.DrawTransport(g2d);
|
||
|
}
|
||
|
}
|