PIbd-23. Yashin A.A. Lab work 05 #5

Closed
Artyom_Yashin wants to merge 2 commits from Lab5 into Lab4
7 changed files with 426 additions and 23 deletions

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" project-jdk-name="21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,390 @@
package PlaneHard;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import drawing_objects.*;
public class FormPlaneConfig extends JFrame {
private static class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel)c).getText());
}
}
private record ColorTransferable(Color color) implements Transferable {
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{colorDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return colorDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private record IDrawEnginesTransferable(IDrawEngines IDrawEnginesObject) implements Transferable {
private static final DataFlavor IDrawEnginesDataFlavor = new DataFlavor(IDrawEngines.class, "IDrawEngines");
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{IDrawEnginesDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return IDrawEnginesDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return IDrawEnginesObject;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private static class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(c.getBackground());
}
}
private static class LabelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}
private static class PanelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}
private static class IDrawEnginesComponent extends JComponent{
public IDrawEngines obj;
public IDrawEnginesComponent(IDrawEngines obj){
this.obj = obj;
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((IDrawEnginesComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawEnginesComponent)e.getComponent()), e, TransferHandler.COPY);
}
}
);
this.setTransferHandler(
new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new IDrawEnginesTransferable(((IDrawEnginesComponent) c).obj);
}
}
);
}
}
private final JComponent pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingPlane != null) drawingPlane.drawTransport(graphics2D);
super.repaint();
}
};
public final JButton addButton = new JButton("Добавить");
public final JButton cancelButton = new JButton("Отмена");
public DrawingPlane drawingPlane;
private final int pictureBoxWidth = 218;
private final int pictureBoxHeight = 178;
public FormPlaneConfig(){
super("Создание объекта");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGui();
setVisible(true);
}
private void createGui(){
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
IDrawEnginesComponent engines = new IDrawEnginesComponent(new DrawingEnginesNotRounded());
IDrawEnginesComponent enginesRoundBack = new IDrawEnginesComponent(new DrawingEnginesRoundedBack());
IDrawEnginesComponent enginesRoundFront = new IDrawEnginesComponent(new DrawingEnginesRoundedFront());
JLabel squareLabel = new JLabel("Квадратные");
squareLabel.setHorizontalAlignment(SwingConstants.CENTER);
squareLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel roundedBackLabel = new JLabel("Круглые сзади");
roundedBackLabel.setHorizontalAlignment(SwingConstants.CENTER);
roundedBackLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel roundedFrontLabel = new JLabel("Круглые спереди");
roundedFrontLabel.setHorizontalAlignment(SwingConstants.CENTER);
roundedFrontLabel.setVerticalAlignment(SwingConstants.CENTER);
engines.setLayout(new GridLayout(1,1));
enginesRoundBack.setLayout(new GridLayout(1,1));
enginesRoundFront.setLayout(new GridLayout(1,1));
engines.add(squareLabel);
enginesRoundBack.add(roundedBackLabel);
enginesRoundFront.add(roundedFrontLabel);
engines.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
enginesRoundBack.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
enginesRoundFront.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel colorLabel = new JLabel("Цвет");
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel additionalColorLabel = new JLabel("Доп цвет");
additionalColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
additionalColorLabel.setVerticalAlignment(SwingConstants.CENTER);
additionalColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel IDrawEnginesLabel = new JLabel("Двигатели");
IDrawEnginesLabel.setHorizontalAlignment(SwingConstants.CENTER);
IDrawEnginesLabel.setVerticalAlignment(SwingConstants.CENTER);
IDrawEnginesLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JCheckBox checkBoxBomb = new JCheckBox("Наличие бомб");
JCheckBox checkBoxFuel = new JCheckBox("Наличие баков");
JLabel simpleLabel = new JLabel("Простой");
simpleLabel.setTransferHandler(new LabelTransferHandler());
simpleLabel.addMouseListener(new LabelMouseAdapter());
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel advancedLabel = new JLabel("Продвинутый");
advancedLabel.setTransferHandler(new LabelTransferHandler());
advancedLabel.addMouseListener(new LabelMouseAdapter());
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel speedLabel = new JLabel ("Скорость");
JLabel weightLabel = new JLabel ("Вес");
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel enginesNumberSpinnerModel = new SpinnerNumberModel(2, 2, 6, 2.0);
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
JSpinner enginesNumberSpinner = new JSpinner(enginesNumberSpinnerModel);
JPanel colorPanel = new JPanel();
JPanel redPanel = new JPanel();
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
JPanel yellowPanel = new JPanel();
JPanel whitePanel = new JPanel();
JPanel grayPanel = new JPanel();
JPanel blackPanel = new JPanel();
JPanel purplePanel = new JPanel();
redPanel.setTransferHandler(new PanelTransferHandler());
greenPanel.setTransferHandler(new PanelTransferHandler());
bluePanel.setTransferHandler(new PanelTransferHandler());
yellowPanel.setTransferHandler(new PanelTransferHandler());
whitePanel.setTransferHandler(new PanelTransferHandler());
grayPanel.setTransferHandler(new PanelTransferHandler());
blackPanel.setTransferHandler(new PanelTransferHandler());
purplePanel.setTransferHandler(new PanelTransferHandler());
redPanel.addMouseListener(new PanelMouseAdapter());
greenPanel.addMouseListener(new PanelMouseAdapter());
bluePanel.addMouseListener(new PanelMouseAdapter());
yellowPanel.addMouseListener(new PanelMouseAdapter());
whitePanel.addMouseListener(new PanelMouseAdapter());
grayPanel.addMouseListener(new PanelMouseAdapter());
blackPanel.addMouseListener(new PanelMouseAdapter());
purplePanel.addMouseListener(new PanelMouseAdapter());
redPanel.setName("Красный");
greenPanel.setName("Зелёный");
bluePanel.setName("Синий");
yellowPanel.setName("Жёлтый");
whitePanel.setName("Белый");
grayPanel.setName("Серый");
blackPanel.setName("Чёрный");
purplePanel.setName("Фиолетовый");
pictureBox.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
int speed = ((Number)speedSpinner.getValue()).intValue();
int weight = ((Number)weightSpinner.getValue()).intValue();
int enginesNumber = ((Number)enginesNumberSpinner.getValue()).intValue();
switch ((String)support.getTransferable().getTransferData(DataFlavor.stringFlavor)) {
case "Простой" -> {
drawingPlane = new DrawingPlane(speed, weight, Color.WHITE,
pictureBoxWidth, pictureBoxHeight, 0, enginesNumber);
drawingPlane.ChangeEnginesCol(enginesNumber);
}
case "Продвинутый" -> {
drawingPlane = new DrawingAirBomber(speed, weight, Color.WHITE, Color.BLACK,
checkBoxBomb.isSelected(), checkBoxFuel.isSelected(),
pictureBoxWidth, pictureBoxHeight, 0, enginesNumber);
drawingPlane.ChangeEnginesCol(enginesNumber);
}
}
drawingPlane.setPosition(pictureBoxWidth / 2 - drawingPlane.getWidth() / 2,
pictureBoxHeight / 2 - drawingPlane.getHeight() / 2);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
IDrawEnginesLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(IDrawEnginesTransferable.IDrawEnginesDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDrawEngines obj = (IDrawEngines) support.getTransferable().getTransferData(IDrawEnginesTransferable.IDrawEnginesDataFlavor);
obj.setNumber(((Number)enginesNumberSpinner.getValue()).intValue());
if (drawingPlane == null)
return false;
drawingPlane.ChangeIDraw(obj);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
colorLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawingPlane == null)
return false;
drawingPlane.ChangeColor(color);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
additionalColorLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawingPlane == null || !(drawingPlane instanceof DrawingAirBomber))
return false;
((DrawingAirBomber) drawingPlane).ChangeAddColor(color);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
addButton.setBounds(555,250,94,29);
cancelButton.setBounds(679,250,94,29);
pictureBox.setBounds(555, 65, pictureBoxWidth, pictureBoxHeight);
colorLabel.setBounds(555, 20, 70, 33);
additionalColorLabel.setBounds(629, 20, 70, 33);
IDrawEnginesLabel.setBounds(703, 20, 70, 33);
checkBoxBomb.setBounds(6, 132, 159, 24);
checkBoxFuel.setBounds(6, 162, 145, 24);
simpleLabel.setBounds(171,169, 120, 50);
advancedLabel.setBounds(297,169, 120, 50);
engines.setBounds(171,229, 120, 50);
enginesRoundBack.setBounds(297,229, 120, 50);
enginesRoundFront.setBounds(423,229, 120, 50);
colorPanel.setBounds(171, 23, 372,143);
speedSpinner.setBounds(6, 46, 150, 27);
speedLabel.setBounds(6, 23, 73, 20);
weightSpinner.setBounds(6, 99, 150, 27);
weightLabel.setBounds(6, 76, 33, 20);
enginesNumberSpinner.setBounds(6, 200, 150, 27);
redPanel.setBackground(Color.RED);
greenPanel.setBackground(Color.GREEN);
bluePanel.setBackground(Color.BLUE);
yellowPanel.setBackground(Color.YELLOW);
whitePanel.setBackground(Color.WHITE);
grayPanel.setBackground(Color.GRAY);
blackPanel.setBackground(Color.BLACK);
purplePanel.setBackground(Color.MAGENTA);
colorPanel.setLayout(new GridLayout(2, 4, 26, 10));
colorPanel.add(redPanel);
colorPanel.add(greenPanel);
colorPanel.add(bluePanel);
colorPanel.add(yellowPanel);
colorPanel.add(whitePanel);
colorPanel.add(grayPanel);
colorPanel.add(blackPanel);
colorPanel.add(purplePanel);
add(colorLabel);
add(additionalColorLabel);
add(IDrawEnginesLabel);
setLayout(null);
setSize(818, 350);
add(speedLabel);
add(speedSpinner);
add(weightLabel);
add(weightSpinner);
add(simpleLabel);
add(advancedLabel);
add(checkBoxBomb);
add(checkBoxFuel);
add(pictureBox);
add(addButton);
add(cancelButton);
add(enginesNumberSpinner);
add(colorPanel);
add(engines);
add(enginesRoundBack);
add(enginesRoundFront);
}
}

