PIbd-21_Eliseev_E.E._Airbus.../Project/src/FormPlaneConfig.java

242 lines
8.2 KiB
Java
Raw Normal View History

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
2022-11-18 18:38:30 +04:00
import java.util.function.Consumer;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
2022-11-18 18:38:30 +04:00
public class FormPlaneConfig extends JFrame{
private JButton ButtonAddObject;
private JButton ButtonCancel;
private JPanel GroupBoxConfig;
private JPanel PanelObject;
private JPanel GroupBoxColors;
private JPanel PanelRed;
private JPanel PanelGreen;
private JPanel PanelBlue;
private JPanel PanelYellow;
private JPanel PanelPurple;
private JPanel PanelBlack;
private JPanel PanelGray;
private JPanel PanelWhite;
private JLabel LabelSimpleObject;
private JLabel LabelModifiedObject;
private JLabel LabelBaseColor;
private JLabel LabelAddColor;
private JLabel LabelSpeed;
private JLabel LabelWeight;
private JSpinner NumericUpDownWeight;
private JSpinner NumericUpDownSpeed;
private JCheckBox CheckBoxAddСompartment;
private JCheckBox CheckBoxAddEngine;
private JPanel GroupBoxSelectPlaneWindow;
private JPanel GroubBoxSelectedCountWindow;
private JSpinner NumericUpDownCountWindow;
private JPanel MainPanel;
private JLabel LabelTypeWindow;
private JLabel LabelSimpleWindow;
private JLabel LabelRectWindow;
private JLabel LabelTriangleWindow;
2022-11-18 18:38:30 +04:00
private JPanel PictureBoxPlane;
//переменная-выбранный самолёт
DrawingPlane _plane;
//событие
Consumer<DrawingPlane> EventAddPlane;
public void AddEvent(Consumer<DrawingPlane> ev){
EventAddPlane = ev;
}
//конструктор
public FormPlaneConfig()
{
2022-11-18 18:38:30 +04:00
super("Самолёт");
CreateWindow();
}
public void Draw(DrawingPlane _plane)
{
2022-11-18 18:38:30 +04:00
PictureBoxPlane.removeAll();
BufferedImage bmp = new BufferedImage(250, 200, BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, 250, 200);
if (_plane.GetPlane() != null) {
_plane.DrawTransport(gr);
JLabel imageOfLogo = new JLabel();
2022-11-18 18:38:30 +04:00
imageOfLogo.setPreferredSize(PictureBoxPlane.getSize());
imageOfLogo.setMinimumSize(new Dimension(1, 1));
imageOfLogo.setIcon(new ImageIcon(bmp));
2022-11-18 18:38:30 +04:00
PictureBoxPlane.add(imageOfLogo, BorderLayout.CENTER);
}
validate();
}
public void CreateWindow()
{
setPreferredSize(new Dimension(1000, 700));
getContentPane().add(MainPanel);
//рамки у Label
LabelAddColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelTypeWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelModifiedObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelSimpleObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelSimpleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelRectWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
LabelTriangleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
//настройка значений у NumericUpDown
NumericUpDownSpeed.setModel(new SpinnerNumberModel(1000, 1000, 3000, 1));
NumericUpDownWeight.setModel(new SpinnerNumberModel(750, 100, 1500, 1));
NumericUpDownCountWindow.setModel(new SpinnerNumberModel(1, 1, 3, 1));
MouseAdapter drag = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseReleased(MouseEvent e) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Drop((JComponent) e.getSource());
}
};
MouseAdapter defCursor = new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
};
//настройки курсоров
2022-11-18 18:38:30 +04:00
PictureBoxPlane.addMouseListener(defCursor);
LabelBaseColor.addMouseListener(defCursor);
LabelAddColor.addMouseListener(defCursor);
LabelTypeWindow.addMouseListener(defCursor);
PanelBlack.addMouseListener(drag);
PanelPurple.addMouseListener(drag);
PanelGray.addMouseListener(drag);
PanelGreen.addMouseListener(drag);
PanelRed.addMouseListener(drag);
PanelWhite.addMouseListener(drag);
PanelYellow.addMouseListener(drag);
PanelBlue.addMouseListener(drag);
LabelSimpleObject.addMouseListener(drag);
LabelModifiedObject.addMouseListener(drag);
LabelSimpleWindow.addMouseListener(drag);
LabelRectWindow.addMouseListener(drag);
LabelTriangleWindow.addMouseListener(drag);
ButtonAddObject.addActionListener(e -> {
EventAddPlane.accept(_plane);
dispose();
});
ButtonCancel.addActionListener(e -> dispose());
}
public void Drop(JComponent dropItem)
{
if(dropItem == null)
{
return;
}
if(dropItem instanceof JPanel panel)
{
if(_plane == null)
{
return;
}
if(LabelBaseColor.getMousePosition() != null)
{
_plane.SetColor(panel.getBackground());
Draw(_plane);
2022-11-18 18:38:30 +04:00
repaint();
}
if(LabelAddColor.getMousePosition() != null && _plane instanceof DrawingAirbus airbus)
{
airbus.SetAddColor(panel.getBackground());
Draw(_plane);
2022-11-18 18:38:30 +04:00
repaint();
}
}
2022-11-18 18:38:30 +04:00
if(dropItem instanceof JLabel label && PictureBoxPlane.getMousePosition() != null)
{
int speed = (int)NumericUpDownSpeed.getValue();
int weight = (int)NumericUpDownWeight.getValue();
int countWindow = (int)NumericUpDownCountWindow.getValue();
boolean addCompartment = CheckBoxAddСompartment.isSelected();
boolean addEngine = CheckBoxAddEngine.isSelected();
if(label == LabelSimpleObject)
{
try
{
_plane = new DrawingPlane(speed, weight, Color.WHITE, countWindow);
Draw(_plane);
2022-11-18 18:38:30 +04:00
repaint();
}
catch(Exception ex) { }
}
else if(label == LabelModifiedObject)
{
try
{
_plane = new DrawingAirbus(speed, weight, Color.WHITE, countWindow, Color.WHITE, addCompartment, addEngine);
Draw(_plane);
2022-11-18 18:38:30 +04:00
repaint();
}
catch (Exception ex) { };
}
if(_plane != null)
{
2022-11-18 18:38:30 +04:00
_plane.SetPosition(PictureBoxPlane.getWidth() - 200, PictureBoxPlane.getHeight() - 150, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
PictureBoxPlane.add(_plane, BorderLayout.CENTER);
Draw(_plane);
2022-11-18 18:38:30 +04:00
repaint();
revalidate();
}
}
2022-11-18 18:38:30 +04:00
else if(dropItem instanceof JLabel label && LabelTypeWindow.getMousePosition() != null && _plane!=null)
{
if(label == LabelSimpleWindow)
{
_plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingAirplaneWindow());
Draw(_plane);
}
if(label == LabelRectWindow)
{
_plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingRectAirplaneWindow());
Draw(_plane);
}
if(label == LabelTriangleWindow)
{
_plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingTriangleAirplaneWindow());
Draw(_plane);
}
2022-11-18 18:38:30 +04:00
repaint();
}
}
}