2022-12-05 00:55:23 +04:00
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ComponentAdapter;
|
|
|
|
|
import java.awt.event.ComponentEvent;
|
2022-12-05 02:29:28 +04:00
|
|
|
|
import java.awt.image.BufferedImage;
|
2022-12-05 00:55:23 +04:00
|
|
|
|
import java.util.Random;
|
2022-12-05 02:29:28 +04:00
|
|
|
|
|
|
|
|
|
public class FormBoat extends JFrame{
|
2022-12-05 00:55:23 +04:00
|
|
|
|
public JPanel Mainpanel;
|
|
|
|
|
private JButton ButtonCreate;
|
|
|
|
|
private JButton ButtonLeft;
|
|
|
|
|
private JButton ButtonUp;
|
|
|
|
|
private JButton ButtonDown;
|
|
|
|
|
private JButton ButtonRight;
|
2022-12-05 02:29:28 +04:00
|
|
|
|
protected DrawningBoat _boat;
|
|
|
|
|
private JPanel PictureBox;
|
2022-12-05 00:55:23 +04:00
|
|
|
|
private JToolBar StatusStrip;
|
2022-12-05 02:29:28 +04:00
|
|
|
|
private JButton ButtonModif;
|
|
|
|
|
private final JLabel JLabelSpeed = new JLabel();
|
|
|
|
|
private final JLabel JLabelWeight = new JLabel();
|
|
|
|
|
private final JLabel JLabelColor = new JLabel();
|
2022-12-05 00:55:23 +04:00
|
|
|
|
public void Draw() {
|
2022-12-05 02:29:28 +04:00
|
|
|
|
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 (_boat != null) {
|
|
|
|
|
_boat.DrawTransport(gr);
|
|
|
|
|
JLabel imageOfPlane = new JLabel();
|
|
|
|
|
imageOfPlane.setPreferredSize(PictureBox.getSize());
|
|
|
|
|
imageOfPlane.setMinimumSize(new Dimension(1, 1));
|
|
|
|
|
imageOfPlane.setIcon(new ImageIcon(bmp));
|
|
|
|
|
PictureBox.add(imageOfPlane,BorderLayout.CENTER);
|
2022-12-05 00:55:23 +04:00
|
|
|
|
}
|
2022-12-05 02:29:28 +04:00
|
|
|
|
validate();
|
|
|
|
|
}
|
|
|
|
|
private void SetData(){
|
|
|
|
|
Random random = new Random();
|
2022-12-05 11:12:21 +04:00
|
|
|
|
_boat.SetPosition(random.nextInt(10, 100), random.nextInt(30, 100), PictureBox.getWidth(), PictureBox.getHeight());
|
2022-12-05 02:29:28 +04:00
|
|
|
|
JLabelSpeed.setText("Cкорость: " + _boat.GetBoat().GetSpeed() + " ");
|
|
|
|
|
JLabelWeight.setText("Вес: " + _boat.GetBoat().GetWeight() + " ");
|
|
|
|
|
JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " "));
|
2022-12-05 00:55:23 +04:00
|
|
|
|
}
|
|
|
|
|
public FormBoat() {
|
|
|
|
|
Box LabelBox = Box.createHorizontalBox();
|
|
|
|
|
LabelBox.setMinimumSize(new Dimension(1, 20));
|
|
|
|
|
LabelBox.add(JLabelSpeed);
|
|
|
|
|
LabelBox.add(JLabelWeight);
|
|
|
|
|
LabelBox.add(JLabelColor);
|
|
|
|
|
StatusStrip.add(LabelBox);
|
|
|
|
|
try {
|
|
|
|
|
Image img = ImageIO.read(FormBoat.class.getResource("/Resource/up.jpg"));
|
|
|
|
|
ButtonUp.setIcon(new ImageIcon(img));
|
|
|
|
|
img = ImageIO.read(FormBoat.class.getResource("/Resource/down.jpg"));
|
|
|
|
|
ButtonDown.setIcon(new ImageIcon(img));
|
|
|
|
|
img = ImageIO.read(FormBoat.class.getResource("/Resource/left.jpg"));
|
|
|
|
|
ButtonLeft.setIcon(new ImageIcon(img));
|
|
|
|
|
img = ImageIO.read(FormBoat.class.getResource("/Resource/right.jpg"));
|
|
|
|
|
ButtonRight.setIcon(new ImageIcon(img));
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
System.out.println(ex);
|
|
|
|
|
}
|
|
|
|
|
ButtonCreate.addActionListener(e -> {
|
|
|
|
|
Random random = new Random();
|
2022-12-05 02:29:28 +04:00
|
|
|
|
_boat = new DrawningBoat(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
|
|
|
|
|
SetData();
|
|
|
|
|
Draw();
|
|
|
|
|
});
|
|
|
|
|
ButtonModif.addActionListener(e -> {
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
_boat = new DrawningCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean());
|
|
|
|
|
SetData();
|
2022-12-05 00:55:23 +04:00
|
|
|
|
Draw();
|
|
|
|
|
});
|
2022-12-05 02:29:28 +04:00
|
|
|
|
PictureBox.addComponentListener(new ComponentAdapter() {
|
2022-12-05 00:55:23 +04:00
|
|
|
|
@Override
|
|
|
|
|
public void componentResized(ComponentEvent e) {
|
2022-12-05 02:29:28 +04:00
|
|
|
|
if(_boat == null || _boat.GetBoat() == null) return;
|
2022-12-05 00:55:23 +04:00
|
|
|
|
super.componentResized(e);
|
2022-12-05 02:29:28 +04:00
|
|
|
|
_boat.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight());
|
|
|
|
|
PictureBox.revalidate();
|
2022-12-05 00:55:23 +04:00
|
|
|
|
Draw();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
ButtonUp.addActionListener(e -> {
|
2022-12-05 02:29:28 +04:00
|
|
|
|
_boat.MoveTransport(Direction.Up);
|
|
|
|
|
PictureBox.revalidate();
|
2022-12-05 00:55:23 +04:00
|
|
|
|
Draw();
|
|
|
|
|
});
|
|
|
|
|
ButtonDown.addActionListener(e -> {
|
2022-12-05 02:29:28 +04:00
|
|
|
|
_boat.MoveTransport(Direction.Down);
|
|
|
|
|
PictureBox.revalidate();
|
2022-12-05 00:55:23 +04:00
|
|
|
|
Draw();
|
|
|
|
|
});
|
|
|
|
|
ButtonRight.addActionListener(e -> {
|
2022-12-05 02:29:28 +04:00
|
|
|
|
_boat.MoveTransport(Direction.Right);
|
|
|
|
|
PictureBox.revalidate();
|
2022-12-05 00:55:23 +04:00
|
|
|
|
Draw();
|
|
|
|
|
});
|
|
|
|
|
ButtonLeft.addActionListener(e -> {
|
2022-12-05 02:29:28 +04:00
|
|
|
|
_boat.MoveTransport(Direction.Left);
|
|
|
|
|
PictureBox.revalidate();
|
2022-12-05 00:55:23 +04:00
|
|
|
|
Draw();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|