View File

@ -13,6 +13,9 @@ public class DrawingAirBomber extends DrawingPlane {
entityPlane = new EntityAirBomber(speed, weight, bodyColor, additionalColor,bombs, fuel);
}
public void ChangeAddColor(Color col){
((EntityAirBomber)entityPlane).additionalColor = col;
}
public void drawTransport(Graphics2D g)
{
if (!(entityPlane instanceof EntityAirBomber))

View File

@ -14,8 +14,8 @@ public class DrawingPlane {
return entityPlane;
}
private IDrawEngines drawingEngines;
private int pictureWidth;
private int pictureHeight;
public int pictureWidth;
public int pictureHeight;
protected int startPosX;
public int getPosX(){return startPosX;}
protected int startPosY;
@ -24,6 +24,7 @@ public class DrawingPlane {
public int getWidth(){return PLANE_WIDTH;}
private final int PLANE_HEIGHT = 160;
public int getHeight(){return PLANE_HEIGHT;}
public DrawingPlane(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) {
if (width < PLANE_WIDTH || height < PLANE_HEIGHT)
return;
@ -140,4 +141,16 @@ public class DrawingPlane {
break;
}
}
public void ChangeColor(Color col){
entityPlane.bodyColor = col;
}
public void ChangeIDraw(IDrawEngines obj){
drawingEngines = obj;
}
public void ChangeEnginesCol(int col){
drawingEngines.setNumber(col);
}
}

View File

@ -3,7 +3,7 @@ package entities;
import java.awt.*;
public class EntityAirBomber extends EntityPlane {
private Color additionalColor;
public Color additionalColor;
public Color getAdditionalColor(){
return additionalColor;
}

View File

@ -11,7 +11,7 @@ public class EntityPlane {
public double getWeight() {
return weight;
}
private Color bodyColor;
public Color bodyColor;
public Color getBodyColor() {
return bodyColor;
}

View File

@ -5,7 +5,10 @@ import javax.swing.border.StrokeBorder;
import Generics.PlaneGenericStorage;
import Generics.PlaneTrashCollection;
import Generics.PlanesGenericCollection;
import PlaneHard.FormPlaneConfig;
import drawing_objects.DrawingPlane;
import movement_strategy.DrawingObjectPlane;
import java.awt.*;
import java.io.IOException;
@ -142,30 +145,25 @@ public class FramePlaneCollection extends JFrame {
storage.DelSet(listStorages.getSelectedValue());
reloadObjects();
}
private void buttonAddPlaneClick() {
if (listStorages.getSelectedIndex() == -1)
public void buttonAddPlaneClick() {
if(listStorages.getSelectedIndex() == -1) {
return;
var obj = storage.getCollection(listStorages.getSelectedValue());
if (obj == null)
return;
FrameAirBomber form;
try {
form = new FrameAirBomber();
} catch (IOException e) {
throw new RuntimeException(e);
}
form.selectPlaneButton.addActionListener(e->{
form.select();
DrawingPlane plane = form.getSelectedPlane();
form.dispose();
if (obj.Insert(plane)) {
JOptionPane.showMessageDialog(this, "Объект добавлен");
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> drawingPlanes = storage.getCollection(listStorages.getSelectedValue());
FormPlaneConfig framePlaneConfig = new FormPlaneConfig();
framePlaneConfig.addButton.addActionListener(e -> {
if (drawingPlanes.Insert(framePlaneConfig.drawingPlane)) {
framePlaneConfig.dispose();
framePlaneConfig.drawingPlane.pictureWidth = pictureBoxCollection.getWidth();
framePlaneConfig.drawingPlane.pictureHeight = pictureBoxCollection.getHeight();
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
pictureBoxCollection.repaint();
}
else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
});
framePlaneConfig.cancelButton.addActionListener(e -> framePlaneConfig.dispose());
}
private void buttonRemovePlaneClick(){
if (listStorages.getSelectedIndex() == -1)