diff --git a/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java b/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java index 854b028..ae81788 100644 --- a/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java +++ b/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java @@ -13,6 +13,14 @@ public class DrawingDumpTruck extends DrawingTruck { setEntityTruck(new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor)); } } + public DrawingDumpTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels) + { + super(speed, weight, bodyColor, width, height, wheelNumber, iDrawingWheels); + if (getEntityTruck() != null) + { + setEntityTruck(new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor)); + } + } public DrawingDumpTruck(EntityDumpTruck entityDumpTruck, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels) { super(entityDumpTruck, width, height, wheelNumber, iDrawingWheels); diff --git a/src/DumpTruck/DrawingObjects/DrawingTruck.java b/src/DumpTruck/DrawingObjects/DrawingTruck.java index efe6923..eddb5ec 100644 --- a/src/DumpTruck/DrawingObjects/DrawingTruck.java +++ b/src/DumpTruck/DrawingObjects/DrawingTruck.java @@ -9,7 +9,7 @@ import DumpTruck.MovementStrategy.*; import DumpTruck.Wheels.*; public class DrawingTruck { - private EntityTruck entityTruck; + public EntityTruck entityTruck; public EntityTruck getEntityTruck() { return entityTruck; @@ -19,10 +19,10 @@ public class DrawingTruck { this.entityTruck = entityTruck; } - private IDrawingWheels drawingWheels; + public IDrawingWheels drawingWheels; - private int _pictureWidth; - private int _pictureHeight; + public int _pictureWidth; + public int _pictureHeight; protected int _startPosX; protected int _startPosY; protected int _truckWidth = 160; @@ -65,6 +65,18 @@ public class DrawingTruck { drawingWheels.setWheelNumber(wheelNumber); } + public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels) + { + if (height < _truckHeight || width < _truckWidth) { + return; + } + _pictureWidth = width; + _pictureHeight = height; + entityTruck = new EntityTruck(speed, weight, bodyColor); + drawingWheels = iDrawingWheels; + drawingWheels.setWheelNumber(wheelNumber); + } + protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight, int wheelNumber) { if (width < truckWidth || height < truckHeight) return; diff --git a/src/DumpTruck/Entities/EntityDumpTruck.java b/src/DumpTruck/Entities/EntityDumpTruck.java index da9a3b1..4acc29e 100644 --- a/src/DumpTruck/Entities/EntityDumpTruck.java +++ b/src/DumpTruck/Entities/EntityDumpTruck.java @@ -20,13 +20,16 @@ public class EntityDumpTruck extends EntityTruck { public Color getDumpBoxColor() { return DumpBoxColor; } - + public void setDumpBoxColor(Color color) { + DumpBoxColor = color; + } private Color TentColor; - public Color getTentColor() { return TentColor; } - + public void setTentColor(Color color) { + TentColor = color; + } public EntityDumpTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor) { super(speed, weight, bodyColor); Tent = tent; diff --git a/src/DumpTruck/Entities/EntityTruck.java b/src/DumpTruck/Entities/EntityTruck.java index fb1f286..ca15e7b 100644 --- a/src/DumpTruck/Entities/EntityTruck.java +++ b/src/DumpTruck/Entities/EntityTruck.java @@ -21,6 +21,9 @@ public class EntityTruck public Color getBodyColor() { return BodyColor; } + public void setBodyColor(Color color) { + BodyColor = color; + } public double getStep() { return (double)Speed * 100 / Weight; diff --git a/src/DumpTruck/FrameTruckConfig.java b/src/DumpTruck/FrameTruckConfig.java new file mode 100644 index 0000000..8bae2aa --- /dev/null +++ b/src/DumpTruck/FrameTruckConfig.java @@ -0,0 +1,17 @@ +package DumpTruck; + +import javax.swing.*; + +public class FrameTruckConfig extends JFrame { + + public PictureBoxTruckConfig pictureBoxTruckConfig; + + public FrameTruckConfig() { + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + pictureBoxTruckConfig = new PictureBoxTruckConfig(); + add(pictureBoxTruckConfig); + pack(); + setLocationRelativeTo(null); + setVisible(true); + } +} \ No newline at end of file diff --git a/src/DumpTruck/Generics/TrucksGenericCollection.java b/src/DumpTruck/Generics/TrucksGenericCollection.java index a49c137..1b5c910 100644 --- a/src/DumpTruck/Generics/TrucksGenericCollection.java +++ b/src/DumpTruck/Generics/TrucksGenericCollection.java @@ -61,6 +61,8 @@ public class TrucksGenericCollection { - if (obj.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) { + FrameTruckConfig frameTruckConfig = new FrameTruckConfig(); + frameTruckConfig.pictureBoxTruckConfig.buttonAdd.addActionListener(e1 -> { + if (obj != null && obj.Add(frameTruckConfig.pictureBoxTruckConfig._truck) != -1) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } - frameDumpTruck.dispose(); + frameTruckConfig.dispose(); repaint(); } ); + frameTruckConfig.pictureBoxTruckConfig.buttonCancel.addActionListener(e1 -> frameTruckConfig.dispose()); }); add(buttonAddTruck); diff --git a/src/DumpTruck/PictureBoxTruckConfig.java b/src/DumpTruck/PictureBoxTruckConfig.java new file mode 100644 index 0000000..f47ebe8 --- /dev/null +++ b/src/DumpTruck/PictureBoxTruckConfig.java @@ -0,0 +1,484 @@ +package DumpTruck; + +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.ActionListener; +import java.io.IOException; +import java.util.Random; +import DumpTruck.DrawingObjects.*; +import DumpTruck.Entities.EntityDumpTruck; +import DumpTruck.Entities.EntityTruck; +import DumpTruck.Wheels.DrawingWheels; +import DumpTruck.Wheels.DrawingWheelsCircles; +import DumpTruck.Wheels.DrawingWheelsStar; +import DumpTruck.Wheels.IDrawingWheels; + +import java.awt.event.*; + +public class PictureBoxTruckConfig extends JPanel { + 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 WheelTransferable implements Transferable { + private IDrawingWheels wheelDrawing; + private static final DataFlavor wheelDrawingDataFlavor = new DataFlavor(IDrawingWheels.class, "Wheel Drawing"); + + public WheelTransferable(IDrawingWheels wheelDrawing) { + this.wheelDrawing = wheelDrawing; + } + + @Override + public DataFlavor[] getTransferDataFlavors() { + return new DataFlavor[]{wheelDrawingDataFlavor}; + } + + @Override + public boolean isDataFlavorSupported(DataFlavor flavor) { + return flavor.equals(wheelDrawingDataFlavor); + } + + @Override + public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { + if (isDataFlavorSupported(flavor)) { + return wheelDrawing; + } else { + throw new UnsupportedFlavorException(flavor); + } + } + } + private class WheelPanel extends JPanel{ + public IDrawingWheels wheelDrawing; + + public WheelPanel(IDrawingWheels _wheelDrawing){ + wheelDrawing = _wheelDrawing; + wheelDrawing.setWheelNumber(2); + this.addMouseListener( + new MouseAdapter(){ + @Override + public void mousePressed(MouseEvent e) { + ((WheelPanel)e.getComponent()).getTransferHandler().exportAsDrag(((WheelPanel)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 WheelTransferable(((WheelPanel)c).wheelDrawing); + } + } + ); + } + + public void paintComponent (Graphics g){ + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + wheelDrawing.drawWheels(g2d,Color.BLACK,0, -50); + } + } + + private class TruckPreview extends JPanel{ + + @Override + public void paintComponent (Graphics g) { + if (_truck == null) { + return; + } + _truck.SetPosition(20, 20); + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + _truck.DrawTransport(g2d); + } + } + DrawingTruck _truck = null; + JButton buttonAdd, buttonCancel; + JLabel labelParameters, labelColors, labelSpeed, labelWeight, labelWheelAmount, labelSimpleObject, labelModifiedObject, labelBodyColor, labelDumboxColor, labelTentColor, labelWheels, labelWheelPreview; + JCheckBox checkBoxTent, checkBoxDumpBox; + SpinnerModel spinnerModelSpeed, spinnerModelWeight, spinnerModelWheelAmount; + JSpinner numericUpDownSpeed, numericUpDownWeight, numericUpDownWheelAmount; + JPanel panelRed, panelGreen, panelBlue, panelYellow, panelWhite, panelGray, panelBlack, panelMagenta; + TruckPreview truckPreview; + public PictureBoxTruckConfig() { + Random random = new Random(); + setLayout(null); + setBounds(0, 0, 800, 300); + buttonAdd = new JButton("Добавить"); + buttonAdd.setFocusable(false); + buttonAdd.setBounds(550, 260, 100, 30); + add(buttonAdd); + + buttonCancel = new JButton("Отмена"); + buttonCancel.setFocusable(false); + buttonCancel.setBounds(670, 260, 100, 30); + add(buttonCancel); + + labelParameters = new JLabel("Параметры"); + labelParameters.setBounds(10, 10, 100, 20); + add(labelParameters); + + labelSpeed = new JLabel("Скорость:"); + labelSpeed.setBounds(10, 50, 100, 20); + add(labelSpeed); + + labelWeight = new JLabel("Вес:"); + labelWeight.setBounds(10, 80, 100, 20); + add(labelWeight); + + labelWheelAmount = new JLabel("Кол-во колес:"); + labelWheelAmount.setBounds(10, 110, 100, 20); + add(labelWheelAmount); + + checkBoxTent = new JCheckBox("Признак наличия кузова"); + checkBoxTent.setBounds(10, 200, 200, 20); + checkBoxTent.setFocusable(false); + add(checkBoxTent); + + checkBoxDumpBox = new JCheckBox("Признак наличия тента"); + checkBoxDumpBox.setBounds(10, 230, 200, 20); + checkBoxDumpBox.setFocusable(false); + add(checkBoxDumpBox); + + spinnerModelSpeed = new SpinnerNumberModel(100, 100, 1000, 1); + numericUpDownSpeed = new JSpinner(spinnerModelSpeed); + numericUpDownSpeed.setBounds(100, 50, 50, 20); + numericUpDownSpeed.setFocusable(false); + add(numericUpDownSpeed); + + spinnerModelWeight = new SpinnerNumberModel(100, 100, 1000, 1); + numericUpDownWeight = new JSpinner(spinnerModelWeight); + numericUpDownWeight.setBounds(100, 80, 50, 20); + numericUpDownWeight.setFocusable(false); + add(numericUpDownWeight); + + spinnerModelWheelAmount = new SpinnerNumberModel(2, 2, 4, 1); + numericUpDownWheelAmount = new JSpinner(spinnerModelWheelAmount); + numericUpDownWheelAmount.setBounds(100, 110, 50, 20); + numericUpDownWheelAmount.setFocusable(false); + add(numericUpDownWheelAmount); + + labelColors = new JLabel("Цвета"); + labelColors.setBounds(250, 20, 100, 20); + add(labelColors); + + Border border = BorderFactory.createLineBorder(Color.BLACK); + + labelSimpleObject = new JLabel("Простой"); + labelSimpleObject.setBounds(250, 240, 100, 50); + labelSimpleObject.setHorizontalAlignment(SwingConstants.CENTER); + labelSimpleObject.setBorder(border); + labelSimpleObject.setTransferHandler(new LabelTransferHandler()); + labelSimpleObject.addMouseListener(new LabelMouseAdapter()); + add(labelSimpleObject); + + labelModifiedObject = new JLabel("Продвинутый"); + labelModifiedObject.setBounds(370, 240, 100, 50); + labelModifiedObject.setHorizontalAlignment(SwingConstants.CENTER); + labelModifiedObject.setBorder(border); + labelModifiedObject.setTransferHandler(new LabelTransferHandler()); + labelModifiedObject.addMouseListener(new LabelMouseAdapter()); + add(labelModifiedObject); + + labelBodyColor = new JLabel("Цвет"); + labelBodyColor.setBounds(530, 20, 80, 40); + labelBodyColor.setHorizontalAlignment(SwingConstants.CENTER); + labelBodyColor.setBorder(border); + labelBodyColor.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 (_truck == null) + return false; + _truck.entityTruck.setBodyColor(color); + truckPreview.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + add(labelBodyColor); + + labelDumboxColor = new JLabel("Цвет кузова"); + labelDumboxColor.setBounds(620, 20, 80, 40); + labelDumboxColor.setHorizontalAlignment(SwingConstants.CENTER); + labelDumboxColor.setBorder(border); + labelDumboxColor.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 (_truck == null) + return false; + if (!(_truck instanceof DrawingDumpTruck)) + return false; + ((EntityDumpTruck)_truck.entityTruck).setDumpBoxColor(color); + truckPreview.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + add(labelDumboxColor); + + labelTentColor = new JLabel("Цвет тента"); + labelTentColor.setBounds(710, 20, 80, 40); + labelTentColor.setHorizontalAlignment(SwingConstants.CENTER); + labelTentColor.setBorder(border); + labelTentColor.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 (_truck == null) + return false; + if (!(_truck instanceof DrawingDumpTruck)) + return false; + ((EntityDumpTruck)_truck.entityTruck).setTentColor(color); + truckPreview.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + add(labelTentColor); + + labelWheelPreview = new JLabel("Колесо"); + labelWheelPreview.setBounds(530, 70, 80, 30); + labelWheelPreview.setHorizontalAlignment(SwingConstants.CENTER); + labelWheelPreview.setBorder(border); + add(labelWheelPreview); + labelWheelPreview.setTransferHandler( + new TransferHandler(){ + @Override + public boolean canImport(TransferHandler.TransferSupport support) { + return support.isDataFlavorSupported(WheelTransferable.wheelDrawingDataFlavor); + } + + @Override + public boolean importData(TransferHandler.TransferSupport support) { + if (canImport(support)) { + try { + IDrawingWheels iDrawingWheels = (IDrawingWheels) support.getTransferable().getTransferData(WheelTransferable.wheelDrawingDataFlavor); + if (_truck == null) + return false; + _truck.drawingWheels = iDrawingWheels; + _truck.drawingWheels.setWheelNumber((int)numericUpDownWheelAmount.getValue()); + truckPreview.repaint(); + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + + panelRed = new JPanel(); + panelRed.setBounds(250, 50, 40, 40); + panelRed.setBackground(Color.RED); + panelRed.setTransferHandler(new PanelTransferHandler()); + panelRed.addMouseListener(new PanelMouseAdapter()); + add(panelRed); + + panelGreen = new JPanel(); + panelGreen.setBounds(300, 50, 40, 40); + panelGreen.setBackground(Color.GREEN); + panelGreen.setTransferHandler(new PanelTransferHandler()); + panelGreen.addMouseListener(new PanelMouseAdapter()); + add(panelGreen); + + panelBlue = new JPanel(); + panelBlue.setBounds(350, 50, 40, 40); + panelBlue.setBackground(Color.BLUE); + panelBlue.setTransferHandler(new PanelTransferHandler()); + panelBlue.addMouseListener(new PanelMouseAdapter()); + add(panelBlue); + + panelYellow = new JPanel(); + panelYellow.setBounds(400, 50, 40, 40); + panelYellow.setBackground(Color.YELLOW); + panelYellow.setTransferHandler(new PanelTransferHandler()); + panelYellow.addMouseListener(new PanelMouseAdapter()); + add(panelYellow); + + panelWhite = new JPanel(); + panelWhite.setBounds(250, 100, 40, 40); + panelWhite.setBackground(Color.WHITE); + panelWhite.setTransferHandler(new PanelTransferHandler()); + panelWhite.addMouseListener(new PanelMouseAdapter()); + add(panelWhite); + + panelGray = new JPanel(); + panelGray.setBounds(300, 100, 40, 40); + panelGray.setBackground(Color.GRAY); + panelGray.setTransferHandler(new PanelTransferHandler()); + panelGray.addMouseListener(new PanelMouseAdapter()); + add(panelGray); + + panelBlack = new JPanel(); + panelBlack.setBounds(350, 100, 40, 40); + panelBlack.setBackground(Color.black); + panelBlack.setTransferHandler(new PanelTransferHandler()); + panelBlack.addMouseListener(new PanelMouseAdapter()); + add(panelBlack); + + panelMagenta = new JPanel(); + panelMagenta.setBounds(400, 100, 40, 40); + panelMagenta.setBackground(Color.MAGENTA); + panelMagenta.setTransferHandler(new PanelTransferHandler()); + panelMagenta.addMouseListener(new PanelMouseAdapter()); + add(panelMagenta); + + labelWheels = new JLabel("Колеса"); + labelWheels.setBounds(250, 150, 100, 20); + add(labelWheels); + WheelPanel wheelPanelDefault = new WheelPanel(new DrawingWheels()); + wheelPanelDefault.setBounds(250, 180, 40, 40); + add(wheelPanelDefault); + WheelPanel wheelPanelCircles = new WheelPanel(new DrawingWheelsCircles()); + wheelPanelCircles.setBounds(300, 180, 40, 40); + add(wheelPanelCircles); + WheelPanel wheelPanelStar = new WheelPanel(new DrawingWheelsStar()); + wheelPanelStar.setBounds(350, 180, 40, 40); + add(wheelPanelStar); + + truckPreview = new TruckPreview(); + truckPreview.setBounds(530, 110, 260, 140); + truckPreview.setBorder(border); + add(truckPreview); + truckPreview.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 "Простой": + _truck = new DrawingTruck((int)numericUpDownSpeed.getValue(), (int)numericUpDownWeight.getValue(), Color.WHITE, truckPreview.getWidth(), truckPreview.getHeight(), (int)numericUpDownWheelAmount.getValue(), new DrawingWheels()); + break; + case "Продвинутый": + _truck = new DrawingDumpTruck((int)numericUpDownSpeed.getValue(), (int)numericUpDownWeight.getValue(), Color.WHITE, checkBoxDumpBox.isSelected(), checkBoxTent.isSelected(), Color.BLACK, Color.GRAY, truckPreview.getWidth(), truckPreview.getHeight(), (int)numericUpDownWheelAmount.getValue(), new DrawingWheels()); + break; + } + truckPreview.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + + setPreferredSize(new Dimension(800, 300)); + } +}