97 lines
3.1 KiB
Java
97 lines
3.1 KiB
Java
import javax.swing.*;
|
||
import javax.swing.border.LineBorder;
|
||
import java.awt.*;
|
||
import java.awt.event.ComponentAdapter;
|
||
import java.awt.event.ComponentEvent;
|
||
import java.util.Random;
|
||
|
||
public class FormAircraft implements Form {
|
||
private JButton createButton;
|
||
private JButton upButton;
|
||
private JButton rightButton;
|
||
private JButton downButton;
|
||
private JButton leftButton;
|
||
private JPanel mainPanel;
|
||
private JPanel DrawPlace;
|
||
private JLabel speedLabel;
|
||
private JLabel weightLabel;
|
||
private JLabel colorLabel;
|
||
|
||
DrawingAircraft _airFighter;
|
||
|
||
|
||
private JFrame jframe = getFrame();
|
||
|
||
private JFrame getFrame() {
|
||
JFrame frame = new JFrame();
|
||
frame.setVisible(true);
|
||
frame.setBounds(300, 100, 800, 600);
|
||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
return frame;
|
||
}
|
||
|
||
public void run() {
|
||
jframe.add(mainPanel);
|
||
Canvas canv = new Canvas(this);
|
||
DrawPlace.add(canv);
|
||
|
||
|
||
createButton.addActionListener(e -> {
|
||
Dimension canvSize = canv.getSize();
|
||
Random rnd = new Random();
|
||
|
||
_airFighter = new DrawingAircraft();
|
||
|
||
_airFighter.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
|
||
|
||
_airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height);
|
||
|
||
Color bodyColor = _airFighter.AirFighter.BodyColor;
|
||
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
|
||
|
||
speedLabel.setText("Скорость: " + _airFighter.AirFighter.Speed + " ");
|
||
weightLabel.setText("Вес: " + _airFighter.AirFighter.Weight + " ");
|
||
colorLabel.setText("Цвет: " + colorString);
|
||
|
||
canv.repaint();
|
||
});
|
||
|
||
jframe.addComponentListener(new ComponentAdapter() {
|
||
@Override
|
||
public void componentResized(ComponentEvent e) {
|
||
if(_airFighter == null) return;
|
||
_airFighter.ChangeBorders(canv.getSize().width, canv.getSize().height);
|
||
jframe.revalidate();
|
||
}
|
||
});
|
||
|
||
upButton.addActionListener(e -> {
|
||
if(_airFighter == null) return;
|
||
_airFighter.MoveTransport(Direction.Up);
|
||
canv.repaint();
|
||
});
|
||
rightButton.addActionListener(e -> {
|
||
if(_airFighter == null) return;
|
||
_airFighter.MoveTransport(Direction.Right);
|
||
canv.repaint();
|
||
});
|
||
downButton.addActionListener(e -> {
|
||
if(_airFighter == null) return;
|
||
_airFighter.MoveTransport(Direction.Down);
|
||
canv.repaint();
|
||
});
|
||
leftButton.addActionListener(e -> {
|
||
if(_airFighter == null) return;
|
||
_airFighter.MoveTransport(Direction.Left);
|
||
canv.repaint();
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void Draw(Graphics2D g) {
|
||
if(_airFighter == null) return;
|
||
_airFighter.DrawTransport(g);
|
||
}
|
||
}
|