118 lines
5.0 KiB
Java
118 lines
5.0 KiB
Java
package frames;
|
|
|
|
import drawing_objects.DrawingLainer;
|
|
import drawing_objects.DirectionType;
|
|
import drawing_objects.DrawingLainer;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.Random;
|
|
|
|
public class FrameLainer extends JFrame {
|
|
private DrawingLainer drawingLainer;
|
|
private JComponent pictureBox;
|
|
public FrameLainer() throws IOException {
|
|
super("лайнер");
|
|
setSize(new Dimension(900,480));
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
pictureBox = new JComponent(){
|
|
public void paintComponent(Graphics graphics){
|
|
super.paintComponent(graphics);
|
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
|
if (drawingLainer != null) drawingLainer.drawTransport(graphics2D);
|
|
super.repaint();
|
|
}
|
|
};
|
|
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
|
JButton createButton = new JButton("Создать");
|
|
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
|
|
rightButton.setPreferredSize(new Dimension(30,30));
|
|
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
|
|
leftButton.setPreferredSize(new Dimension(30,30));
|
|
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
|
|
upButton.setPreferredSize(new Dimension(30,30));
|
|
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
|
|
downButton.setPreferredSize(new Dimension(30,30));
|
|
|
|
createButton.addActionListener(e -> buttonCreateClick());
|
|
rightButton.setActionCommand("right");
|
|
rightButton.addActionListener(this::buttonMoveClick);
|
|
leftButton.setActionCommand("left");
|
|
leftButton.addActionListener(this::buttonMoveClick);
|
|
upButton.setActionCommand("up");
|
|
upButton.addActionListener(this::buttonMoveClick);
|
|
downButton.setActionCommand("down");
|
|
downButton.addActionListener(this::buttonMoveClick);
|
|
|
|
JPanel panelLainer = new JPanel(new BorderLayout());
|
|
JPanel createPanel = new JPanel(new BorderLayout());
|
|
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
|
|
JPanel movementPanel = new JPanel(new GridBagLayout());
|
|
JPanel rightPanel = new JPanel(new BorderLayout());
|
|
GridBagConstraints constraints = new GridBagConstraints();
|
|
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
|
|
|
createPanel.add(createButton, BorderLayout.SOUTH);
|
|
|
|
constraints.gridx = 2;
|
|
constraints.gridy = 1;
|
|
movementPanel.add(rightButton, constraints);
|
|
constraints.gridx = 0;
|
|
constraints.gridy = 1;
|
|
movementPanel.add(leftButton, constraints);
|
|
constraints.gridx = 1;
|
|
constraints.gridy = 0;
|
|
movementPanel.add(upButton, constraints);
|
|
constraints.gridx = 1;
|
|
constraints.gridy = 1;
|
|
movementPanel.add(downButton, constraints);
|
|
|
|
setLayout(new BorderLayout());
|
|
add(pictureBox);
|
|
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
|
panelLainer.add(rightPanel, BorderLayout.EAST);
|
|
panelLainer.add(createPanel, BorderLayout.WEST);
|
|
add(panelLainer,BorderLayout.CENTER);
|
|
setVisible(true);
|
|
}
|
|
private void buttonCreateClick() {
|
|
Random random = new Random();
|
|
drawingLainer = new DrawingLainer();
|
|
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
|
drawingLainer.init(random.nextInt(200) + 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)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(4));
|
|
drawingLainer.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
|
draw();
|
|
}
|
|
private void buttonMoveClick(ActionEvent event) {
|
|
if(drawingLainer == null || drawingLainer.getEntityLainer() == null)
|
|
return;
|
|
switch (event.getActionCommand()) {
|
|
case "left" :
|
|
drawingLainer.moveTransport(DirectionType.LEFT);
|
|
break;
|
|
case "right" :
|
|
drawingLainer.moveTransport(DirectionType.RIGHT);
|
|
break;
|
|
case "up" :
|
|
drawingLainer.moveTransport(DirectionType.UP);
|
|
break;
|
|
case "down" :
|
|
drawingLainer.moveTransport(DirectionType.DOWN);
|
|
break;
|
|
}
|
|
draw();
|
|
}
|
|
private void draw() {
|
|
if (drawingLainer == null)
|
|
{
|
|
return;
|
|
}
|
|
pictureBox.repaint();
|
|
}
|
|
} |