2022-12-05 18:41:18 +04:00

154 lines
4.0 KiB
Java

package Form;
import Classes.Direction;
import Classes.DrawingAircraft;
import Classes.DrawingMilitaryAircraft;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormAirFighter extends JDialog
{
private DrawingAircraft _aircraft;
public DrawingAircraft SelectedAircraft;
private JPanel Form;
private JPanel label;
private JPanel pictureBox;
private JTextArea speedLabel;
private JTextArea weightLabel;
private JTextArea colorLabel;
private JButton moveRight;
private JButton moveDown;
private JButton moveLeft;
private JButton moveUp;
private JButton btnCreate;
private JButton btnModification;
private JButton SelectButton;
private void moveButtonClick(ActionEvent event) {
if (_aircraft == null) return;
String name = ((JButton)event.getSource()).getName();
switch (name) {
case "left" -> {
_aircraft.MoveTransport(Direction.Left);
}
case "right" -> {
_aircraft.MoveTransport(Direction.Right);
}
case "up" -> {
_aircraft.MoveTransport(Direction.Up);
}
case "down" -> {
_aircraft.MoveTransport(Direction.Down);
}
}
repaint();
}
private void SetData()
{
Random rnd = new Random();
_aircraft.SetPosition(
rnd.nextInt(0, 100),
rnd.nextInt(0, 100),
Form.getWidth(),
Form.getHeight()
);
speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed());
weightLabel.setText("Weight: " + _aircraft.Plane.getWeight());
colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor());
}
private void SetData(int width, int height)
{
Random rnd = new Random();
_aircraft.SetPosition(
rnd.nextInt(0, 100),
rnd.nextInt(0, 100),
width,
height
);
speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed());
weightLabel.setText("Weight: " + _aircraft.Plane.getWeight());
colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor());
}
private void CreateEntity() {
Random rnd = new Random();
Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
_aircraft = new DrawingAircraft(rnd.nextInt(10, 300),
rnd.nextFloat(1000, 2000),color);
SetData();
repaint();
}
private void CreateModifiedEntity()
{
Random rnd = new Random();
Color color = JColorChooser.showDialog(this, "Основной цвет", Color.white);
Color dopColor = JColorChooser.showDialog(this, "Дополнительный цвет", Color.white);
_aircraft = new DrawingMilitaryAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
color, dopColor, rnd.nextBoolean(), rnd.nextBoolean());
SetData();
repaint();
}
private void selectAircraft()
{
SelectedAircraft = _aircraft;
dispose();
}
public DrawingAircraft getSelectedAircraft() {
return SelectedAircraft;
}
private void resizeWindow() {
_aircraft.ChangeBorders(pictureBox.getWidth(), pictureBox.getHeight());
repaint();
}
public FormAirFighter() {
setContentPane(Form);
// action move //
ActionListener listener = this::moveButtonClick;
moveRight.addActionListener(listener);
moveDown.addActionListener(listener);
moveLeft.addActionListener(listener);
moveUp.addActionListener(listener);
btnCreate.addActionListener(e -> CreateEntity());
btnModification.addActionListener(e -> CreateModifiedEntity());
SelectButton.addActionListener(e -> selectAircraft());
}
public FormAirFighter(DrawingAircraft aircraft)
{
this();
_aircraft = aircraft;
SetData(_aircraft._pictureWidth, aircraft._pictureHeight);
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D graphics = (Graphics2D) pictureBox.getGraphics();
if(_aircraft != null)
{
_aircraft.DrawTransport(graphics);
}
}
}