107 lines
3.7 KiB
Java
107 lines
3.7 KiB
Java
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
import java.awt.event.*;
|
||
|
import java.util.Random;
|
||
|
|
||
|
public class FormPlane extends JComponent
|
||
|
{
|
||
|
private DrawingPlane _plane;
|
||
|
private EntityPlane _entity;
|
||
|
|
||
|
public FormPlane()
|
||
|
{
|
||
|
JFrame formFrame = new JFrame("Plane");
|
||
|
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
|
formFrame.setSize(840, 560);
|
||
|
formFrame.setVisible(true);
|
||
|
formFrame.setLocationRelativeTo(null);
|
||
|
|
||
|
formFrame.addComponentListener(new ComponentListener()
|
||
|
{
|
||
|
@Override
|
||
|
public void componentResized(ComponentEvent e)
|
||
|
{
|
||
|
if (_plane != null) _plane.ChangeBorders(formFrame.getWidth(), formFrame.getHeight());
|
||
|
repaint();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void componentMoved(ComponentEvent e) {}
|
||
|
|
||
|
@Override
|
||
|
public void componentShown(ComponentEvent e) {}
|
||
|
|
||
|
@Override
|
||
|
public void componentHidden(ComponentEvent e) {}
|
||
|
|
||
|
});
|
||
|
Panel statusPanel = new Panel();
|
||
|
statusPanel.setBackground(Color.WHITE);
|
||
|
statusPanel.setLayout(new GridLayout());
|
||
|
setLayout(new BorderLayout());
|
||
|
add(statusPanel, BorderLayout.SOUTH);
|
||
|
|
||
|
Label speedLabel = new Label("Speed:");
|
||
|
Label weightLabel = new Label("Weight:");
|
||
|
Label colorLabel = new Label("Color:");
|
||
|
|
||
|
JButton createButton = new JButton("Create");
|
||
|
createButton.addActionListener(e -> {
|
||
|
Random rand = new Random();
|
||
|
_plane = new DrawingPlane();
|
||
|
_entity = new EntityPlane();
|
||
|
_plane.Init(300 + rand.nextInt( 100), 1000 + rand.nextInt(1000), Color.getHSBColor(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextInt(3), _entity);
|
||
|
_plane.SetPosition(10 + rand.nextInt(90), 10 + rand.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
|
||
|
speedLabel.setText("Speed: " + _plane.Plane.getSpeed());
|
||
|
weightLabel.setText("Weight: " + (int)_plane.Plane.getWeight());
|
||
|
colorLabel.setText("Color: " + _plane.Plane.getBodyColor().getRed() + " " + _plane.Plane.getBodyColor().getGreen() + " " + _plane.Plane.getBodyColor().getBlue() );
|
||
|
repaint();
|
||
|
});
|
||
|
|
||
|
statusPanel.add(createButton);
|
||
|
statusPanel.add(speedLabel);
|
||
|
statusPanel.add(weightLabel);
|
||
|
statusPanel.add(colorLabel);
|
||
|
|
||
|
JButton moveRightButton = new JButton(">");
|
||
|
moveRightButton.addActionListener(e -> {
|
||
|
if (_plane != null) _plane.MoveTransport(Direction.Right);
|
||
|
repaint();
|
||
|
});
|
||
|
JButton moveLeftButton = new JButton("<");
|
||
|
moveLeftButton.addActionListener(e -> {
|
||
|
if (_plane != null) _plane.MoveTransport(Direction.Left);
|
||
|
repaint();
|
||
|
});
|
||
|
JButton moveUpButton = new JButton("^");
|
||
|
moveUpButton.addActionListener(e -> {
|
||
|
if (_plane != null) _plane.MoveTransport(Direction.Up);
|
||
|
repaint();
|
||
|
});
|
||
|
JButton moveDownButton = new JButton("v");
|
||
|
moveDownButton.addActionListener(e -> {
|
||
|
if (_plane != null) _plane.MoveTransport(Direction.Down);
|
||
|
repaint();
|
||
|
});
|
||
|
|
||
|
statusPanel.add(moveUpButton);
|
||
|
statusPanel.add(moveDownButton);
|
||
|
statusPanel.add(moveLeftButton);
|
||
|
statusPanel.add(moveRightButton);
|
||
|
|
||
|
formFrame.getContentPane().add(this);
|
||
|
}
|
||
|
@Override
|
||
|
protected void paintComponent(Graphics g) {
|
||
|
super.paintComponent(g);
|
||
|
Graphics2D g2 = (Graphics2D)g;
|
||
|
if (_plane != null) _plane.DrawTransport(g2);
|
||
|
super.repaint();
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) throws Exception {
|
||
|
new FormPlane();
|
||
|
}
|
||
|
|
||
|
}
|