Лабораторная работа №5 (ничего не работает)
This commit is contained in:
parent
4d1afeaa06
commit
ed5df725c2
@ -6,7 +6,7 @@ import java.awt.*;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class DrawningTruck {
|
public class DrawningTruck {
|
||||||
protected EntityTruck EntityTruck;
|
public EntityTruck EntityTruck;
|
||||||
public EntityTruck getEntityTruck() {
|
public EntityTruck getEntityTruck() {
|
||||||
return EntityTruck;
|
return EntityTruck;
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,16 @@ public class EntityCleaningCar extends EntityTruck {
|
|||||||
public Color getAdditionalColor() {
|
public Color getAdditionalColor() {
|
||||||
return AdditionalColor;
|
return AdditionalColor;
|
||||||
}
|
}
|
||||||
|
public void setAdditionalColor(Color color) {AdditionalColor = color;}
|
||||||
private boolean Tank;
|
private boolean Tank;
|
||||||
public boolean getTank() {return Tank;}
|
public boolean getTank() {return Tank;}
|
||||||
|
public void setTank(boolean tank) {Tank = tank;}
|
||||||
private boolean SweepingBrush;
|
private boolean SweepingBrush;
|
||||||
public boolean getSweepingBrush() {return SweepingBrush;}
|
public boolean getSweepingBrush() {return SweepingBrush;}
|
||||||
|
public void setSweepingBrush(boolean sweepingBrush) {SweepingBrush = sweepingBrush;}
|
||||||
private boolean FlashLight;
|
private boolean FlashLight;
|
||||||
public boolean getFlashLight() {return 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) {
|
public EntityCleaningCar(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashLight) {
|
||||||
super(speed, weight, bodyColor);
|
super(speed, weight, bodyColor);
|
||||||
AdditionalColor = additionalColor;
|
AdditionalColor = additionalColor;
|
||||||
|
@ -7,14 +7,17 @@ public class EntityTruck {
|
|||||||
public int getSpeed() {
|
public int getSpeed() {
|
||||||
return Speed;
|
return Speed;
|
||||||
}
|
}
|
||||||
|
public void setSpeed(int speed) {Speed = speed;}
|
||||||
private double Weight;
|
private double Weight;
|
||||||
public double getWeight() {
|
public double getWeight() {
|
||||||
return Weight;
|
return Weight;
|
||||||
}
|
}
|
||||||
|
public void setWeight(double weight) {Weight = weight;}
|
||||||
private Color BodyColor;
|
private Color BodyColor;
|
||||||
public Color getBodyColor() {
|
public Color getBodyColor() {
|
||||||
return BodyColor;
|
return BodyColor;
|
||||||
}
|
}
|
||||||
|
public void setBodyColor(Color color) {BodyColor = color;}
|
||||||
public double Step() {
|
public double Step() {
|
||||||
return Speed * 100 / Weight;
|
return Speed * 100 / Weight;
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<horizontalAlignment value="0"/>
|
<horizontalAlignment value="0"/>
|
||||||
<text value="Простой"/>
|
<text value="Простой"/>
|
||||||
|
<toolTipText value="simple"/>
|
||||||
<verticalAlignment value="0"/>
|
<verticalAlignment value="0"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
@ -52,6 +53,7 @@
|
|||||||
<horizontalAlignment value="0"/>
|
<horizontalAlignment value="0"/>
|
||||||
<horizontalTextPosition value="0"/>
|
<horizontalTextPosition value="0"/>
|
||||||
<text value="Продвинутый"/>
|
<text value="Продвинутый"/>
|
||||||
|
<toolTipText value="modified"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
<component id="4ddc3" class="javax.swing.JLabel" binding="labelSpeed">
|
<component id="4ddc3" class="javax.swing.JLabel" binding="labelSpeed">
|
||||||
@ -235,7 +237,7 @@
|
|||||||
</grid>
|
</grid>
|
||||||
</children>
|
</children>
|
||||||
</grid>
|
</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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<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"/>
|
<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">
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="line"/>
|
<border type="line"/>
|
||||||
@ -254,7 +256,7 @@
|
|||||||
</grid>
|
</grid>
|
||||||
<component id="6a122" class="javax.swing.JButton" binding="buttonAdd">
|
<component id="6a122" class="javax.swing.JButton" binding="buttonAdd">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<text value="Добавить"/>
|
<text value="Добавить"/>
|
||||||
@ -262,12 +264,36 @@
|
|||||||
</component>
|
</component>
|
||||||
<component id="896cf" class="javax.swing.JButton" binding="buttonCancel">
|
<component id="896cf" class="javax.swing.JButton" binding="buttonCancel">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<text value="Отмена"/>
|
<text value="Отмена"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</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>
|
</children>
|
||||||
</grid>
|
</grid>
|
||||||
</children>
|
</children>
|
||||||
|
@ -1,12 +1,19 @@
|
|||||||
|
import Drawnings.DrawningCleaningCar;
|
||||||
import Drawnings.DrawningTruck;
|
import Drawnings.DrawningTruck;
|
||||||
|
import Drawnings.IDrawningWheels;
|
||||||
|
import Entities.EntityCleaningCar;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.border.LineBorder;
|
import javax.swing.border.LineBorder;
|
||||||
import java.awt.*;
|
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.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
import java.io.IOException;
|
||||||
import java.awt.event.MouseMotionAdapter;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class FormTruckConfig extends JFrame {
|
public class FormTruckConfig extends JFrame {
|
||||||
@ -35,36 +42,190 @@ public class FormTruckConfig extends JFrame {
|
|||||||
private JButton buttonAdd;
|
private JButton buttonAdd;
|
||||||
private JButton buttonCancel;
|
private JButton buttonCancel;
|
||||||
private JPanel panelObject;
|
private JPanel panelObject;
|
||||||
|
private JLabel labelBodyColor;
|
||||||
|
private JLabel labelAdditionalColor;
|
||||||
|
|
||||||
public FormTruckConfig() {
|
public FormTruckConfig() {
|
||||||
setTitle("Создание объекта");
|
setTitle("Создание объекта");
|
||||||
add(panel);
|
add(panel);
|
||||||
setMinimumSize(new Dimension(900, 300));
|
setMinimumSize(new Dimension(900, 300));
|
||||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
buttonCancel.addActionListener(e -> this.dispose());
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
Cursor cursor = Cursor.getDefaultCursor();
|
||||||
protected void Init() {
|
protected void Init() {
|
||||||
labelSimpleObject.setBorder(new LineBorder(Color.BLACK));
|
labelSimpleObject.setBorder(new LineBorder(Color.BLACK));
|
||||||
labelModifiedObject.setBorder(new LineBorder(Color.BLACK));
|
labelModifiedObject.setBorder(new LineBorder(Color.BLACK));
|
||||||
spinnerSpeed.setModel(new SpinnerNumberModel(0, 0, 1000, 1));
|
spinnerSpeed.setModel(new SpinnerNumberModel(0, 0, 1000, 1));
|
||||||
spinnerWeight.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<>();
|
var labelObjectsMouseDown = new MouseAdapter() {
|
||||||
// labelObject.add(labelSimpleObject);
|
|
||||||
// labelObject.add(labelModifiedObject);
|
|
||||||
|
|
||||||
MouseAdapter labelObject_MouseDown = new MouseAdapter() {
|
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
super.mousePressed(e);
|
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var labelObjectsTransferHandler = new TransferHandler() {
|
||||||
|
@Override
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
MouseMotionAdapter mouseMotionAdapter = new MouseMotionAdapter() {
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseDragged(MouseEvent e) {
|
public void paint(Graphics g) {
|
||||||
super.mouseDragged(e);
|
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());
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user