This commit is contained in:
Alenka 2023-12-29 17:26:50 +04:00
parent e3c4b3b319
commit 11e90f1f62
5 changed files with 396 additions and 3 deletions

View File

@ -41,7 +41,7 @@ public class CruiserCollectionFrame extends JFrame {
return; return;
} }
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue()); CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
GameFrame form = new GameFrame(); FormCruiserConfig form = new FormCruiserConfig();
if (form.getSelectedCruiser() != null) { if (form.getSelectedCruiser() != null) {
if (_cruisers.plus(_cruisers, form.SelectedCruiser)) { if (_cruisers.plus(_cruisers, form.SelectedCruiser)) {
JOptionPane.showMessageDialog(null, "Объект добавлен"); JOptionPane.showMessageDialog(null, "Объект добавлен");

View File

@ -47,4 +47,7 @@ public class DrawingAdvancedCruiser extends DrawingCruiser{
if (wheels == null){return;} if (wheels == null){return;}
wheels.drawWheels(g, _startPosX, _startPosY, brush); wheels.drawWheels(g, _startPosX, _startPosY, brush);
} }
public void ChangeAddColor(Color color) {
EntityCruiser.AdditionalColor = color;
}
} }

View File

@ -40,7 +40,7 @@ public class DrawingCruiser {
return EntityCruiser; return EntityCruiser;
} }
protected DrawingCruiser(int speed, double weight, Color bodyColor, int public DrawingCruiser(int speed, double weight, Color bodyColor, int
width, int height, int cruiserWidth, int cruiserHeight) { width, int height, int cruiserWidth, int cruiserHeight) {
if (width <= _cruiserWidth || height <= _cruiserHeight) { if (width <= _cruiserWidth || height <= _cruiserHeight) {
return; return;
@ -144,4 +144,11 @@ public class DrawingCruiser {
wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor); wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor);
} }
public IMoveableObject GetMoveableObject(){return new DrawingObjectCruiser(this);} public IMoveableObject GetMoveableObject(){return new DrawingObjectCruiser(this);}
public void ChangeColor(Color color) {
EntityCruiser.BodyColor = color;
}
public void ChangeIDop(IDop obj, String n) {
wheels = obj;
wheels.setNumOfWheels(n);
}
} }

383
FormCruiserConfig.java Normal file
View File

