1. Создание формы FormArmoredCarConfig и добавление доп логики в классы объектов

This commit is contained in:
prodigygirl 2022-11-19 15:34:40 +04:00
parent 0dcc62e3b2
commit dfab5596fc
8 changed files with 526 additions and 0 deletions

View File

@ -40,11 +40,19 @@ public class DrawingArmoredCar {
this.carWidth = carWidth;
this.carHeight = carHeight;
}
public IDrawingCaterpillar GetCaterpillars() {
return drawingCaterpillar;
}
public void SetCaterpillar(IDrawingCaterpillar caterpillar) {
this.drawingCaterpillar = caterpillar;
}
public void SetColor(Color color) {
armoredCar = new EntityArmoredCar(armoredCar.getSpeed(), armoredCar.getWeight(), color);
drawingCaterpillar.setColor(color);
}
public void SetPosition(int x, int y, int width, int height) {
if (x >= 0 && y >= 0 && x + carWidth < width && y + carHeight < height) {
startPosX = x;

View File

@ -33,4 +33,9 @@ public class DrawingCaterpillar implements IDrawingCaterpillar {
g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size);
}
}
@Override
public void setColor(Color color) {
this.color = color;
}
}

View File

@ -39,4 +39,9 @@ public class DrawingCrossCaterpillar implements IDrawingCaterpillar{
g2d.drawLine(startPosX + dist * i + size, startPosY + 30, startPosX + dist * i, startPosY + 30 + size);
}
}
@Override
public void setColor(Color color) {
this.color = color;
}
}

View File

@ -36,4 +36,9 @@ public class DrawingDoubleCaterpillar implements IDrawingCaterpillar{
g2d.fillOval(startPosX + dist * i + size/4, startPosY + 30 + size/4, size/2, size/2);
}
}
@Override
public void setColor(Color color) {
this.color = color;
}
}

View File

@ -27,4 +27,17 @@ public class DrawingTank extends DrawingArmoredCar{
super.DrawTransport(g);
startPosY -= 5;
}
@Override
public void SetColor(Color color) {
if (armoredCar instanceof EntityTank tank)
armoredCar = new EntityTank(tank.getSpeed(), tank.getWeight(), color,
tank.getDopColor(), tank.isTowerWeapon(), tank.isAMachineGun());
drawingCaterpillar.setColor(color);
}
public void SetDopColor(Color color) {
if (armoredCar instanceof EntityTank tank)
armoredCar = new EntityTank(tank.getSpeed(), tank.getWeight(), tank.getBodyColor(),
color, tank.isTowerWeapon(), tank.isAMachineGun());
}
}

View File

