171 lines
5.5 KiB
Java
171 lines
5.5 KiB
Java
package projectMonorail;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.Random;
|
|
|
|
public class PictureBox extends JPanel {
|
|
|
|
private DrawingMonorail drawingMonorail;
|
|
|
|
private JButton buttonLeft;
|
|
|
|
private JButton buttonUp;
|
|
|
|
private JButton buttonRight;
|
|
|
|
private JButton buttonDown;
|
|
|
|
private JButton buttonCreateMonorail;
|
|
|
|
private JPanel buttonsPanel;
|
|
|
|
private JPanel buttonsMovePanel;
|
|
|
|
private JPanel paddingPanel;
|
|
|
|
public PictureBox() {
|
|
setLayout(new BorderLayout());
|
|
|
|
buttonsPanel = new JPanel();
|
|
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
|
|
buttonsPanel.setOpaque(false);
|
|
|
|
buttonCreateMonorail = new JButton("Create");
|
|
buttonCreateMonorail.setFocusable(false);
|
|
buttonCreateMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
|
buttonCreateMonorail.setBackground(Color.LIGHT_GRAY);
|
|
buttonCreateMonorail.setPreferredSize(new Dimension(75, 23));
|
|
|
|
buttonCreateMonorail.addActionListener (e -> {
|
|
Random random = new Random();
|
|
drawingMonorail = new DrawingMonorail();
|
|
drawingMonorail.Init(random.nextInt(200, 300),
|
|
random.nextInt(1000, 3000),
|
|
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
|
random.nextInt(0, 256)),
|
|
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
|
random.nextInt(0, 256)),
|
|
random.nextBoolean(), random.nextBoolean(), this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
|
drawingMonorail.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
|
repaint();
|
|
});
|
|
|
|
ActionListener buttonMoveListener = e -> {
|
|
if (drawingMonorail == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
String buttonName = ((JButton) e.getSource()).getName();
|
|
|
|
switch (buttonName) {
|
|
case ("buttonUp"):
|
|
drawingMonorail.MoveTransport(DirectionType.Up);
|
|
break;
|
|
case ("buttonDown"):
|
|
drawingMonorail.MoveTransport(DirectionType.Down);
|
|
break;
|
|
case ("buttonLeft"):
|
|
drawingMonorail.MoveTransport(DirectionType.Left);
|
|
break;
|
|
case ("buttonRight"):
|
|
drawingMonorail.MoveTransport(DirectionType.Right);
|
|
break;
|
|
}
|
|
repaint();
|
|
};
|
|
|
|
buttonLeft = new JButton();
|
|
buttonLeft.setName("buttonLeft");
|
|
buttonLeft.setFocusable(false);
|
|
buttonLeft.setPreferredSize(new Dimension(30, 30));
|
|
buttonLeft.setIcon(new ImageIcon("Resources/arrowLeft.png"));
|
|
buttonLeft.addActionListener(buttonMoveListener);
|
|
|
|
buttonRight = new JButton();
|
|
buttonRight.setName("buttonRight");
|
|
buttonRight.setFocusable(false);
|
|
buttonRight.setPreferredSize(new Dimension(30, 30));
|
|
buttonRight.setIcon(new ImageIcon("Resources/arrowRight.png"));
|
|
buttonRight.addActionListener(buttonMoveListener);
|
|
|
|
buttonDown = new JButton();
|
|
buttonDown.setName("buttonDown");
|
|
buttonDown.setFocusable(false);
|
|
buttonDown.setPreferredSize(new Dimension(30, 30));
|
|
buttonDown.setIcon(new ImageIcon("Resources/arrowDown.png"));
|
|
buttonDown.addActionListener(buttonMoveListener);
|
|
|
|
buttonUp = new JButton();
|
|
buttonUp.setName("buttonUp");
|
|
buttonUp.setFocusable(false);
|
|
buttonUp.setPreferredSize(new Dimension(30, 30));
|
|
buttonUp.setIcon(new ImageIcon("Resources/arrowUp.png"));
|
|
buttonUp.addActionListener(buttonMoveListener);
|
|
|
|
buttonsMovePanel = new JPanel();
|
|
buttonsMovePanel.setLayout(new GridBagLayout());
|
|
buttonsMovePanel.setOpaque(false);
|
|
|
|
GridBagConstraints constrains = new GridBagConstraints();
|
|
constrains.insets = new Insets(5,5,5,5);
|
|
constrains.gridx = 0;
|
|
constrains.gridy = 0;
|
|
|
|
buttonsMovePanel.add(Box.createHorizontalStrut(30), constrains);
|
|
|
|
constrains.gridx = 1;
|
|
constrains.gridy = 0;
|
|
|
|
buttonsMovePanel.add(buttonUp, constrains);
|
|
|
|
constrains.gridx = 2;
|
|
constrains.gridy = 0;
|
|
|
|
buttonsMovePanel.add(Box.createHorizontalStrut(30), constrains);
|
|
|
|
constrains.gridx = 0;
|
|
constrains.gridy = 1;
|
|
|
|
buttonsMovePanel.add(buttonLeft, constrains);
|
|
|
|
constrains.gridx = 1;
|
|
constrains.gridy = 1;
|
|
|
|
buttonsMovePanel.add(buttonDown, constrains);
|
|
|
|
constrains.gridx = 2;
|
|
constrains.gridy = 1;
|
|
|
|
buttonsMovePanel.add(buttonRight, constrains);
|
|
|
|
paddingPanel = new JPanel();
|
|
paddingPanel.setLayout(new BoxLayout(paddingPanel, BoxLayout.Y_AXIS));
|
|
paddingPanel.setOpaque(false);
|
|
|
|
paddingPanel.add(buttonsMovePanel);
|
|
paddingPanel.add(Box.createVerticalStrut(15));
|
|
|
|
buttonsPanel.add(Box.createHorizontalStrut(20));
|
|
buttonsPanel.add(buttonCreateMonorail);
|
|
buttonsPanel.add(Box.createHorizontalStrut(650));
|
|
buttonsPanel.add(paddingPanel);
|
|
|
|
add(buttonsPanel, BorderLayout.SOUTH);
|
|
setPreferredSize(new Dimension(900, 500));
|
|
}
|
|
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
if (drawingMonorail == null) {
|
|
return;
|
|
}
|
|
|
|
super.paintComponent(g);
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
drawingMonorail.DrawTransport(g2d);
|
|
}
|
|
}
|