Лабораторная работа №5 (возможно что-то да работает)

This commit is contained in:
DjonniStorm 2024-06-10 02:52:12 +04:00
parent fcb6bf6d11
commit f6d2369ac6
4 changed files with 193 additions and 77 deletions

View File

@ -40,7 +40,7 @@ public class FormCleaningCar extends JFrame {
_drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
comboBoxStrategy.setEnabled(true);
_strategy = null;
repaint(); //по-идее вызовет paint()
repaint();
}
public FormCleaningCar() {
setSize(1000, 600);

View File

@ -86,7 +86,6 @@ public class FormCleaningCarCollection extends JFrame{
//ButtonAddTruck_Click
buttonAddTruck.addActionListener(e -> {
//CreateObject("DrawningTruck");
FormTruckConfig form = new FormTruckConfig();
form.Init();
form.setVisible(true);
@ -251,34 +250,6 @@ public class FormCleaningCarCollection extends JFrame{
}
});
}
private void CreateObject(String type) {
if (_company == null) return;
Random random = new Random();
DrawningTruck drawningTruck;
switch (type) {
case "DrawningTruck":
drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextDouble(100, 3000),
GetColor(random));
break;
case "DrawningCleaningCar":
drawningTruck = new DrawningCleaningCar(random.nextInt(100, 300), random.nextDouble(100, 3000),
GetColor(random), GetColor(random),
random.nextBoolean(), random.nextBoolean(), random.nextBoolean());
break;
default: return;
}
if (_company._collection.Insert(drawningTruck, 0) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
private Color GetColor(Random random) {
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color colorChooser = JColorChooser.showDialog(this, "Выберите цвет", color);
return colorChooser;
}
private void RefreshListBoxItems()
{
listBoxCollection.removeAll();

View File

@ -8,7 +8,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="50bbc" binding="GroupBoxConfig" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="50bbc" binding="GroupBoxConfig" layout-manager="GridLayoutManager" row-count="7" column-count="6" 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="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false">
@ -22,7 +22,7 @@
<children>
<component id="9090b" class="javax.swing.JLabel" binding="labelSimpleObject">
<constraints>
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="2" fill="0" indent="0" use-parent-layout="false">
<grid row="6" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="90" height="40"/>
</grid>
</constraints>
@ -33,19 +33,14 @@
<verticalAlignment value="0"/>
</properties>
</component>
<hspacer id="56992">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="f3150">
<constraints>
<grid row="0" column="2" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="0" column="3" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="bef18" class="javax.swing.JLabel" binding="labelModifiedObject">
<constraints>
<grid row="5" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="2" fill="0" indent="0" use-parent-layout="false">
<grid row="6" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="90" height="40"/>
</grid>
</constraints>
@ -58,7 +53,7 @@
</component>
<component id="4ddc3" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость:"/>
@ -66,9 +61,9 @@
</component>
<component id="e70e0" class="javax.swing.JSpinner" binding="spinnerSpeed">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="20"/>
<preferred-size width="61" height="20"/>
<preferred-size width="70" height="20"/>
<maximum-size width="70" height="-1"/>
</grid>
</constraints>
@ -76,7 +71,7 @@
</component>
<component id="32f" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес:"/>
@ -84,9 +79,9 @@
</component>
<component id="532dd" class="javax.swing.JSpinner" binding="spinnerWeight">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="20"/>
<preferred-size width="61" height="20"/>
<preferred-size width="70" height="20"/>
<maximum-size width="70" height="-1"/>
</grid>
</constraints>
@ -94,7 +89,7 @@
</component>
<component id="77aaa" class="javax.swing.JCheckBox" binding="checkBoxTank">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия бака"/>
@ -102,15 +97,15 @@
</component>
<component id="189fd" class="javax.swing.JCheckBox" binding="checkBoxSweepingBrush">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия щётки"/>
</properties>
</component>
<component id="69dcd" class="javax.swing.JCheckBox" binding="CheckBoxFlashLight">
<component id="69dcd" class="javax.swing.JCheckBox" binding="checkBoxFlashLight">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия маячка"/>
@ -119,7 +114,7 @@
<grid id="59a96" binding="groupBoxColors" layout-manager="GridLayoutManager" row-count="2" column-count="4" 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="3" row-span="5" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="0" column="4" row-span="6" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="300" height="-1"/>
<preferred-size width="300" height="-1"/>
</grid>
@ -235,6 +230,61 @@
</grid>
</children>
</grid>
<component id="8a3dc" class="javax.swing.JLabel" binding="labelCountWheels">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Число колёс:"/>
</properties>
</component>
<component id="23c65" class="javax.swing.JSpinner" binding="spinnerCountWheels">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="70" height="20"/>
<preferred-size width="20" height="20"/>
<maximum-size width="70" height="20"/>
</grid>
</constraints>
<properties/>
</component>
<component id="ac4ad" class="javax.swing.JLabel" binding="labelTriangleWheels">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<text value="Треугольник"/>
<toolTipText value="Triangle"/>
</properties>
</component>
<component id="d0d6" class="javax.swing.JLabel" binding="labelCircleWheels">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="2"/>
<text value="Круг"/>
<toolTipText value="Circle"/>
<verticalTextPosition value="0"/>
</properties>
</component>
<component id="7ba83" class="javax.swing.JLabel" binding="labelRhombWheels">
<constraints>
<grid row="6" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<text value="Ромб"/>
<toolTipText value="Rhomb"/>
</properties>
</component>
</children>
</grid>
<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">

View File

@ -1,7 +1,5 @@
import CollectionGenericObjects.AbstractCompany;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import Drawnings.IDrawningWheels;
import Drawnings.*;
import Entities.EntityCleaningCar;
import javax.swing.*;
@ -16,9 +14,6 @@ import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class FormTruckConfig extends JFrame {
@ -34,7 +29,7 @@ public class FormTruckConfig extends JFrame {
private JLabel labelWeight;
private JCheckBox checkBoxTank;
private JCheckBox checkBoxSweepingBrush;
private JCheckBox CheckBoxFlashLight;
private JCheckBox checkBoxFlashLight;
private JPanel groupBoxColors;
private JPanel panelRed;
private JPanel panelGreen;
@ -50,25 +45,34 @@ public class FormTruckConfig extends JFrame {
private JPanel panelObject;
private JLabel labelBodyColor;
private JLabel labelAdditionalColor;
private JSpinner spinnerCountWheels;
private JLabel labelCountWheels;
private JLabel labelTriangleWheels;
private JLabel labelCircleWheels;
private JLabel labelRhombWheels;
public FormTruckConfig() {
setTitle("Создание объекта");
add(panel);
setMinimumSize(new Dimension(900, 300));
//setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
buttonCancel.addActionListener(e -> this.dispose());
Init();
}
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));
labelSimpleObject.setBorder(new LineBorder(Color.BLACK, 2));
labelModifiedObject.setBorder(new LineBorder(Color.BLACK, 2));
labelCircleWheels.setBorder(new LineBorder(Color.BLACK, 2));
labelRhombWheels.setBorder(new LineBorder(Color.BLACK, 2));
labelTriangleWheels.setBorder(new LineBorder(Color.BLACK, 2));
spinnerSpeed.setModel(new SpinnerNumberModel(100, 1, 1000, 1));
spinnerWeight.setModel(new SpinnerNumberModel(100, 1, 1000, 1));
spinnerCountWheels.setModel(new SpinnerNumberModel(1, 1, 3, 1));
labelBodyColor.setBorder(new LineBorder(Color.BLACK));
labelAdditionalColor.setBorder(new LineBorder(Color.BLACK));
MouseAdapter labelObjectsMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
@ -90,11 +94,40 @@ public class FormTruckConfig extends JFrame {
labelModifiedObject.addMouseListener(labelObjectsMouseDown);
labelModifiedObject.setTransferHandler(labelObjectsTransferHandler);
ArrayList<JLabel> IDrawningWheelsLabels = new ArrayList<>();
IDrawningWheelsLabels.add(labelTriangleWheels);
IDrawningWheelsLabels.add(labelCircleWheels);
IDrawningWheelsLabels.add(labelRhombWheels);
for (JLabel i : IDrawningWheelsLabels) {
i.addMouseListener(labelObjectsMouseDown);
i.setTransferHandler(new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
IDrawningWheels wheels;
switch (i.getToolTipText()) {
case "Triangle":
wheels = new DrawningTriangleWheels();
break;
case "Circle":
wheels = new DrawningCircleWheels();
break;
case "Rhomb":
wheels = new DrawningRhombWheels();
break;
default: return null;
}
return new WheelsTransferable(wheels);
}
});
}
panelObject.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor
/* || support.isDataFlavorSupported(DecksTransferable.decksDataFlavor*/);
return support.isDataFlavorSupported(DataFlavor.stringFlavor)
|| support.isDataFlavorSupported(WheelsTransferable.wheelsDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
@ -105,21 +138,28 @@ public class FormTruckConfig extends JFrame {
case "Простой":
_truck = new DrawningTruck((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE);
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());
checkBoxTank.isSelected(), checkBoxSweepingBrush.isSelected(), checkBoxFlashLight.isSelected());
break;
}
if (_truck != null) {
_truck.SetPictureSize(panelObject.getWidth(), panelObject.getHeight());
_truck.SetPosition(20,20);
_truck.SetPosition(60,30);
_truck.drawningWheels.setCountWheels((int)spinnerCountWheels.getValue());
}
else return false;
}
catch (UnsupportedFlavorException | IOException e) {}
try {
IDrawningWheels wheels =
(IDrawningWheels) support.getTransferable().getTransferData(WheelsTransferable.wheelsDataFlavor);
_truck.drawningWheels = wheels;
_truck.drawningWheels.setCountWheels((int) spinnerCountWheels.getValue());
}catch (UnsupportedFlavorException | IOException e) {}
repaint();
return true;
}
@ -198,30 +238,61 @@ public class FormTruckConfig extends JFrame {
return false;
}
});
//SetTruck
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || _truck == null) {
return;
}
DrawningTruck truck;
if (_truck instanceof DrawningCleaningCar) {
truck = new DrawningCleaningCar((EntityCleaningCar) _truck.EntityTruck, _truck.drawningWheels);
} else {
truck = new DrawningTruck(_truck.getEntityTruck(), _truck.drawningWheels);
}
if (_company._collection.Insert(truck, 0) != -1) {
dispose();
if (_company._collection.Insert(_truck, 0) != -1) {
FormCleaningCarCollection.Show();
JOptionPane.showMessageDialog(null, "Объект добавлен");
//_company = null;
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
dispose();
_company = null;
}
});
spinnerCountWheels.addChangeListener(e -> {
if (_truck == null) return;
_truck.drawningWheels.setCountWheels((int)spinnerCountWheels.getValue());
repaint();
});
checkBoxTank.addActionListener(e -> {
if (_truck == null) return;
if (_truck.EntityTruck instanceof EntityCleaningCar) {
((EntityCleaningCar) _truck.EntityTruck).setTank(checkBoxTank.isSelected());
repaint();
}
});
checkBoxSweepingBrush.addActionListener(e -> {
if (_truck == null) return;
if (_truck.EntityTruck instanceof EntityCleaningCar) {
((EntityCleaningCar) _truck.EntityTruck).setSweepingBrush(checkBoxSweepingBrush.isSelected());
repaint();
}
});
checkBoxFlashLight.addActionListener(e -> {
if (_truck == null) return;
if (_truck.EntityTruck instanceof EntityCleaningCar) {
((EntityCleaningCar) _truck.EntityTruck).setFlashLight(checkBoxFlashLight.isSelected());
repaint();
}
});
spinnerWeight.addChangeListener(e -> {
if (_truck == null) return;
_truck.EntityTruck.setSpeed((int)spinnerSpeed.getValue());
});
spinnerWeight.addChangeListener(e -> {
if (_truck == null) return;
_truck.EntityTruck.setWeight((int)spinnerWeight.getValue());
});
}
public void SetCompany(AbstractCompany company) {
_company = company;
@ -259,4 +330,28 @@ public class FormTruckConfig extends JFrame {
}
}
}
private class WheelsTransferable implements Transferable {
private IDrawningWheels wheels;
private static final DataFlavor wheelsDataFlavor = new DataFlavor(IDrawningWheels.class, "Decks");
public WheelsTransferable(IDrawningWheels wheels) {
this.wheels = wheels;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{wheelsDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(wheelsDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return wheels;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
}