PIbd21.LyovushkinaA.A.Conta.../FormShipConfig.java
2023-11-20 19:04:16 +04:00

400 lines
17 KiB
Java

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.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
import java.io.IOException;
public class FormShipConfig {
class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.DrawShip(g2d);
super.repaint();
}
}
Canvas canv;
//для перетаскивания типа объекта
private 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 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, IOException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
//для перетаскивания цвета
private class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(((JPanel)c).getBackground());
}
}
//для обработки нажатий
private class LabelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}
private class PanelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}
//что бы палубы можно было таскать
private class DeckTransferable implements Transferable {
private IDecksDrawing decksDrawing;
private static final DataFlavor deckDrawingDataFlavor = new DataFlavor(IDecksDrawing.class, "Отрисовка палуб");
public DeckTransferable(IDecksDrawing decksDrawing) {
this.decksDrawing = decksDrawing;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{deckDrawingDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(deckDrawingDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return decksDrawing;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
//для отрисовки палуб, которые можно взять и потащить
private class ComponentDeck extends JComponent{
public IDecksDrawing decksDrawing;
public ComponentDeck(IDecksDrawing _decksDrawing){
decksDrawing = _decksDrawing;
decksDrawing.setNumDecks(1);
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((ComponentDeck)e.getComponent()).getTransferHandler().exportAsDrag(((ComponentDeck)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 DeckTransferable(((ComponentDeck)c).decksDrawing);
}
}
);
}
public void paintComponent (Graphics g){
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
decksDrawing.DrawDeck(-20,-64,Color.BLACK,g2d);
super.repaint();
}
}
final int WindowHeight = 700;
final int WindowWidth = 800;
final int CanvasHeight = 600;
final int CanvasWidth = 600;
public DrawingShip _drawingShip = null;
public JButton buttonAdd;
public JFrame w;
public FormShipConfig(){
canv = new Canvas();
Border border = BorderFactory.createLineBorder(Color.GRAY);
JLabel labelSpeed = new JLabel("скорость");
JLabel labelWeight = new JLabel("вес");
JLabel labelWheelNum = new JLabel("палубы");
SpinnerModel spinnerModel = new SpinnerNumberModel(100, 100, 1000, 1);
JSpinner numericSpeed = new JSpinner(spinnerModel);
SpinnerModel spinnerModel2 = new SpinnerNumberModel(100, 100, 1000, 1);
JSpinner numericWeight = new JSpinner(spinnerModel2);
SpinnerModel spinnerModel3 = new SpinnerNumberModel(1, 1, 3, 1);
JSpinner numericDeckNum = new JSpinner(spinnerModel3);
JCheckBox checkBoxContainers = new JCheckBox("контейнеры");
JCheckBox checkBoxCrane = new JCheckBox("кран");
JPanel[] colorPanels = {
new JPanel(),new JPanel(),new JPanel(),new JPanel(),
new JPanel(),new JPanel(),new JPanel(),new JPanel(),
};
colorPanels[0].setBackground(Color.CYAN);
colorPanels[1].setBackground(Color.BLUE);
colorPanels[2].setBackground(Color.PINK);
colorPanels[3].setBackground(Color.YELLOW);
colorPanels[4].setBackground(Color.RED);
colorPanels[5].setBackground(Color.GREEN);
colorPanels[6].setBackground(Color.ORANGE);
colorPanels[7].setBackground(Color.MAGENTA);
for (var it : colorPanels){
it.setTransferHandler(new PanelTransferHandler());
it.addMouseListener(new PanelMouseAdapter());
}
JLabel labelShip = new JLabel("кораблик");
labelShip.setTransferHandler(new LabelTransferHandler());
labelShip.addMouseListener(new LabelMouseAdapter());
labelShip.setBorder(border);
labelShip.setHorizontalAlignment(SwingConstants.CENTER);
labelShip.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelContainerShip = new JLabel("контейнеровоз");
labelContainerShip.setTransferHandler(new LabelTransferHandler());
labelContainerShip.addMouseListener(new LabelMouseAdapter());
labelContainerShip.setBorder(border);
labelContainerShip.setHorizontalAlignment(SwingConstants.CENTER);
labelContainerShip.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelColor = new JLabel("цвет");
labelColor.setBorder(border);
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
labelColor.setVerticalAlignment(SwingConstants.CENTER);
labelColor.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 (_drawingShip == null)
return false;
_drawingShip.EntityShip.setBodyColor(color);
canv.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JLabel labelAdditionalColor = new JLabel("доп цвет");
labelAdditionalColor.setBorder(border);
labelAdditionalColor.setHorizontalAlignment(SwingConstants.CENTER);
labelAdditionalColor.setVerticalAlignment(SwingConstants.CENTER);
labelAdditionalColor.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 (_drawingShip == null)
return false;
if (!(_drawingShip instanceof DrawingContainerShip))
return false;
((EntityContainerShip)_drawingShip.EntityShip).setAdditionalColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JLabel labelDeck = new JLabel("палубы");
labelDeck.setBorder(border);
labelDeck.setHorizontalAlignment(SwingConstants.CENTER);
labelDeck.setVerticalAlignment(SwingConstants.CENTER);
labelDeck.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DeckTransferable.deckDrawingDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDecksDrawing decksDrawing = (IDecksDrawing)support.getTransferable().getTransferData(DeckTransferable.deckDrawingDataFlavor).getClass().getDeclaredConstructor().newInstance();
if (_drawingShip == null)
return false;
if(_drawingShip instanceof DrawingContainerShip){
decksDrawing.setNumDecks(((EntityContainerShip) _drawingShip.EntityShip).Deck);
((DrawingContainerShip)_drawingShip).iDecksDrawing = decksDrawing;
}
canv.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}
);
canv.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 {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "кораблик":
_drawingShip = new DrawingShip((int)numericSpeed.getValue(),(int)numericWeight.getValue(), Color.WHITE,CanvasWidth,CanvasHeight);
break;
case "контейнеровоз":
_drawingShip = new DrawingContainerShip((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, Color.BLACK,checkBoxCrane.isSelected(), checkBoxContainers.isSelected(),(int)numericDeckNum.getValue(), 1, CanvasWidth,CanvasHeight);
break;
}
canv.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
buttonAdd = new JButton("добавить");
JButton buttonCancel = new JButton("отмена");
buttonCancel.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
w.dispose();
}
}
);
ComponentDeck componentSimple = new ComponentDeck(new DrawingDecks());
ComponentDeck componentRect = new ComponentDeck(new DrawingDecksRect());
ComponentDeck componentTrapez = new ComponentDeck(new DrawingDecksTrapez());
labelSpeed.setBounds(10,10,40,20);
labelWeight.setBounds(10,40,40,20);
labelWheelNum.setBounds(10,70,40,20);
numericSpeed.setBounds(55,10,80,20);
numericWeight.setBounds(55,40,80,20);
numericDeckNum.setBounds(55,70,80,20);
checkBoxContainers.setBounds(10,100,120,20);
checkBoxCrane.setBounds(10,130,120,20);
for (int i = 0; i < colorPanels.length; i+=2){
colorPanels[i].setBounds(10,200+i/2*60,50,50);
colorPanels[i+1].setBounds(70,200+i/2*60,50,50);
}
componentSimple.setBounds(10,470,110,20);
componentRect.setBounds(10,500,110,20);
componentTrapez.setBounds(10,530,110,20);
labelShip.setBounds(10,560 ,50,30);
labelContainerShip.setBounds(70,560 ,50,30);
labelColor.setBounds(WindowWidth-CanvasWidth, 10, CanvasWidth/3, 30);
labelAdditionalColor.setBounds(WindowWidth-CanvasWidth + CanvasWidth/3, 10, CanvasWidth/3, 30);
labelDeck.setBounds(WindowWidth-CanvasWidth + CanvasWidth*2/3, 10, CanvasWidth/3, 30);
canv.setBounds(WindowWidth-CanvasWidth, 50, CanvasWidth, CanvasHeight);
buttonAdd.setBounds(WindowWidth-CanvasWidth, CanvasHeight+60, CanvasWidth/3, 30);
buttonCancel.setBounds(WindowWidth-CanvasWidth + CanvasWidth*2/3, CanvasHeight+60, CanvasWidth/3, 30);
w = new JFrame();
w.setSize (WindowWidth+20, WindowHeight+40);
w.setLayout(null);
w.add(labelSpeed);
w.add(labelWeight);
w.add(labelWheelNum);
w.add(numericSpeed);
w.add(numericWeight);
w.add(numericDeckNum);
w.add(checkBoxContainers);
w.add(checkBoxCrane);
for (var it : colorPanels)
w.add(it);
w.add(labelShip);
w.add(labelContainerShip);
w.add(labelColor);
w.add(labelAdditionalColor);
w.add(labelDeck);
w.add(canv);
w.add(buttonAdd);
w.add(buttonCancel);
w.add(componentSimple);
w.add(componentRect);
w.add(componentTrapez);
w.setVisible(true);
}
}