PIbd-12_Karamushko_M.K._Air.../FormAircraft.java

96 lines
3.0 KiB
Java
Raw Normal View History

2022-10-28 15:45:35 +04:00
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 {
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();
});
}
public void draw(Graphics2D g) {
if(_airFighter == null) return;
_airFighter.DrawTransport(g);
}
}