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

242 lines
8.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.function.Consumer;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
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;
private JPanel PictureBoxPlane;
//переменная-выбранный самолёт
DrawingPlane _plane;
//событие
Consumer<DrawingPlane> EventAddPlane;
public void AddEvent(Consumer<DrawingPlane> ev){
EventAddPlane = ev;
}
//конструктор
public FormPlaneConfig()
{
super("Самолёт");
CreateWindow();
}
public void Draw(DrawingPlane _plane)
{
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();
imageOfLogo.setPreferredSize(PictureBoxPlane.getSize());
imageOfLogo.setMinimumSize(new Dimension(1, 1));
imageOfLogo.setIcon(new ImageIcon(bmp));
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));
}
};
//настройки курсоров
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);
repaint();
}
if(LabelAddColor.getMousePosition() != null && _plane instanceof DrawingAirbus airbus)
{
airbus.SetAddColor(panel.getBackground());
Draw(_plane);
repaint();
}
}
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);
repaint();
}
catch(Exception ex) { }
}
else if(label == LabelModifiedObject)
{
try
{
_plane = new DrawingAirbus(speed, weight, Color.WHITE, countWindow, Color.WHITE, addCompartment, addEngine);
Draw(_plane);
repaint();
}
catch (Exception ex) { };
}
if(_plane != null)
{
_plane.SetPosition(PictureBoxPlane.getWidth() - 200, PictureBoxPlane.getHeight() - 150, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
PictureBoxPlane.add(_plane, BorderLayout.CENTER);
Draw(_plane);
repaint();
revalidate();
}
}
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);
}
repaint();
}
}
}