PIbd-23_Bogdanov_D.S._Ship..../FormShip.java
2022-11-22 10:13:07 +04:00

99 lines
3.7 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.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormShip extends JFrame {
private JPanel shipPane;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JPanel pictureBox;
private JButton createButton;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JButton createAdvancedButton;
private DrawingShip _ship;
public FormShip() {
this.setTitle("Ship");
this.setContentPane(shipPane);
createButton.addActionListener(e -> {
Random rnd = new Random();
_ship = new DrawingShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(
rnd.nextInt(0, 256),
rnd.nextInt(0, 256),
rnd.nextInt(0, 256)),
rnd.nextInt(4, 7)
);
_ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Speed: %s", _ship.getShip().getSpeed()));
weightLabel.setText(String.format("Weight: %s", _ship.getShip().getWeight()));
colorLabel.setText(String.format("Color: %x", _ship.getShip().getBodyColor().getRGB()));
repaint();
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (_ship != null) _ship.changeBorders(pictureBox.getWidth(), pictureBox.getHeight());
repaint();
}
});
buttonLeft.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Left);
repaint();
});
buttonRight.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Right);
repaint();
});
buttonUp.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Up);
repaint();
});
buttonDown.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Down);
repaint();
});
createAdvancedButton.addActionListener(e -> {
Random rnd = new Random();
_ship = new DrawingMotorShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(
rnd.nextInt(0, 256),
rnd.nextInt(0, 256),
rnd.nextInt(0, 256)),
rnd.nextInt(4, 7),
new Color(
rnd.nextInt(0, 256),
rnd.nextInt(0, 256),
rnd.nextInt(0, 256)),
rnd.nextBoolean(),
rnd.nextBoolean()
);
_ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Скорость: %s", _ship.getShip().getSpeed()));
weightLabel.setText(String.format("Вес: %s", _ship.getShip().getWeight()));
colorLabel.setText(String.format("Цвет: %x", _ship.getShip().getBodyColor().getRGB()));
repaint();
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) pictureBox.getGraphics();
if (_ship != null) {
_ship.drawTransport(g2d);
}
}
}