import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.util.Random; public class FormShipMixer extends JFrame{ public JPanel Mainpanel; private JPanel pictureBox; private JButton ButtonCreate; private JButton ButtonCreateModif; private JToolBar StatusStrip; private JLabel LabelInfo; private JButton ButtonMix; private JLabel JLabelSpeed = new JLabel(); private JLabel JLabelWeight = new JLabel(); private JLabel JLabelColor = new JLabel(); private ShipsMix _drawningEntities; private IDrawningDeck SetData() { Random random = new Random(); switch (random.nextInt(3)) { case 0: return new DrawDeck(); case 1: return new DrawGuns(); } return new DrawOvalDeck(); } private void Draw(DrawingShip _plane) { pictureBox.removeAll(); Random random = new Random(); 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 (_plane != null) { _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); _plane.DrawTransport(gr); JLabelSpeed.setText("Cкорость: " + _plane.GetWarmlyShip().GetSpeed() + " "); JLabelWeight.setText("Вес: " + _plane.GetWarmlyShip().GetWeight() + " "); JLabelColor.setText(("Цвет: " + _plane.GetWarmlyShip().GetBodyColor() + " ")); JLabel imageOfShip = new JLabel(); imageOfShip.setPreferredSize(pictureBox.getSize()); imageOfShip.setMinimumSize(new Dimension(1, 1)); imageOfShip.setIcon(new ImageIcon(bmp)); pictureBox.add(imageOfShip,BorderLayout.CENTER); } validate(); } public FormShipMixer() { Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); LabelBox.add(JLabelSpeed); LabelBox.add(JLabelWeight); LabelBox.add(JLabelColor); StatusStrip.add(LabelBox); _drawningEntities = new ShipsMix<>(10,10); ButtonCreate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){ Random random = new Random(); Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); EntityWarmlyShip ship = new EntityWarmlyShip(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst); IDrawningDeck deck = SetData(); int DecksCount=random.nextInt(1,4); deck.SetDeckCount(DecksCount); if(_drawningEntities.Insert(ship) != -1 && _drawningEntities.Insert(deck) != -1) { JOptionPane.showMessageDialog(null,"Объект добавлен"); Draw(_drawningEntities.MakeShipMix()); LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } } }); ButtonCreateModif.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Random random = new Random(); Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); EntityMotorShip _ship = new EntityMotorShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean()); IDrawningDeck deck = SetData(); int DecksCount=random.nextInt(1,4); deck.SetDeckCount(DecksCount); if(_drawningEntities.Insert(_ship) != -1 && _drawningEntities.Insert(deck) != -1) { JOptionPane.showMessageDialog(null,"Объект добавлен"); Draw(_drawningEntities.MakeShipMix()); LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } } }); ButtonMix.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(_drawningEntities.shipsCount == 0 || _drawningEntities.deckCount == 0) return; Draw(_drawningEntities.MakeShipMix()); LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY); } }); } }