@ -0,0 +1,233 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormArmoredCarConfig">
<grid id="27dc6" binding="mainPanel" 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>
<xy x="20" y="20" width="864" height="273"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="48e70" layout-manager="GridLayoutManager" row-count="7" column-count="10" 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="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="4c6f3" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="8" row-span="6" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="8a4cf" class="javax.swing.JButton" binding="buttonOk">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить"/>
</properties>
</component>
<component id="5498b" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="2" column="2" 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="ce0c6" class="javax.swing.JLabel" binding="labelBaseColor">
<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"/>
</constraints>
<properties>
<text value="Цвет"/>
</properties>
</component>
<component id="e8476" class="javax.swing.JLabel" binding="labelDopColor">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Доп. Цвет"/>
</properties>
</component>
<grid id="333f1" binding="drawPanel" custom-create="true" 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="1" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-3291443"/>
</properties>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
<component id="b99f7" class="javax.swing.JSpinner" binding="numericUpDownSpeed" custom-create="true">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="fd32c" class="javax.swing.JSpinner" binding="numericUpDownWeight" custom-create="true">
<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"/>
</constraints>
<properties/>
</component>
<component id="9d793" class="javax.swing.JCheckBox" binding="checkBoxTowerWeapon">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия орудия на башне"/>
</properties>
</component>
<component id="d2725" class="javax.swing.JCheckBox" binding="checkBoxAMachineGun">
<constraints>
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия зенитного пулемета"/>
</properties>
</component>
<component id="a8425" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость:"/>
</properties>
</component>
<component id="ba547" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес:"/>
</properties>
</component>
<grid id="b50ae" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="5" row-span="6" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b4cbb" binding="colorsPanel" 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="3" vsize-policy="2" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-1"/>
</properties>
<border type="none"/>
<children/>
</grid>
<component id="486f2" class="javax.swing.JLabel" binding="labelDoubleCaterpillar">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Двойные"/>
</properties>
</component>
<component id="287da" class="javax.swing.JLabel" binding="labelSimpleCaterpillar">
<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"/>
</constraints>
<properties>
<text value="Обычные"/>
</properties>
</component>
<component id="61c17" class="javax.swing.JLabel" binding="labelCrossCaterpillar">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Крестики"/>
</properties>
</component>
<grid id="7be45" layout-manager="GridLayoutManager" row-count="1" 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="2" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="88d7e" class="javax.swing.JLabel" binding="labelModifiedObject">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Продвинутый"/>
</properties>
</component>
<component id="e9351" class="javax.swing.JLabel" binding="labelSimpleObject">
<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"/>
</constraints>
<properties>
<text value="Простой"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<component id="b4b99" class="javax.swing.JLabel" binding="labelParameters">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Параметры"/>
</properties>
</component>
<hspacer id="893be">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="c09a6">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="f3f03">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="abacc">
<constraints>
<grid row="4" column="9" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="def80" class="javax.swing.JSpinner" binding="numericUpDownCount" custom-create="true">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="39892" class="javax.swing.JLabel" binding="labelCatCounts">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Количество катков"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,254 @@
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
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;
public class FormArmoredCarConfig extends JDialog{
private JPanel mainPanel;
private JButton buttonOk;
private JButton buttonCancel;
private JSpinner numericUpDownSpeed;
private JSpinner numericUpDownWeight;
private JCheckBox checkBoxAMachineGun;
private JCheckBox checkBoxTowerWeapon;
private JLabel labelSpeed;
private JLabel labelWeight;
private JLabel labelParameters;
private JLabel labelSimpleObject;
private JLabel labelModifiedObject;
private JLabel labelBaseColor;
private JLabel labelDopColor;
private JPanel drawPanel;
private JPanel colorsPanel;
private JLabel labelSimpleCaterpillar;
private JLabel labelDoubleCaterpillar;
private JLabel labelCrossCaterpillar;
private JSpinner numericUpDownCount;
private JLabel labelCatCounts;
private DrawingArmoredCar armoredCar = null;
public FormArmoredCarConfig() {
setTitle("Создание объекта");
setBounds(100, 100, 800, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setContentPane(mainPanel);
setVisible(true);
labelSimpleObject.setBorder(new LineBorder(Color.BLACK));
labelModifiedObject.setBorder(new LineBorder(Color.BLACK));
labelBaseColor.setBorder(new LineBorder(Color.BLACK));
labelDopColor.setBorder(new LineBorder(Color.BLACK));
labelSimpleCaterpillar.setBorder(new LineBorder(Color.BLACK));
labelDoubleCaterpillar.setBorder(new LineBorder(Color.BLACK));
labelCrossCaterpillar.setBorder(new LineBorder(Color.BLACK));
var mouseAdapter = new DragMouseAdapter();
labelSimpleObject.addMouseListener(mouseAdapter);
labelSimpleObject.setTransferHandler(new TransferHandler("text"));
labelModifiedObject.addMouseListener(mouseAdapter);
labelModifiedObject.setTransferHandler(new TransferHandler("text"));
labelSimpleCaterpillar.addMouseListener(mouseAdapter);
labelDoubleCaterpillar.addMouseListener(mouseAdapter);
labelCrossCaterpillar.addMouseListener(mouseAdapter);
labelSimpleCaterpillar.setTransferHandler(new TransferHandler("text"));
labelDoubleCaterpillar.setTransferHandler(new TransferHandler("text"));
labelCrossCaterpillar.setTransferHandler(new TransferHandler("text"));
drawPanel.setBackground(null);
colorsPanel.setBorder(BorderFactory.createTitledBorder("Цвета"));
colorsPanel.setLayout(new GridLayout(2, 4));
colorsPanel.setBackground(null);
setColors();
buttonCancel.addActionListener((e) -> dispose());
new DrawPanelDropTargetListener(drawPanel);
new LabelColorDropTargetListener(labelBaseColor);
new LabelColorDropTargetListener(labelDopColor);
buttonOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO работа с делегатом (вызов метода по созданию объекта из другой формы)
dispose();
}
});
}
private class DragMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
var c = (JComponent) e.getSource();
var handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
private class LabelColorDropTargetListener extends DropTargetAdapter{
private final DropTarget dropTarget;
private final JLabel label;
public LabelColorDropTargetListener(JLabel label) {
this.label = label;
dropTarget = new DropTarget(label, DnDConstants.ACTION_COPY,
this, true, null);
}
@Override
public void drop(DropTargetDropEvent event) {
try {
var tr = event.getTransferable();
var dataFlavors = tr.getTransferDataFlavors();
String s = (String) tr.getTransferData(dataFlavors[0]);
Color color = Color.decode(s);
switch (label.getText()) {
case "Цвет": {
if (armoredCar != null) {
armoredCar.SetColor(color);
}
break;
}
case "Доп. Цвет": {
if (armoredCar != null) {
if (armoredCar instanceof DrawingTank tank) {
tank.SetDopColor(color);
}
}
break;
}
}
drawPanel.repaint();
} catch (Exception e) {
event.rejectDrop();
}
}
}
private class DrawPanelDropTargetListener extends DropTargetAdapter {
private final DropTarget dropTarget;
private final JPanel panel;
public DrawPanelDropTargetListener(JPanel panel) {
this.panel = panel;
dropTarget = new DropTarget(panel, DnDConstants.ACTION_COPY,
this, true, null);
}
public void drop(DropTargetDropEvent event) {
try {
var tr = event.getTransferable();
var dataFlavors = tr.getTransferDataFlavors();
String drawing_type = (String) tr.getTransferData(dataFlavors[0]);
event.acceptDrop(DnDConstants.ACTION_COPY);
switch (drawing_type) {
case "Простой": {
armoredCar = new DrawingArmoredCar((int) numericUpDownSpeed.getValue(), (int) numericUpDownWeight.getValue(), Color.WHITE);
IDrawingCaterpillar drawingCaterpillar = armoredCar.GetCaterpillars();
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
break;
}
case "Продвинутый": {
armoredCar = new DrawingTank((int) numericUpDownSpeed.getValue(), (int) numericUpDownWeight.getValue(), Color.WHITE, Color.BLACK,
checkBoxTowerWeapon.isSelected(), checkBoxAMachineGun.isSelected());
IDrawingCaterpillar drawingCaterpillar = armoredCar.GetCaterpillars();
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
break;
}
case "Обычные": {
if (armoredCar != null) {
IDrawingCaterpillar drawingCaterpillar = new DrawingCaterpillar(armoredCar.armoredCar.getBodyColor());
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
armoredCar.SetCaterpillar(drawingCaterpillar);
}
break;
}
case "Двойные": {
if (armoredCar != null) {
IDrawingCaterpillar drawingCaterpillar = new DrawingDoubleCaterpillar(armoredCar.armoredCar.getBodyColor());
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
armoredCar.SetCaterpillar(drawingCaterpillar);
}
break;
}
case "Крестики": {
if (armoredCar != null) {
IDrawingCaterpillar drawingCaterpillar = new DrawingCrossCaterpillar(armoredCar.armoredCar.getBodyColor());
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
armoredCar.SetCaterpillar(drawingCaterpillar);
}
break;
}
}
event.dropComplete(true);
drawPanel.repaint();
} catch (Exception e) {
e.printStackTrace();
event.rejectDrop();
}
}
}
public static void main(String[] args) {
new FormArmoredCarConfig();
}
private void createUIComponents() {
drawPanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (armoredCar != null) {
Graphics2D g2d = (Graphics2D) g;
armoredCar.SetPosition(drawPanel.getWidth() / 4, drawPanel.getHeight() / 4, drawPanel.getWidth(), drawPanel.getHeight());
armoredCar.DrawTransport(g2d);
}
super.repaint();
}
};
numericUpDownCount = new JSpinner(new SpinnerNumberModel(4, 4, 6, 1));
numericUpDownSpeed = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 1));
numericUpDownWeight = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 1));
}
private void setColors() {
Color[] colors = new Color[8];
colors[0] = Color.RED;
colors[1] = Color.GREEN;
colors[2] = Color.BLUE;
colors[3] = Color.YELLOW;
colors[4] = Color.WHITE;
colors[5] = Color.DARK_GRAY;
colors[6] = Color.BLACK;
colors[7] = Color.MAGENTA;
int i = 0;
int width = 40;
for (Color color : colors) {
JButton btn = new JButton();
btn.setBackground(color);
btn.setEnabled(false);
int x = i % 4;
int y = i / 4;
btn.setBounds(x * width + 10, y * width + 10, width, width);
btn.setText(Integer.toString(color.getRGB()));
btn.setFont(new Font("Arial", Font.PLAIN, 0));
btn.setTransferHandler(new TransferHandler("text"));
btn.addMouseListener(new DragMouseAdapter());
colorsPanel.add(btn);
i++;
}
}
}

View File

@ -4,4 +4,7 @@ public interface IDrawingCaterpillar {
void setNumRinks(int n);
void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY);
void setColor(Color color);
}