@ -0,0 +1,383 @@
import javax.swing.*;
import javax.swing.border.Border;
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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
public class FormCruiserConfig extends JDialog {
public DrawingCruiser drawingCruiser;
JPanel cruiserPanel = new JPanel();
protected DrawingCruiser SelectedCruiser;
protected static JLabel colorLabel = new JLabel("Цвет", 0);
protected JLabel colorAddLabel = new JLabel("Доп. цвет", 0);
FormCruiserConfig() {
setTitle("Cruiser");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.setResizable(false);
setSize(900, 400);
setLayout(null);
JCheckBox ifTentCheckBox = new JCheckBox("Признак наличия площадки под вертолет");
JCheckBox ifKitCheckBox = new JCheckBox("Признак наличия ракетных шахт ");
ifTentCheckBox.setBounds(20, 200, 150, 30);
this.add(ifTentCheckBox);
ifKitCheckBox.setBounds(20, 250, 150, 30);
this.add(ifKitCheckBox);
JSpinner speedSpinner = new JSpinner();
speedSpinner.setBounds(20 + 100, 100, 50, 20);
speedSpinner.setValue(100);
this.add(speedSpinner);
JSpinner weightSpinner = new JSpinner();
weightSpinner.setBounds(20 + 100, 150, 50, 20);
weightSpinner.setValue(100);
this.add(weightSpinner);
JLabel speedLabel = new JLabel("Скорость");
speedLabel.setBounds(20, 100, 150, 20);
this.add(speedLabel);
JLabel weightLabel = new JLabel("Вес");
weightLabel.setBounds(20, 150, 150, 20);
this.add(weightLabel);
int xStart = 300;
int yStart = 50;
int panelColorSize = 50;
Color[] colors = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.PINK};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
JPanel colorPanel = new JPanel();
colorPanel.setTransferHandler(new PanelTransferHandler());
colorPanel.addMouseListener(new PanelMouseAdapter());
colorPanel.setBackground(colors[i * 4 + j]);
colorPanel.setBounds(xStart, yStart, panelColorSize, panelColorSize);
this.add(colorPanel);
xStart += panelColorSize + 10;
}
yStart += panelColorSize + 10;
xStart -= (4 * panelColorSize + 40);
}
JLabel easyLabel = new JLabel("Простой", 0);
easyLabel.setTransferHandler(new LabelTransferHandler());
easyLabel.addMouseListener(new LabelMouseAdapter());
easyLabel.setBounds(300, 300, 100, 50);
easyLabel.setOpaque(true);
easyLabel.setBackground(Color.LIGHT_GRAY);
this.add(easyLabel);
JLabel hardLabel = new JLabel("Продвинутый", 0);
hardLabel.setTransferHandler(new LabelTransferHandler());
hardLabel.addMouseListener(new LabelMouseAdapter());
hardLabel.setBounds(410, 300, 100, 50);
hardLabel.setOpaque(true);
hardLabel.setBackground(Color.LIGHT_GRAY);
this.add(hardLabel);
colorLabel.setTransferHandler(new TransferHandler("background"));
colorLabel.setBounds(600, 50, 100, 40);
colorLabel.setOpaque(true);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1); // Создание границы
colorLabel.setBorder(border);
this.add(colorLabel);
colorAddLabel.setTransferHandler(new TransferHandler("background"));
colorAddLabel.setBounds(750, 50, 100, 40);
colorAddLabel.setOpaque(true);
colorAddLabel.setBorder(border);
this.add(colorAddLabel);
JLabel iDopLabel = new JLabel("Палубы", 0);
iDopLabel.setBounds(680, 10, 100, 30);
iDopLabel.setOpaque(true);
iDopLabel.setBorder(border);
this.add(iDopLabel);
colorLabel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawingCruiser == null)
return false;
drawingCruiser.ChangeColor(color);
colorLabel.setBackground(color);
repaintPanel();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
colorAddLabel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawingCruiser == null || !(drawingCruiser instanceof DrawingAdvancedCruiser))
return false;
((DrawingAdvancedCruiser) drawingCruiser).ChangeAddColor(color);
colorAddLabel.setBackground(color);
repaintPanel();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JSpinner numOfWheelsSpinner = new JSpinner();
iDopLabel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(iDopTransferable.iDrawDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDop obj = (IDop) support.getTransferable().getTransferData(iDopTransferable.iDrawDataFlavor);
if (drawingCruiser == null)
return false;
drawingCruiser.ChangeIDop(obj, (String) numOfWheelsSpinner.getValue());
repaintPanel();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JButton deleteButton = new JButton("Отмена");
deleteButton.setBounds(750, 300, 100, 30);
this.add(deleteButton);
JButton buttonCruiser = new JButton("Добавить");
buttonCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SelectedCruiser = drawingCruiser;
dispose();
}
});
buttonCruiser.setBounds(600, 300, 100, 30);
this.add(buttonCruiser);
JLabel numWheelsLabel = new JLabel("Палубы");
numWheelsLabel.setBounds(300, 200, 70, 20);
this.add(numWheelsLabel);
numOfWheelsSpinner.setValue(3);
numOfWheelsSpinner.setBounds(370, 200, 40, 20);
this.add(numOfWheelsSpinner);
IDopComponent triangleLComp = new IDopComponent(new DopClassMixed(3));
JLabel triangleLabel = new JLabel("1 вид", 0);
triangleLComp.add(triangleLabel);
triangleLComp.setBounds(300, 250, 20, 20);
triangleLComp.setBorder(border);
this.add(triangleLComp);
IDopComponent rectComp = new IDopComponent(new DopClassRect(3));
JLabel rectLabel = new JLabel("2 вид", 0);
rectComp.add(rectLabel);
rectComp.setBounds(330, 250, 20, 20);
rectComp.setBorder(border);
this.add(rectComp);
IDopComponent circleComp = new IDopComponent(new NumberOfWheels(3));
JLabel circleLabel = new JLabel("3 вид", 0);
circleComp.add(circleLabel);
circleComp.setBounds(360, 250, 20, 20);
circleComp.setBorder(border);
this.add(circleComp);
cruiserPanel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
int speed = ((Number) speedSpinner.getValue()).intValue();
int weight = ((Number) weightSpinner.getValue()).intValue();
int enginesNumber = ((Number) numOfWheelsSpinner.getValue()).intValue();
switch ((String) support.getTransferable().getTransferData(DataFlavor.stringFlavor)) {
case "Простой" -> {
drawingCruiser = new DrawingCruiser(speed, weight, Color.WHITE,
cruiserPanel.getWidth(), cruiserPanel.getHeight(), 0, enginesNumber);
}
case "Продвинутый" -> {
drawingCruiser = new DrawingAdvancedCruiser(speed, (double) weight, Color.WHITE, Color.BLACK,
ifKitCheckBox.isSelected(), ifTentCheckBox.isSelected(), true, cruiserPanel.getWidth(), cruiserPanel.getHeight());
}
}
drawingCruiser.SetPosition(10, 10);
repaintPanel();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
cruiserPanel.setBounds(600, 100, 250, 190);
this.add(cruiserPanel);
this.setVisible(true);
}
void repaintPanel() {
Graphics g = cruiserPanel.getGraphics();
g.clearRect(0, 0, cruiserPanel.getWidth(), cruiserPanel.getHeight());
drawingCruiser.DrawTransport(g);
}
private static class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel) c).getText());
}
}
private record ColorTransferable(Color color) implements Transferable {
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "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 static class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(c.getBackground());
}
}
private static class LabelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
}
private static class PanelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
}
private record IDopTransferable(IDop obj) implements Transferable {
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "IDop");
@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 obj;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private static class IDopMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
}
private class iDopTransferable implements Transferable {
private IDop iDopObject;
private static final DataFlavor iDrawDataFlavor = new DataFlavor(IDop.class, "IDraw");
public iDopTransferable(IDop iDrawObject) {
this.iDopObject = iDrawObject;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{iDrawDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return iDrawDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return iDopObject;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class IDopComponent extends JComponent {
public IDop obj;
public IDopComponent(IDop obj) {
this.obj = obj;
this.addMouseListener(
new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((IDopComponent) e.getComponent()).getTransferHandler().exportAsDrag(((IDopComponent) e.getComponent()), e, TransferHandler.COPY);
}
}
);
this.setTransferHandler(
new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new iDopTransferable(((IDopComponent) c).obj);
}
}
);
}
}
public DrawingCruiser getSelectedCruiser() {
return SelectedCruiser;
}
}

View File

@ -1,5 +1,5 @@
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
CruiserCollectionFrame frame = new CruiserCollectionFrame(); FormCruiserConfig frame = new FormCruiserConfig();
} }
} }