Лабораторная работа №5 (ничего не работает)

This commit is contained in:
DjonniStorm 2024-06-09 18:56:43 +04:00
parent 4d1afeaa06
commit ed5df725c2
5 changed files with 211 additions and 17 deletions

View File

@ -6,7 +6,7 @@ import java.awt.*;
import java.util.Random;
public class DrawningTruck {
protected EntityTruck EntityTruck;
public EntityTruck EntityTruck;
public EntityTruck getEntityTruck() {
return EntityTruck;
}

View File

@ -6,12 +6,16 @@ public class EntityCleaningCar extends EntityTruck {
public Color getAdditionalColor() {
return AdditionalColor;
}
public void setAdditionalColor(Color color) {AdditionalColor = color;}
private boolean Tank;
public boolean getTank() {return Tank;}
public void setTank(boolean tank) {Tank = tank;}
private boolean SweepingBrush;
public boolean getSweepingBrush() {return SweepingBrush;}
public void setSweepingBrush(boolean sweepingBrush) {SweepingBrush = sweepingBrush;}
private boolean FlashLight;
public boolean getFlashLight() {return FlashLight;}
public void setFlashLight(boolean flashLight) {FlashLight = flashLight;}
public EntityCleaningCar(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashLight) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;

View File

@ -7,14 +7,17 @@ public class EntityTruck {
public int getSpeed() {
return Speed;
}
public void setSpeed(int speed) {Speed = speed;}
private double Weight;
public double getWeight() {
return Weight;
}
public void setWeight(double weight) {Weight = weight;}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public void setBodyColor(Color color) {BodyColor = color;}
public double Step() {
return Speed * 100 / Weight;
}

View File

@ -29,6 +29,7 @@
<properties>
<horizontalAlignment value="0"/>
<text value="Простой"/>
<toolTipText value="simple"/>
<verticalAlignment value="0"/>
</properties>
</component>
@ -52,6 +53,7 @@
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Продвинутый"/>
<toolTipText value="modified"/>
</properties>
</component>
<component id="4ddc3" class="javax.swing.JLabel" binding="labelSpeed">
@ -235,7 +237,7 @@
</grid>
</children>
</grid>
<grid id="292f4" binding="panelRight" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="292f4" binding="panelRight" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -246,7 +248,7 @@
<grid id="193d6" binding="panelObject" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="line"/>
@ -254,7 +256,7 @@
</grid>
<component id="6a122" class="javax.swing.JButton" binding="buttonAdd">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить"/>
@ -262,12 +264,36 @@
</component>
<component id="896cf" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Отмена"/>
</properties>
</component>
<component id="f5ca6" class="javax.swing.JLabel" binding="labelAdditionalColor">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="60" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Доп. цвет"/>
</properties>
</component>
<component id="b6369" class="javax.swing.JLabel" binding="labelBodyColor">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="60" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Цвет"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@ -1,12 +1,19 @@
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import Drawnings.IDrawningWheels;
import Entities.EntityCleaningCar;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.ArrayList;
public class FormTruckConfig extends JFrame {
@ -35,36 +42,190 @@ public class FormTruckConfig extends JFrame {
private JButton buttonAdd;
private JButton buttonCancel;
private JPanel panelObject;
private JLabel labelBodyColor;
private JLabel labelAdditionalColor;
public FormTruckConfig() {
setTitle("Создание объекта");
add(panel);
setMinimumSize(new Dimension(900, 300));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
buttonCancel.addActionListener(e -> this.dispose());
Init();
}
Cursor cursor = Cursor.getDefaultCursor();
protected void Init() {
labelSimpleObject.setBorder(new LineBorder(Color.BLACK));
labelModifiedObject.setBorder(new LineBorder(Color.BLACK));
spinnerSpeed.setModel(new SpinnerNumberModel(0, 0, 1000, 1));
spinnerWeight.setModel(new SpinnerNumberModel(0, 0, 1000, 1));
labelBodyColor.setBorder(new LineBorder(Color.BLACK));
labelAdditionalColor.setBorder(new LineBorder(Color.BLACK));
// ArrayList<JLabel> labelObject = new ArrayList<>();
// labelObject.add(labelSimpleObject);
// labelObject.add(labelModifiedObject);
MouseAdapter labelObject_MouseDown = new MouseAdapter() {
var labelObjectsMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
MouseMotionAdapter mouseMotionAdapter = new MouseMotionAdapter() {
var labelObjectsTransferHandler = new TransferHandler() {
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel) c).getText());
}
};
labelSimpleObject.addMouseListener(labelObjectsMouseDown);
labelSimpleObject.setTransferHandler(labelObjectsTransferHandler);
labelModifiedObject.addMouseListener(labelObjectsMouseDown);
labelModifiedObject.setTransferHandler(labelObjectsTransferHandler);
panelObject.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor
/* || support.isDataFlavorSupported(DecksTransferable.decksDataFlavor*/);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Простой":
_truck = new DrawningTruck((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.BLUE);
System.out.println("a");
break;
case "Продвинутый":
_truck = new DrawningCleaningCar((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE, Color.BLACK,
checkBoxTank.isSelected(), checkBoxSweepingBrush.isSelected(), CheckBoxFlashLight.isSelected());
break;
}
if (_truck != null) {
_truck.SetPictureSize(panelObject.getWidth(), panelObject.getHeight());
_truck.SetPosition(20,20);
}
else return false;
}
catch (UnsupportedFlavorException | IOException e) {}
repaint();
return true;
}
return false;
}
});
ArrayList<JPanel> colorList = new ArrayList<>();
colorList.add(panelObject); colorList.add(panelWhite);colorList.add(panelBlack);
colorList.add(panelPurple); colorList.add(panelBlue); colorList.add(panelGreen);
colorList.add(panelGray); colorList.add(panelRed); colorList.add(panelYellow);
MouseAdapter colorMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
};
for (JPanel color : colorList) {
color.addMouseListener(colorMouseDown);
color.setTransferHandler(new ColorTransferHandler());
}
labelBodyColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null) return false;
_truck.EntityTruck.setBodyColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
labelAdditionalColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
if (!(_truck instanceof DrawningCleaningCar)) return false;
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null) return false;
if (_truck.EntityTruck instanceof EntityCleaningCar cleaningCar) {
cleaningCar.setAdditionalColor(color);
//labelColor.setBackground(color);
return true;
}
return false;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_truck == null) return;
Graphics e = panelObject.getGraphics();
e.setColor(panelObject.getBackground());
e.fillRect(0,0, panelObject.getWidth(), panelObject.getHeight());
_truck.DrawTransport(e);
System.out.println(_truck.GetPosX());
}
private class ColorTransferable implements Transferable {
private Color color;
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
public ColorTransferable(Color color) {
this.color = 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 class ColorTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(c.getBackground());
}
}
}