Pibd-12 Pyzhov_E.A. LabWork05 Hard #6

Closed
pyzhov.egor wants to merge 2 commits from LabWork05 into LabWork04
8 changed files with 552 additions and 50 deletions
Showing only changes of commit 957c518be2 - Show all commits

View File

@ -3,6 +3,7 @@ package Drawnings;
import CollectionGenericObjects.*;
import CollectionGenericObjects.MassiveGenericObjects;
import Forms.FormBoatConfig;
import Forms.FormCatamaran;
import Forms.FormConstructor;
@ -14,7 +15,7 @@ import java.util.Stack;
public class DrawningAbstractCompany extends JComponent {
private AbstractCompany _company = null;
DrawningBoat _drawningBoat;
private final StorageCollection<DrawningBoat> storageCollection = new StorageCollection<>();
private Stack<DrawningBoat> rubbishBinStack = new Stack<>();
public boolean collectionComboBox_SelectedIndexChanged(JFrame frame, JList<String> collectionList, JComboBox<String> obj, int width, int height) {
@ -31,41 +32,26 @@ public class DrawningAbstractCompany extends JComponent {
return false;
}
}
public void createObject(int type, JFrame obj) {
public void createObject(JFrame obj) {
if (_company == null) {
return;
}
DrawningBoat _drawningBoat;
Random random = new Random();
switch (type) {
case 0:
_drawningBoat = new DrawningBoat(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
getColorR(obj, random), random.nextInt(3));
break;
case 1:
_drawningBoat = new DrawningCatamaran(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
getColorR(obj, random), random.nextInt(3),
getColorR(obj, random),
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
if (AbstractCompany.add(_company, _drawningBoat) != -1) {
JOptionPane.showMessageDialog(obj, "Объект добавлен");
}
else {
JOptionPane.showMessageDialog(obj, "Не удалось добавить объект");
}
}
Color getColorR(JFrame obj, Random rnd) {
Color color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
if (obj == null) {
return color;
}
JColorChooser colorChooser = new JColorChooser();
colorChooser.setColor(color);
return JColorChooser.showDialog(obj, "Выберите цвет", color);
FormBoatConfig formBoatConfig = new FormBoatConfig();
formBoatConfig.OpenFrame();
formBoatConfig.getButtonAdd().addActionListener(e -> {
if (formBoatConfig.getDrawningConfig().boat != null) {
_drawningBoat = formBoatConfig.getDrawningConfig().boat;
if (AbstractCompany.add(_company, _drawningBoat) != -1) {
JOptionPane.showMessageDialog(obj, "Объект добавлен");
obj.repaint();
}
else {
JOptionPane.showMessageDialog(obj, "Не удалось добавить объект");
}
formBoatConfig.getjFrameBoatConfig().dispatchEvent(new WindowEvent(formBoatConfig.getjFrameBoatConfig(), WindowEvent.WINDOW_CLOSING));
}
});
}
public boolean deleteButtonAction(int val, Frame obj) {

View File

@ -20,7 +20,13 @@ public class DrawningBoat {
public Integer GetPosY(){return _startPosY;}
public int GetWidth(){return _drawingBoatWidth;}
public int GetHeight(){return _drawingBoatHeight;}
public IDrawPaddles drawPaddles;
protected IDrawPaddles drawPaddles;
public IDrawPaddles getDrawPaddles() {
return drawPaddles;
}
public void setDrawPaddles(IDrawPaddles drawPaddles) {
this.drawPaddles = drawPaddles;
}
public DrawningBoat(int speed, float weight, Color bodyColor, int paddlesType) {
entityBoat = new EntityBoat(speed, weight, bodyColor);

View File

@ -0,0 +1,19 @@
package Drawnings;
import javax.swing.*;
import java.awt.*;
public class DrawningBoatConfig extends JComponent {
public DrawningBoat boat = null;
@Override
public void paintComponent(Graphics g) {
if (boat == null) {
return;
}
super.paintComponent(g);
boat.setPosition(10, 10);
boat.setPictureSize(200, 200);
boat.drawBoat(g);
super.repaint();
}
}

View File

@ -7,11 +7,20 @@ public class EntityBoat {
public int getSpeed() {
return Speed;
}
public void setSpeed(int speed) {
Speed = speed;
}
private double Weight;
public void setWeight(double weight){
Weight = weight;
}
public double getWeight() {
return Weight;
}
private Color BodyColor;
public void setBodyColor(Color bodyColor) {
BodyColor = bodyColor;
}
public Color getBodyColor() {
return BodyColor;
}

View File

@ -7,14 +7,23 @@ public class EntityCatamaran extends EntityBoat {
public Color getAdditionalColor() {
return AdditionalColor;
}
public void setAdditionalColor(Color additionalColor) {
AdditionalColor = additionalColor;
}
private boolean Sail;
public boolean getSail() {
return Sail;
}
public void setSail(boolean sail) {
Sail = sail;
}
private boolean Floaters;
public boolean getFloaters() {
return Floaters;
}
public void setFloaters(boolean floaters) {
Floaters = floaters;
}
public EntityCatamaran(int speed, double weight, Color bodyColor ,Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;

View File

@ -1,6 +1,6 @@
package Forms;
import CollectionGenericObjects.CollectionType;
import CollectionGenericObjects.StorageCollection;
import Drawnings.DrawningAbstractCompany;
import Drawnings.DrawningBoat;
@ -12,7 +12,6 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
public class FormBoatCollection extends JFrame {
final private JFrame jFrameCollectionBoats = new JFrame();
@ -30,9 +29,7 @@ public class FormBoatCollection extends JFrame {
final private JLabel toolsNameLabel = new JLabel("Инструменты");
final private JPanel labelPanel = new JPanel();
final private JButton buttonAddBoat = new JButton("Добавить лодку");
final private JButton buttonAddCatamaran = new JButton("Добавить катамаран");
final private JPanel addBoatPanel = new JPanel();
final private JPanel addCatamaranPanel = new JPanel();
final private JButton buttonRemove = new JButton("Удалить лолку");
final private JPanel removePanel = new JPanel();
final private JButton goToCheckButton = new JButton("Отправит на тесты");
@ -89,10 +86,6 @@ public class FormBoatCollection extends JFrame {
addBoatPanel.setSize(170, 25);
addBoatPanel.add(buttonAddBoat, BorderLayout.CENTER);
addCatamaranPanel.setLayout(new BorderLayout());
addCatamaranPanel.setSize(170, 25);
addCatamaranPanel.add(buttonAddCatamaran, BorderLayout.CENTER);
removePanel.setLayout(new BorderLayout());
removePanel.setSize(170, 25);
removePanel.add(buttonRemove, BorderLayout.CENTER);
@ -168,7 +161,6 @@ public class FormBoatCollection extends JFrame {
jFrameCollectionBoats.add(comboBoxPanel);
jFrameCollectionBoats.add(createCompanyPanel);
jFrameCollectionBoats.add(addBoatPanel);
jFrameCollectionBoats.add(addCatamaranPanel);
jFrameCollectionBoats.add(textBoxPanel);
jFrameCollectionBoats.add(removePanel);
jFrameCollectionBoats.add(goToCheckPanel);
@ -178,7 +170,6 @@ public class FormBoatCollection extends JFrame {
jFrameCollectionBoats.add(_company);
listOfDownPanel.add(buttonAddBoat);
listOfDownPanel.add(buttonAddCatamaran);
listOfDownPanel.add(addFromConstructorButton);
listOfDownPanel.add(textBoxPosition);
listOfDownPanel.add(buttonRemove);
@ -204,7 +195,6 @@ public class FormBoatCollection extends JFrame {
goGoToCheckFromRubbishBinPanel.setLocation(jFrameCollectionBoats.getWidth() - 200,
jFrameCollectionBoats.getHeight() - 295);
addBoatPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 241);
addCatamaranPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 214);
addFromConstructorPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 187);
textBoxPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 146);
removePanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 119);
@ -217,11 +207,7 @@ public class FormBoatCollection extends JFrame {
});
buttonAddBoat.addActionListener(e -> {
_company.createObject(0, jFrameCollectionBoats);
jFrameCollectionBoats.repaint();
});
buttonAddCatamaran.addActionListener(e -> {
_company.createObject(1, jFrameCollectionBoats);
_company.createObject(jFrameCollectionBoats);
jFrameCollectionBoats.repaint();
});

View File

@ -0,0 +1,484 @@
package Forms;
import Drawnings.*;
import Entities.EntityBoat;
import Entities.EntityCatamaran;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
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.*;
import java.io.IOException;
public class FormBoatConfig extends JFrame {
final private JFrame jFrameBoatConfig = new JFrame();
public JFrame getjFrameBoatConfig() {
return jFrameBoatConfig;
}
final private DrawningBoatConfig drawningConfig = new DrawningBoatConfig();
public DrawningBoatConfig getDrawningConfig() {
return drawningConfig;
}
final private JSpinner spinnerSpeed = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 100));
final private JSpinner spinnerWeight = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 100));
final private JSpinner spinnerPaddles = new JSpinner(new SpinnerNumberModel(2, 2, 6, 2));
final private JCheckBox checkBoxSail = new JCheckBox();
final private JCheckBox checkBoxFloaters = new JCheckBox();
final private JPanel CheckBoxesPanel = new JPanel();
final private JPanel SpinnersPanel = new JPanel();
final private JPanel PicturePanel = new JPanel();
final private JLabel labelBodyColor = new JLabel("Основной цвет");
final private JLabel labelAdditionalColor = new JLabel("Доп цвет");
final private JPanel BoatColorsPanel = new JPanel();
final private JLabel labelBoat = new JLabel("Простой");
final private JLabel labelCatamaran = new JLabel("Продвинутый");
final private JPanel TypeBoatPanel = new JPanel();
final private JPanel ColorsPanel = new JPanel();
final private JPanel panelRed = new JPanel();
final private JPanel panelGreen = new JPanel();
final private JPanel panelBlue = new JPanel();
final private JPanel panelYellow = new JPanel();
final private JPanel panelWhite = new JPanel();
final private JPanel panelBlack = new JPanel();
final private JPanel panelGray = new JPanel();
final private JPanel panelPink = new JPanel();
final private JLabel labelColor = new JLabel("Цвет");
final private JPanel labelColorPanel = new JPanel();
final private JLabel labelDefaultPaddles = new JLabel("Обычные");
final private JLabel labelOvalPaddles = new JLabel("Овал");
final private JLabel labelRectanglesPaddles = new JLabel("Прямоугольники");
final private JPanel PaddlesPanel = new JPanel();
final private JLabel labelSpeed = new JLabel("Скорость:");
final private JLabel labelWeight = new JLabel("Вес:");
final private JLabel labelPaddles = new JLabel("Весла:");
final private JLabel labelSail = new JLabel("Парус:");
final private JLabel labelFloaters = new JLabel("Поплавки:");
final private JButton buttonAdd = new JButton("Создать");
final private JPanel buttonAddPanel = new JPanel();
public JButton getButtonAdd() {
return buttonAdd;
}
final private JButton buttonCancel = new JButton("Отмена");
final private JPanel buttonCancelPanel = new JPanel();
public void OpenFrame() {
jFrameBoatConfig.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Toolkit tolls = Toolkit.getDefaultToolkit();
Dimension dimension = tolls.getScreenSize();
jFrameBoatConfig.setBounds(dimension.width / 2 - 250, dimension.height / 2 - 250,
700, 330);
jFrameBoatConfig.setTitle("Создание объекта");
SpinnersPanel.setLayout(new GridLayout(3, 2));
labelSpeed.setSize(new Dimension(50, 30));
spinnerSpeed.setSize(new Dimension(120, 30));
labelWeight.setSize(new Dimension(50, 30));
spinnerWeight.setSize(new Dimension(120, 30));
labelPaddles.setSize(new Dimension(50, 30));
spinnerPaddles.setSize(new Dimension(120, 30));
SpinnersPanel.setSize(new Dimension(170, 90));
SpinnersPanel.add(labelSpeed);
SpinnersPanel.add(spinnerSpeed);
SpinnersPanel.add(labelWeight);
SpinnersPanel.add(spinnerWeight);
SpinnersPanel.add(labelPaddles);
SpinnersPanel.add(spinnerPaddles);
CheckBoxesPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
CheckBoxesPanel.setSize(new Dimension(100, 90));
CheckBoxesPanel.add(labelSail);
CheckBoxesPanel.add(checkBoxSail);
CheckBoxesPanel.add(labelFloaters);
CheckBoxesPanel.add(checkBoxFloaters);
labelBoat.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelCatamaran.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
TypeBoatPanel.setLayout(new BorderLayout());
TypeBoatPanel.setSize(new Dimension(140, 42));
TypeBoatPanel.add(labelBoat, BorderLayout.WEST);
TypeBoatPanel.add(labelCatamaran, BorderLayout.EAST);
PicturePanel.setSize(new Dimension(220, 130));
PicturePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelBodyColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelAdditionalColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
BoatColorsPanel.setLayout(new BorderLayout());
BoatColorsPanel.setSize(new Dimension(150, 50));
BoatColorsPanel.add(labelBodyColor, BorderLayout.WEST);
BoatColorsPanel.add(labelAdditionalColor, BorderLayout.EAST);
labelDefaultPaddles.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelOvalPaddles.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelRectanglesPaddles.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
PaddlesPanel.setLayout(new BorderLayout(5, 5));
PaddlesPanel.setSize(new Dimension(210, 42));
PaddlesPanel.add(labelDefaultPaddles, BorderLayout.WEST);
PaddlesPanel.add(labelOvalPaddles, BorderLayout.CENTER);
PaddlesPanel.add(labelRectanglesPaddles, BorderLayout.EAST);
panelRed.setBackground(Color.RED);
panelGreen.setBackground(Color.GREEN);
panelBlue.setBackground(Color.BLUE);
panelYellow.setBackground(Color.YELLOW);
panelWhite.setBackground(Color.WHITE);
panelBlack.setBackground(Color.BLACK);
panelGray.setBackground(Color.GRAY);
panelPink.setBackground(Color.PINK);
ColorsPanel.setLayout(new GridLayout(2, 4, 5, 5));
ColorsPanel.setSize(new Dimension(150, 75));
ColorsPanel.add(panelRed);
ColorsPanel.add(panelGreen);
ColorsPanel.add(panelBlue);
ColorsPanel.add(panelYellow);
ColorsPanel.add(panelWhite);
ColorsPanel.add(panelBlack);
ColorsPanel.add(panelGray);
ColorsPanel.add(panelPink);
labelColorPanel.setLayout(new BorderLayout());
labelColorPanel.setSize(new Dimension(70, 30));
labelColorPanel.add(labelColor, BorderLayout.CENTER);
buttonAddPanel.setLayout(new BorderLayout());
buttonAddPanel.setSize(new Dimension(90, 40));
buttonAddPanel.add(buttonAdd, BorderLayout.CENTER);
buttonCancelPanel.setLayout(new BorderLayout());
buttonCancelPanel.setSize(new Dimension(90, 40));
buttonCancelPanel.add(buttonCancel, BorderLayout.CENTER);
jFrameBoatConfig.setLayout(null);
jFrameBoatConfig.add(buttonCancelPanel);
jFrameBoatConfig.add(buttonAddPanel);
jFrameBoatConfig.add(PaddlesPanel);
jFrameBoatConfig.add(CheckBoxesPanel);
jFrameBoatConfig.add(TypeBoatPanel);
jFrameBoatConfig.add(labelColorPanel);
jFrameBoatConfig.add(ColorsPanel);
jFrameBoatConfig.add(PicturePanel);
jFrameBoatConfig.add(BoatColorsPanel);
jFrameBoatConfig.add(SpinnersPanel);
jFrameBoatConfig.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
SpinnersPanel.setLocation(jFrameBoatConfig.getWidth()-650, 10);
CheckBoxesPanel.setLocation(jFrameBoatConfig.getWidth()-650, 115);
TypeBoatPanel.setLocation(jFrameBoatConfig.getWidth()-670, 235);
BoatColorsPanel.setLocation(jFrameBoatConfig.getWidth()-237, 35);
labelColorPanel.setLocation(jFrameBoatConfig.getWidth()-390, 5);
PicturePanel.setLocation(jFrameBoatConfig.getWidth()-270, 95);
ColorsPanel.setLocation(jFrameBoatConfig.getWidth()-440, 35);
PaddlesPanel.setLocation(jFrameBoatConfig.getWidth()-500, 235);
buttonAddPanel.setLocation(jFrameBoatConfig.getWidth()-225, 235);
buttonCancelPanel.setLocation(jFrameBoatConfig.getWidth()-125, 235);
jFrameBoatConfig.repaint();
}
});
MouseAdapter labelObjectsMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
TransferHandler labelObjectsTransferHandler = new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel) c).getText());
}
};
MouseAdapter labelPaddlesMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
labelBoat.addMouseListener(labelObjectsMouseDown);
labelBoat.setTransferHandler(labelObjectsTransferHandler);
labelCatamaran.addMouseListener(labelObjectsMouseDown);
labelCatamaran.setTransferHandler(labelObjectsTransferHandler);
labelDefaultPaddles.addMouseListener(labelPaddlesMouseDown);
labelRectanglesPaddles.addMouseListener(labelPaddlesMouseDown);
labelOvalPaddles.addMouseListener(labelPaddlesMouseDown);
labelDefaultPaddles.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new PaddlesTransferable(new DrawningPaddles());
}
});
labelOvalPaddles.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new PaddlesTransferable(new DrawningOvalPaddles());
}
});
labelRectanglesPaddles.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new PaddlesTransferable(new DrawningRectanglePaddles());
}
});
PicturePanel.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor)
|| support.isDataFlavorSupported(PaddlesTransferable.paddlesDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
IDrawPaddles paddles = new DrawningPaddles();
paddles.setNumber((int) spinnerPaddles.getValue() / 2);
switch (data) {
case "Простой":
drawningConfig.boat = new DrawningBoat(new EntityBoat((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(), Color.WHITE), paddles);
PicturePanel.repaint();
return true;
case "Продвинутый":
drawningConfig.boat = new DrawningCatamaran(new EntityCatamaran((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE, Color.BLACK, checkBoxSail.isSelected(), checkBoxFloaters.isSelected()), paddles);
PicturePanel.repaint();
return true;
}
}catch (UnsupportedFlavorException | IOException e) {}
try {
IDrawPaddles drawPaddles =
(IDrawPaddles) support.getTransferable().getTransferData(PaddlesTransferable.paddlesDataFlavor);
drawningConfig.boat.setDrawPaddles(drawPaddles);
drawningConfig.boat.getDrawPaddles().setNumber((int) spinnerPaddles.getValue() / 2);
}catch (UnsupportedFlavorException | IOException e) {}
PicturePanel.repaint();
return false;
}
});
PicturePanel.setLayout(new BorderLayout());
PicturePanel.add(drawningConfig, BorderLayout.CENTER);
MouseAdapter colorMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
};
panelBlack.addMouseListener(colorMouseDown);
panelBlack.setTransferHandler(new ColorTransferHandler());
panelBlue.addMouseListener(colorMouseDown);
panelBlue.setTransferHandler(new ColorTransferHandler());
panelGreen.addMouseListener(colorMouseDown);
panelGreen.setTransferHandler(new ColorTransferHandler());
panelGray.addMouseListener(colorMouseDown);
panelGray.setTransferHandler(new ColorTransferHandler());
panelPink.addMouseListener(colorMouseDown);
panelPink.setTransferHandler(new ColorTransferHandler());
panelRed.addMouseListener(colorMouseDown);
panelRed.setTransferHandler(new ColorTransferHandler());
panelYellow.addMouseListener(colorMouseDown);
panelYellow.setTransferHandler(new ColorTransferHandler());
panelWhite.addMouseListener(colorMouseDown);
panelWhite.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 (drawningConfig.boat == null) return false;
drawningConfig.boat.entityBoat.setBodyColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
labelAdditionalColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
if (!(drawningConfig.boat instanceof DrawningCatamaran)) return false;
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawningConfig.boat == null) return false;
if (drawningConfig.boat.entityBoat instanceof EntityCatamaran catamaran) {
catamaran.setAdditionalColor(color);
return true;
}
return false;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
spinnerPaddles.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.boat == null) {
return;
}
drawningConfig.boat.getDrawPaddles().setNumber((int) spinnerPaddles.getValue() / 2);
PicturePanel.repaint();
}
});
spinnerWeight.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.boat == null) {
return;
}
drawningConfig.boat.entityBoat.setWeight((int) spinnerWeight.getValue());
PicturePanel.repaint();
}
});
spinnerSpeed.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.boat == null) {
return;
}
drawningConfig.boat.entityBoat.setSpeed((int) spinnerSpeed.getValue());
PicturePanel.repaint();
}
});
checkBoxSail.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.boat == null) {
return;
}
if (drawningConfig.boat.entityBoat instanceof EntityCatamaran) {
((EntityCatamaran) drawningConfig.boat.entityBoat).setSail(checkBoxSail.isSelected());
PicturePanel.repaint();
}
}
});
checkBoxFloaters.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.boat == null) {
return;
}
if (drawningConfig.boat.entityBoat instanceof EntityCatamaran) {
((EntityCatamaran) drawningConfig.boat.entityBoat).setFloaters(checkBoxFloaters.isSelected());
PicturePanel.repaint();
}
}
});
buttonCancel.addActionListener(e -> {
jFrameBoatConfig.dispose();
}
);
jFrameBoatConfig.setVisible(true);
}
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());
}
}
private class PaddlesTransferable implements Transferable {
private IDrawPaddles paddles;
private static final DataFlavor paddlesDataFlavor = new DataFlavor(IDrawPaddles.class, "Paddles");
public PaddlesTransferable(IDrawPaddles paddles) {
this.paddles = paddles;
this.paddles.setNumber((int) spinnerPaddles.getValue() / 2);
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{paddlesDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(paddlesDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return paddles;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
}

View File

@ -1,7 +1,10 @@
import Forms.FormBoatCollection;
import Forms.FormBoatConfig;
public class Main {
public static void main(String[] args) {
FormBoatCollection formBoatCollection = new FormBoatCollection();
formBoatCollection.OpenFrame();
}
}