PIbd-21_Markov_D.P._Contain.../FormShip.java
2022-12-01 23:31:53 +04:00

97 lines
3.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormShip {
public JPanel Mainpanel;
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
protected DrawingShip PictureBoxShip;
private JToolBar StatusStrip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
public void Draw() {
if (PictureBoxShip.GetShip() == null) {
return;
}
PictureBoxShip.DrawTransport();
}
public FormShip() {
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(FormShip.class.getResource("Images/ArrowUp.png"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/ArrowDown.png"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/ArrowLeft.png"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/ArrowRight.png"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
PictureBoxShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
PictureBoxShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxShip.getWidth(), PictureBoxShip.getHeight());
JLabelSpeed.setText("орость: " + PictureBoxShip.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + PictureBoxShip.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + PictureBoxShip.GetShip().GetBodyColor() + " "));
Draw();
}
});
PictureBoxShip.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
PictureBoxShip.ChangeBorders(PictureBoxShip.getWidth(), PictureBoxShip.getHeight());
Draw();
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Up);
Draw();
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Down);
Draw();
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Right);
Draw();
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Left);
Draw();
}
});
}
}