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

This commit is contained in:
DjonniStorm 2024-06-09 23:09:55 +04:00
parent ed5df725c2
commit fcb6bf6d11
4 changed files with 72 additions and 48 deletions

View File

@ -100,7 +100,7 @@
<text value="Создать компанию"/>
</properties>
</component>
<grid id="ceabb" binding="panelCompanyTools" layout-manager="GridLayoutManager" row-count="9" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="ceabb" binding="panelCompanyTools" layout-manager="GridLayoutManager" row-count="8" 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="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -118,17 +118,9 @@
<text value="Добавление грузовика"/>
</properties>
</component>
<component id="112e" class="javax.swing.JButton" binding="buttonAddCleaningCar">
<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"/>
</constraints>
<properties>
<text value="Добавление уборочной машины"/>
</properties>
</component>
<component id="8fa47" class="javax.swing.JFormattedTextField" binding="maskedTextBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
@ -136,7 +128,7 @@
</component>
<component id="af87b" class="javax.swing.JButton" binding="buttonDel">
<constraints>
<grid row="3" 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="Удалить грузовик"/>
@ -144,7 +136,7 @@
</component>
<component id="7c328" class="javax.swing.JButton" binding="buttonGoToCheck">
<constraints>
<grid row="5" 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="4" 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="Передать на тесты"/>
@ -152,7 +144,7 @@
</component>
<component id="44aef" class="javax.swing.JButton" binding="buttonRefresh">
<constraints>
<grid row="6" 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="5" 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="Обновить"/>
@ -160,7 +152,7 @@
</component>
<component id="5f2f8" class="javax.swing.JButton" binding="buttonAdditionalCollection">
<constraints>
<grid row="7" 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="6" 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="Дополнительная коллекция"/>
@ -168,7 +160,7 @@
</component>
<component id="b5aef" class="javax.swing.JButton" binding="buttonResurrected">
<constraints>
<grid row="8" 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="7" 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="Из удалённых"/>
@ -176,7 +168,7 @@
</component>
<component id="bf6d" class="javax.swing.JButton" binding="buttonIndex">
<constraints>
<grid row="4" 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="3" 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="Удалить через индексатор"/>

View File

@ -7,6 +7,7 @@ import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Arrays;
import java.util.LinkedList;
@ -23,7 +24,6 @@ public class FormCleaningCarCollection extends JFrame{
private JPanel tools;
private JComboBox comboBoxSelectorCompany;
private JButton buttonAddTruck;
private JButton buttonAddCleaningCar;
private JPanel pictureBox;
private JFormattedTextField maskedTextBox;
private JButton buttonDel;
@ -60,7 +60,6 @@ public class FormCleaningCarCollection extends JFrame{
try {
maskFormatter = new MaskFormatter("##");
maskFormatter.setPlaceholder("00");
//это intellij
} catch (ParseException e) {
throw new RuntimeException(e);
}
@ -86,9 +85,13 @@ public class FormCleaningCarCollection extends JFrame{
});
//ButtonAddTruck_Click
buttonAddTruck.addActionListener(e -> CreateObject("DrawningTruck"));
//ButtonAddCleaningCar_Click
buttonAddCleaningCar.addActionListener(e -> CreateObject("DrawningCleaningCar"));
buttonAddTruck.addActionListener(e -> {
//CreateObject("DrawningTruck");
FormTruckConfig form = new FormTruckConfig();
form.Init();
form.setVisible(true);
form.SetCompany(_company);
});
//ButtonDelTruck_Click
buttonDel.addActionListener(new ActionListener() {
@Override
@ -298,6 +301,4 @@ public class FormCleaningCarCollection extends JFrame{
_company.Show(e);
}
public static void Show() {for (Frame i : Frame.getFrames()) {if (!i.isActive()) {i.repaint();}}}
// Method method = Component.class.getSuperclass().getDeclaredMethod("repaint");
// method.setAccessible(true);
}

View File

@ -1,3 +1,4 @@
import CollectionGenericObjects.AbstractCompany;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import Drawnings.IDrawningWheels;
@ -10,14 +11,19 @@ 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.ActionEvent;
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 {
private DrawningTruck _truck;
private AbstractCompany _company;
private JPanel panel;
private JPanel GroupBoxConfig;
private JLabel labelSimpleObject;
@ -49,13 +55,12 @@ public class FormTruckConfig extends JFrame {
setTitle("Создание объекта");
add(panel);
setMinimumSize(new Dimension(900, 300));
setDefaultCloseOperation(EXIT_ON_CLOSE);
//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));
@ -64,13 +69,13 @@ public class FormTruckConfig extends JFrame {
labelBodyColor.setBorder(new LineBorder(Color.BLACK));
labelAdditionalColor.setBorder(new LineBorder(Color.BLACK));
var labelObjectsMouseDown = new MouseAdapter() {
MouseAdapter labelObjectsMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
var labelObjectsTransferHandler = new TransferHandler() {
TransferHandler labelObjectsTransferHandler = new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
@ -99,7 +104,7 @@ public class FormTruckConfig extends JFrame {
switch (data) {
case "Простой":
_truck = new DrawningTruck((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.BLUE);
Color.WHITE);
System.out.println("a");
break;
case "Продвинутый":
@ -124,7 +129,7 @@ public class FormTruckConfig extends JFrame {
ArrayList<JPanel> colorList = new ArrayList<>();
colorList.add(panelObject); colorList.add(panelWhite);colorList.add(panelBlack);
colorList.add(panelWhite);colorList.add(panelBlack);
colorList.add(panelPurple); colorList.add(panelBlue); colorList.add(panelGreen);
colorList.add(panelGray); colorList.add(panelRed); colorList.add(panelYellow);
@ -134,10 +139,20 @@ public class FormTruckConfig extends JFrame {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
};
TransferHandler colorTransferHandler = new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(((JPanel)c).getBackground());
}
};
for (JPanel color : colorList) {
color.addMouseListener(colorMouseDown);
color.setTransferHandler(new ColorTransferHandler());
color.setTransferHandler(colorTransferHandler);
}
labelBodyColor.setTransferHandler(new TransferHandler() {
@ -151,6 +166,7 @@ public class FormTruckConfig extends JFrame {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null) return false;
_truck.EntityTruck.setBodyColor(color);
repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
@ -172,7 +188,7 @@ public class FormTruckConfig extends JFrame {
if (_truck == null) return false;
if (_truck.EntityTruck instanceof EntityCleaningCar cleaningCar) {
cleaningCar.setAdditionalColor(color);
//labelColor.setBackground(color);
repaint();
return true;
}
return false;
@ -183,6 +199,32 @@ public class FormTruckConfig extends JFrame {
}
});
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();
FormCleaningCarCollection.Show();
JOptionPane.showMessageDialog(null, "Объект добавлен");
//_company = null;
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
}
public void SetCompany(AbstractCompany company) {
_company = company;
}
@Override
public void paint(Graphics g) {
@ -192,7 +234,6 @@ public class FormTruckConfig extends JFrame {
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 {
@ -218,14 +259,4 @@ public class FormTruckConfig extends JFrame {
}
}
}
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());
}
}
}

View File

@ -1,9 +1,9 @@
class Main {
public static void main(String[] args) {
//FormCleaningCarCollection form = new FormCleaningCarCollection();
//form.setVisible(true);
FormTruckConfig config = new FormTruckConfig();
config.setVisible(true);
FormCleaningCarCollection form = new FormCleaningCarCollection();
form.setVisible(true);
// FormTruckConfig config = new FormTruckConfig();
// config.setVisible(true);
}
}