Pibd-22_Presnyakova.V.V_Cat.../FormBoat.java

79 lines
3.2 KiB
Java
Raw Normal View History

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;
import java.util.Random;
public class FormBoat {
public JPanel Mainpanel;
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
protected DrawningBoat PictureBoxBoat;
private JToolBar StatusStrip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
public void Draw() {
if (PictureBoxBoat.GetBoat() == null) {
return;
}
PictureBoxBoat.DrawTransport();
}
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();
PictureBoxBoat.Init(random.nextInt(10, 30), random.nextInt(10, 20), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
PictureBoxBoat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxBoat.getWidth(), PictureBoxBoat.getHeight());
JLabelSpeed.setText("орость: " + PictureBoxBoat.GetBoat().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + PictureBoxBoat.GetBoat().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + PictureBoxBoat.GetBoat().GetBodyColor() + " "));
Draw();
});
PictureBoxBoat.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
PictureBoxBoat.ChangeBorders(PictureBoxBoat.getWidth(), PictureBoxBoat.getHeight());
Draw();
}
});
ButtonUp.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Up);
Draw();
});
ButtonDown.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Down);
Draw();
});
ButtonRight.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Right);
Draw();
});
ButtonLeft.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Left);
Draw();
});
}
}