Pibd-22_Emelyanov_A.S._Airb.../FormPlane.java

140 lines
5.4 KiB
Java
Raw Normal View History

2022-11-16 16:44:07 +04:00
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
2022-11-16 16:44:07 +04:00
import java.util.Random;
2022-11-29 00:13:32 +04:00
import static java.lang.Boolean.parseBoolean;
2022-11-30 19:25:43 +04:00
public class FormPlane extends JDialog{
2022-11-16 16:44:07 +04:00
public JPanel Mainpanel;
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
protected DrawningPlane _plane;
private JPanel PictureBox;
2022-11-16 16:44:07 +04:00
private JToolBar StatusStrip;
2022-11-29 00:13:32 +04:00
private JButton ButtonModif;
2022-11-30 19:25:43 +04:00
private JButton ButtonSelectPlane;
2022-11-27 15:16:31 +04:00
private final JLabel JLabelSpeed = new JLabel();
private final JLabel JLabelWeight = new JLabel();
private final JLabel JLabelColor = new JLabel();
2022-11-30 19:25:43 +04:00
private DrawningPlane SelectedPlane;
public DrawningPlane GetSelectedPlane() {
return SelectedPlane;
}
2022-11-16 16:44:07 +04:00
public void Draw() {
PictureBox.removeAll();
BufferedImage bmp = new BufferedImage(PictureBox.getWidth(), PictureBox.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
if (_plane != null) {
_plane.DrawTransport(gr);
JLabel imageOfPlane = new JLabel();
imageOfPlane.setPreferredSize(PictureBox.getSize());
imageOfPlane.setMinimumSize(new Dimension(1, 1));
imageOfPlane.setIcon(new ImageIcon(bmp));
PictureBox.add(imageOfPlane,BorderLayout.CENTER);
2022-11-16 16:44:07 +04:00
}
validate();
2022-11-16 16:44:07 +04:00
}
2022-11-29 00:13:32 +04:00
private void SetData(){
Random random = new Random();
_plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBox.getWidth(), PictureBox.getHeight());
JLabelSpeed.setText("орость: " + _plane.GetPlane().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " "));
}
2022-12-14 15:30:36 +04:00
private void CreateWindow(){
2022-11-30 19:25:43 +04:00
add(Mainpanel);
2022-11-16 16:44:07 +04:00
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
StatusStrip.add(LabelBox);
try {
Image img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowUp.jpg"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowDown.jpg"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowLeft.jpg"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowRight.jpg"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ButtonCreate.addActionListener(e -> {
Random random = new Random();
2022-11-30 19:25:43 +04:00
Color color = JColorChooser.showDialog(null, "Цвет", null);
_plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),color);
2022-11-29 00:13:32 +04:00
SetData();
Draw();
});
ButtonModif.addActionListener(e -> {
Random random = new Random();
2022-11-30 19:25:43 +04:00
Color first = JColorChooser.showDialog(null, "Цвет", null);
Color second = JColorChooser.showDialog(null, "Цвет", null);
_plane = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), first, second, random.nextBoolean(), random.nextBoolean(), random.nextBoolean());
2022-11-29 00:13:32 +04:00
SetData();
2022-11-16 16:44:07 +04:00
Draw();
});
PictureBox.addComponentListener(new ComponentAdapter() {
2022-11-16 16:44:07 +04:00
@Override
public void componentResized(ComponentEvent e) {
if(_plane == null || _plane.GetPlane() == null) return;
2022-11-16 16:44:07 +04:00
super.componentResized(e);
_plane.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight());
PictureBox.revalidate();
2022-11-16 16:44:07 +04:00
Draw();
}
});
ButtonUp.addActionListener(e -> {
_plane.MoveTransport(Direction.Up);
PictureBox.revalidate();
2022-11-16 16:44:07 +04:00
Draw();
});
ButtonDown.addActionListener(e -> {
_plane.MoveTransport(Direction.Down);
PictureBox.revalidate();
2022-11-16 16:44:07 +04:00
Draw();
});
ButtonRight.addActionListener(e -> {
_plane.MoveTransport(Direction.Right);
PictureBox.revalidate();
2022-11-16 16:44:07 +04:00
Draw();
});
ButtonLeft.addActionListener(e -> {
_plane.MoveTransport(Direction.Left);
PictureBox.revalidate();
2022-11-16 16:44:07 +04:00
Draw();
});
2022-11-30 19:25:43 +04:00
ButtonSelectPlane.addActionListener(e -> {
SelectedPlane = _plane;
setVisible(false);
dispose();
});
2022-11-16 16:44:07 +04:00
}
2022-12-14 15:30:36 +04:00
public FormPlane() {
super(new Frame("Самолёт"));
CreateWindow();
setModal(true);
getContentPane().add(Mainpanel);
}
public FormPlane(DrawningObjectPlane plane){
super(new Frame("Корабль"));
CreateWindow();
setModal(true);
_plane = plane.GetDrawningObjectPlane();
getContentPane().add(Mainpanel);
}
2022-11-30 19:25:43 +04:00
2022-11-16 16:44:07 +04:00
}