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 FormParam extends JFrame{ public JPanel MainPanel; private JPanel pictureBoxBoat; private JButton ButtonCreate; private JButton ButtonCreateModif; private JToolBar StatusStrip; private JLabel LabelInfo; private JLabel JLabelSpeed = new JLabel(); private JLabel JLabelWeight = new JLabel(); private JLabel JLabelColor = new JLabel(); private DrawningEntities _drawningEntities; private IDrawningOars SetData() { Random random=new Random(); int r = random.nextInt(3); if(r==0) { return new DrawningOars(); } if(r==1) { return new DrawningSqareOars(); } else { return new DrawningOvalOars(); } } private void Draw(DrawningBoat _boat) { pictureBoxBoat.removeAll(); Random random = new Random(); BufferedImage bmp = new BufferedImage(pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight(),BufferedImage.TYPE_INT_RGB); Graphics gr = bmp.getGraphics(); gr.setColor(new Color(238, 238, 238)); gr.fillRect(0, 0, pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight()); if (_boat != null) { _boat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight()); _boat.DrawTransport(gr); JLabelSpeed.setText("Cкорость: " + _boat.GetBoat().GetSpeed() + " "); JLabelWeight.setText("Вес: " + _boat.GetBoat().GetWeight() + " "); JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " ")); JLabel imageOfBoat = new JLabel(); imageOfBoat.setPreferredSize(pictureBoxBoat.getSize()); imageOfBoat.setMinimumSize(new Dimension(1, 1)); imageOfBoat.setIcon(new ImageIcon(bmp)); pictureBoxBoat.add(imageOfBoat,BorderLayout.CENTER); } validate(); } public FormParam() { Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); LabelBox.add(JLabelSpeed); LabelBox.add(JLabelWeight); LabelBox.add(JLabelColor); StatusStrip.add(LabelBox); _drawningEntities = new DrawningEntities<>(10,10); ButtonCreate.addActionListener(e -> { Random random = new Random(); Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); EntityBoat _boat = new EntityBoat(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst); IDrawningOars oar = SetData(); int OarCount = random.nextInt(1,4); oar.SetOarsCount(OarCount); if((_drawningEntities.Insert(_boat)!=-1) & (_drawningEntities.Insert(oar)!=-1)) { JOptionPane.showMessageDialog(null,"Объект добавлен"); Draw(_drawningEntities.CreateBoat()); LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } }); ButtonCreateModif.addActionListener(e -> { Random random = new Random(); Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); EntityCatamaran _boat = new EntityCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean()); IDrawningOars oar = SetData(); int OarCount = random.nextInt(1,4); oar.SetOarsCount(OarCount); if((_drawningEntities.Insert(_boat)!=-1) & (_drawningEntities.Insert(oar)!=-1)) { JOptionPane.showMessageDialog(null,"Объект добавлен"); Draw(_drawningEntities.CreateBoat()); LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } }); } }