This commit is contained in:
AnnaLioness 2023-11-20 19:04:16 +04:00
parent b836adc7ce
commit 6ce40d0e13
6 changed files with 423 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import java.awt.*;
public class DrawingContainerShip extends DrawingShip{ public class DrawingContainerShip extends DrawingShip{
private IDecksDrawing iDecksDrawing; public IDecksDrawing iDecksDrawing;
public DrawingContainerShip(int speed, double weight, Color bodyColor, Color public DrawingContainerShip(int speed, double weight, Color bodyColor, Color
additionalColor, boolean crane, boolean containers,int deck, int deckType, int width, int height) additionalColor, boolean crane, boolean containers,int deck, int deckType, int width, int height)

View File

@ -24,7 +24,23 @@ public class DrawingDecks implements IDecksDrawing{
} }
public void DrawDeck(int _startPosX, int _startPosY,Color bodyColor, Graphics2D g){ public void DrawDeck(int _startPosX, int _startPosY,Color bodyColor, Graphics2D g){
if(numDecks == NumberOfDecks.Deck_1){ if(numDecks == NumberOfDecks.Deck_1){
g.setPaint(bodyColor);
// заполнение борта
int x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 5);
//борт корабля контур
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 5);
//рисунок на борту
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
} }
if(numDecks == NumberOfDecks.Deck_2){ if(numDecks == NumberOfDecks.Deck_2){
g.setPaint(Color.BLACK); g.setPaint(Color.BLACK);
@ -36,4 +52,5 @@ public class DrawingDecks implements IDecksDrawing{
g.drawLine(_startPosX + 125, _startPosY + 70, _startPosX+ 50, _startPosY + 70); g.drawLine(_startPosX + 125, _startPosY + 70, _startPosX+ 50, _startPosY + 70);
} }
} }
} }

View File

@ -17,4 +17,5 @@ additionalColor, boolean crane, boolean containers, int deck, int deckType)
Deck = deck; Deck = deck;
DeckType = deckType; DeckType = deckType;
} }
public void setAdditionalColor(Color color){AdditionalColor = color;}
} }

View File

@ -11,4 +11,5 @@ public class EntityShip {
BodyColor = bodyColor; BodyColor = bodyColor;
Step = (double)Speed * 100 / Weight; Step = (double)Speed * 100 / Weight;
} }
public void setBodyColor(Color color){BodyColor = color;}
} }

View File

@ -79,11 +79,11 @@ public class FormShipCollection {
{ {
return; return;
} }
FormContainerShip form = new FormContainerShip(); FormShipConfig form = new FormShipConfig();
form.buttonSelectedShip.addActionListener( form.buttonAdd.addActionListener(
new ActionListener(){ new ActionListener(){
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if(obj.Add(form._drawingShip) != -1){ if(obj.Add(form._drawingShip) != -1 && obj != null){
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw(); Draw();
} }

399
FormShipConfig.java Normal file
View File

@ -0,0 +1,399 @@
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);
}
}