From 36f0b0ffc6a1fe85d0b99b8552c61017b1717a58 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Thu, 8 Dec 2022 21:17:09 +0400 Subject: [PATCH] lab#5 --- DrawingContainerShip.java | 19 +- DrawingObjectShip.java | 1 + DrawingShip.java | 59 +++--- FormMapWithSetShipsGeneric.java | 40 ++-- FormShip.java | 11 +- FormShipConfig.form | 338 ++++++++++++++++++++++++++++++++ FormShipConfig.java | 246 +++++++++++++++++++++++ IDrawingObject.java | 1 + 8 files changed, 666 insertions(+), 49 deletions(-) create mode 100644 FormShipConfig.form create mode 100644 FormShipConfig.java diff --git a/DrawingContainerShip.java b/DrawingContainerShip.java index ef37b6f..eb214c1 100644 --- a/DrawingContainerShip.java +++ b/DrawingContainerShip.java @@ -2,16 +2,31 @@ import java.awt.*; public class DrawingContainerShip extends DrawingShip { - public DrawingContainerShip(int speed, float weight, Color bodyColor, Color dopColor, boolean crane, boolean containers) + private Color dopColor; + public DrawingContainerShip(int speed, float weight, Color bodyColor,int numdeck,Color dopColor,boolean crane,boolean containers) { - super(speed,weight,bodyColor,130,45); + super(speed,weight,bodyColor,numdeck,130,45); Ship = new EntityContainerShip(speed, weight, bodyColor, dopColor, crane, containers); + this.dopColor=dopColor; } protected DrawingContainerShip(EntityShip ship,IAdditionalDrawingObject deck) { super(ship,deck); Ship=ship; } + public void SetDopColor(Color color) + { + var temp = (EntityContainerShip) Ship; + Ship=new EntityContainerShip(temp.GetSpeed(),temp.GetWeight(),temp.GetBodyColor(),color,temp.GetCrane(),temp.GetContainers()); + dopColor=color; + } + @Override + public void SetColor(Color color) + { + var temp = (EntityContainerShip) Ship; + dopColor = dopColor==null? Color.WHITE : dopColor; + Ship=new EntityContainerShip(temp.GetSpeed(),temp.GetWeight(),color,dopColor,temp.GetCrane(),temp.GetContainers()); + } @Override public void DrawTransport(Graphics g) { if (!(GetShip() instanceof EntityContainerShip containerShip)) diff --git a/DrawingObjectShip.java b/DrawingObjectShip.java index dc419c7..851054b 100644 --- a/DrawingObjectShip.java +++ b/DrawingObjectShip.java @@ -13,6 +13,7 @@ public class DrawingObjectShip implements IDrawingObject { } return 0; } + @Override public DrawingShip GetDrawingObjectShip() { return _ship; } diff --git a/DrawingShip.java b/DrawingShip.java index aa6cfbf..20ef30a 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -1,27 +1,27 @@ import javax.swing.*; import java.awt.*; import java.util.Random; -public class DrawingShip extends JPanel { +import java.util.Set; + +public class DrawingShip extends JComponent{ protected EntityShip Ship; - public IAdditionalDrawingObject Deck; - public void SetEnum() { - Random r = new Random(); - int numbEnum = r.nextInt(1, 4); - Deck.SetAddEnum(numbEnum); - } - public void SetFormEnum() + protected IAdditionalDrawingObject Deck; + public void SetColor(Color color) { - Random r = new Random(); - int numbEnum = r.nextInt(1, 4); - if (numbEnum == 1) { - Deck = new DrawingDeck(); - } - if (numbEnum == 2) { - Deck = new DrawingOvalDeck(); - } - if (numbEnum == 3) { - Deck=new DrawingTrapezDeck(); - } + Ship=new EntityShip(Ship.GetSpeed(),Ship.GetWeight(),color); + } + public IAdditionalDrawingObject GetDeck() + { + return Deck; + } + public void SetFormEnum(int decksnum,IAdditionalDrawingObject deck) + { + Deck=deck; + Deck.SetAddEnum(decksnum); + } + public void SetDeck(IAdditionalDrawingObject deck) + { + Deck=deck; } public EntityShip GetShip() { return Ship; @@ -32,14 +32,25 @@ public class DrawingShip extends JPanel { private Integer _pictureHeight = null; private int _shipWidth = 100; private int _shipHeight = 60; - public DrawingShip(int speed, float weight, Color bodycolor) { + public DrawingShip(int speed, float weight, Color bodycolor,int numdeck) { Ship = new EntityShip(speed, weight, bodycolor); - SetFormEnum(); - SetEnum(); + Deck = new DrawingTrapezDeck(); + Random r = new Random(); + int numbEnum = r.nextInt(1, 4); + if (numbEnum == 1) { + Deck=new DrawingDeck(); + } + if (numbEnum == 2) { + Deck = new DrawingOvalDeck(); + } + if (numbEnum == 3) { + Deck = new DrawingTrapezDeck(); + } + Deck.SetAddEnum(numdeck); } - public DrawingShip(int speed,float weight,Color bodyColor,int shipWidth,int shipHeight) + public DrawingShip(int speed,float weight,Color bodyColor,int numdeck,int shipWidth,int shipHeight) { - this(speed,weight,bodyColor); + this(speed,weight,bodyColor, numdeck); _shipWidth = shipWidth; _shipHeight = shipHeight; } diff --git a/FormMapWithSetShipsGeneric.java b/FormMapWithSetShipsGeneric.java index d04c3fe..da82121 100644 --- a/FormMapWithSetShipsGeneric.java +++ b/FormMapWithSetShipsGeneric.java @@ -24,9 +24,9 @@ public class FormMapWithSetShipsGeneric extends JFrame{ private JButton ButtonRight; private JTextField maskedTextBoxPosition; private JTextField textBoxNewMapName; - private int picWidth=600; + private final int picWidth=600; - private int picHeight=400; + private final int picHeight=400; private JButton ButtonAddMap; private JList ListBoxMaps; private JButton ButtonDeleteMap; @@ -89,24 +89,24 @@ public class FormMapWithSetShipsGeneric extends JFrame{ ButtonAddShip.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (ListBoxMaps.getSelectedIndex() == -1) - { - return; - } - FormShip form = new FormShip(); - form.setSize(1000,700); - form.setVisible(true); - form.setModal(true); - DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip()); - if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship)!=-1) - { - JOptionPane.showMessageDialog(null,"Объект добавлен"); - UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); - } - else - { - JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); - } + FormShipConfig formShipConfig = new FormShipConfig(); + formShipConfig.AddEvent(newShip -> { + if (ListBoxMaps.getSelectedIndex() == -1) { + return; + } + if (newShip != null) { + DrawingObjectShip ship = new DrawingObjectShip(newShip); + if (_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + } else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + + }); + formShipConfig.setSize(850, 300); + formShipConfig.setVisible(true); } }); ListBoxMaps.addListSelectionListener(new ListSelectionListener() { diff --git a/FormShip.java b/FormShip.java index eed562f..f533413 100644 --- a/FormShip.java +++ b/FormShip.java @@ -28,6 +28,11 @@ public class FormShip extends JDialog{ { return SelectedShip; } + public int SetEnum() { + Random r = new Random(); + int numbEnum = r.nextInt(1, 4); + return numbEnum; + } public void Draw(DrawingShip _ship) { pictureBoxShip.removeAll(); BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB); @@ -44,7 +49,7 @@ public class FormShip extends JDialog{ } validate(); } - public FormShip(DrawingObjectShip ship) + public FormShip(IDrawingObject ship) { super(new Frame("Корабль")); CreateWindow(); @@ -82,7 +87,7 @@ public class FormShip extends JDialog{ public void actionPerformed(ActionEvent e){ Random random = new Random(); Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); - _ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst); + _ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,SetEnum()); _ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight()); JLabelSpeed.setText("Cкорость: " + _ship.GetShip().GetSpeed() + " "); JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " "); @@ -141,7 +146,7 @@ public class FormShip extends JDialog{ Random random = new Random(); Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); - _ship=new DrawingContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean()); + _ship=new DrawingContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,SetEnum(),colorSecond,random.nextBoolean(),random.nextBoolean()); _ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight()); JLabelSpeed.setText("Cкорость: " + _ship.GetShip().GetSpeed() + " "); JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " "); diff --git a/FormShipConfig.form b/FormShipConfig.form new file mode 100644 index 0000000..0c2fbf8 --- /dev/null +++ b/FormShipConfig.form @@ -0,0 +1,338 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormShipConfig.java b/FormShipConfig.java new file mode 100644 index 0000000..4b532d7 --- /dev/null +++ b/FormShipConfig.java @@ -0,0 +1,246 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.image.BufferedImage; + +import java.util.function.Consumer; + +public class FormShipConfig extends JFrame { + public JPanel mainPanel; + DrawingShip _ship; + Consumer EventAddShip; + private JPanel groupBoxConfig; + private JLabel labelSpeed; + private JSpinner numericUpDownSpeed; + private JLabel labelWeight; + private JSpinner numericUpDownWeight; + private JCheckBox checkBoxContainers; + private JCheckBox checkBoxCrane; + private JPanel groupBoxColors; + private JPanel PanelRed; + private JPanel PanelBlue; + private JPanel PanelWhite; + private JPanel PanelYellow; + private JPanel PanelBlack; + private JPanel PanelGreen; + private JPanel PanelPurple; + private JPanel PanelCyan; + private JLabel LabelSimple; + private JLabel LabelModif; + private JLabel LabelBaseColor; + private JLabel LabelDopColor; + private JPanel pictureBox; + private JButton buttonAdd; + private JButton buttonCancel; + private JSpinner numericUpDownDecks; + private JLabel labelDecks; + private JLabel labelOvalDeck; + private JLabel labelTrapezDeck; + private JLabel labelSimpleDeck; + private JLabel LabelDecksForm; + private JPanel groupBoxPicture; + Object ObjFDnD = null; + Object TargetObjFDnD = null; + public FormShipConfig() + { + super("Корабль"); + CreateWindow(); + + } + public void Draw(DrawingShip _ship) + { + pictureBox.removeAll(); + BufferedImage bmp = new BufferedImage(pictureBox.getWidth(), pictureBox.getHeight(),BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight()); + if (_ship != null) { + _ship.DrawTransport(gr); + JLabel imageOfLoco = new JLabel(); + imageOfLoco.setPreferredSize(pictureBox.getSize()); + imageOfLoco.setMinimumSize(new Dimension(1, 1)); + imageOfLoco.setIcon(new ImageIcon(bmp)); + pictureBox.add(imageOfLoco,BorderLayout.CENTER); + } + revalidate(); + } + public void AddEvent(Consumer ev){EventAddShip = ev;} + public void CreateWindow() { + setPreferredSize(new Dimension(720, 200)); + getContentPane().add(mainPanel); + PanelBlack.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + PanelBlue.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + PanelRed.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + PanelYellow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + PanelCyan.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + PanelPurple.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + PanelWhite.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + PanelGreen.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + LabelSimple.setText("Простой"); + LabelModif.setText("Модифицированный"); + LabelSimple.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + LabelModif.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + LabelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + LabelDopColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + labelSimpleDeck.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + labelTrapezDeck.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + labelOvalDeck.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + LabelDecksForm.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + numericUpDownSpeed.setModel(new SpinnerNumberModel(100, 100, 1000, 1)); + numericUpDownWeight.setModel(new SpinnerNumberModel(100, 100, 1000, 1)); + numericUpDownDecks.setModel(new SpinnerNumberModel(1, 1, 3, 1)); + MouseAdapter drag = new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + setCursor(new Cursor(Cursor.HAND_CURSOR)); + } + + @Override + public void mouseReleased(MouseEvent e) { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + Drop((JComponent) e.getSource()); + } + }; + MouseAdapter defCursor = new MouseAdapter() { + @Override + public void mouseExited(MouseEvent e) { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + } + }; + AdditDeckDropObject(labelOvalDeck); + AdditDeckDropObject(labelSimpleDeck); + AdditDeckDropObject(labelTrapezDeck); + AdditDeckDropTarget(LabelDecksForm); + pictureBox.addMouseListener(defCursor); + LabelBaseColor.addMouseListener(defCursor); + LabelDopColor.addMouseListener(defCursor); + PanelRed.addMouseListener(drag); + PanelBlack.addMouseListener(drag); + PanelBlue.addMouseListener(drag); + PanelYellow.addMouseListener(drag); + PanelCyan.addMouseListener(drag); + PanelWhite.addMouseListener(drag); + PanelPurple.addMouseListener(drag); + PanelGreen.addMouseListener(drag); + LabelSimple.addMouseListener(drag); + LabelModif.addMouseListener(drag); + buttonAdd.addActionListener(e -> { + EventAddShip.accept(_ship); + dispose(); + }); + buttonCancel.addActionListener(e -> dispose()); + } + void AdditDeckDropTarget(JComponent obj){ + obj.addMouseListener(new MouseAdapter() { + @Override + public void mouseEntered(MouseEvent e) {super.mouseEntered(e); + Deck_DragEnter(obj); + } + @Override + public void mouseExited(MouseEvent e) {super.mouseExited(e); + DragExit(); + } + }); + } + void AdditDeckDropObject(JComponent obj){ + obj.addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) {super.mousePressed(e); + Deck_MouseDown(obj); + } + @Override + public void mouseReleased(MouseEvent e) {super.mouseReleased(e); + Deck_DragDrop(); + } + }); + } + public void Drop(JComponent dropditem) + { + if(dropditem==null) + { + return; + } + if(dropditem instanceof JPanel panel) + { + if(_ship == null) + { + return; + } + if(LabelBaseColor.getMousePosition()!=null) + { + _ship.SetColor(panel.getBackground()); + Draw(_ship); + revalidate(); + } + if(LabelDopColor.getMousePosition()!=null && _ship instanceof DrawingContainerShip containerShip) + { + containerShip.SetDopColor(panel.getBackground()); + Draw(_ship); + revalidate(); + } + } + if(dropditem instanceof JLabel jLabel && pictureBox.getMousePosition()!=null) + { + int speed = (int)numericUpDownSpeed.getValue(); + int weight=(int)numericUpDownWeight.getValue(); + int NumDecks=(int)numericUpDownDecks.getValue(); + boolean crane= checkBoxCrane.isSelected(); + boolean containers= checkBoxContainers.isSelected(); + if(jLabel == LabelSimple) + { + _ship=new DrawingShip(speed,weight,Color.WHITE,NumDecks); + Draw(_ship); + revalidate(); + } + else if(jLabel==LabelModif) + { + _ship=new DrawingContainerShip(speed,weight,Color.WHITE,NumDecks,Color.WHITE,crane,containers); + Draw(_ship); + revalidate(); + } + if(_ship != null) + { + _ship.SetPosition(20,50,pictureBox.getWidth(),pictureBox.getHeight()); + Draw(_ship); + revalidate(); + } + } + } + void Deck_MouseDown(Object sender) { + IAdditionalDrawingObject Deck; + switch (((JLabel)sender).getText()){ + case "Овальные": + Deck = new DrawingOvalDeck(); + break; + case "Трапецевидные": + Deck = new DrawingTrapezDeck(); + break; + case "Прямоугольные": + Deck = new DrawingDeck(); + break; + default: + return; + } + Deck.SetAddEnum((int)numericUpDownDecks.getValue()); + ObjFDnD = Deck; + } + void Deck_DragEnter(Object sender){ + if(ObjFDnD!=null&& IAdditionalDrawingObject.class.isAssignableFrom(ObjFDnD.getClass())){ + setCursor(new Cursor(Cursor.HAND_CURSOR)); + TargetObjFDnD = sender; + } + } + void Deck_DragDrop(){ + if(TargetObjFDnD==null) {return;} + _ship.SetFormEnum((int)numericUpDownDecks.getValue(),(IAdditionalDrawingObject)ObjFDnD); + DragExit(); + ObjFDnD = null; + } + void DragExit(){ + if(ObjFDnD!=null&&TargetObjFDnD!=null) { + setCursor(Cursor.getDefaultCursor()); + Draw(_ship); + TargetObjFDnD = null; + } + } +} diff --git a/IDrawingObject.java b/IDrawingObject.java index d59fce2..331aa5b 100644 --- a/IDrawingObject.java +++ b/IDrawingObject.java @@ -6,4 +6,5 @@ public interface IDrawingObject { void MoveObject(Direction direction); void DrawingObject(Graphics g); float[] GetCurrentPosition(); + public DrawingShip GetDrawingObjectShip(); }