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

110 lines
4.0 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.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Random;
public class FormMap extends JFrame {
private JPanel shipPane;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JPanel pictureBox;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JButton createButton;
private JButton createAdvancedButton;
private JComboBox comboBoxSelectorMap;
private AbstractMap _abstractMap;
private Image bufferedImage;
public FormMap() {
this.setTitle("Artillery");
this.setContentPane(shipPane);
_abstractMap = new SimpleMap();
createButton.addActionListener(e -> {
Random rnd = new Random();
var ship = new DrawingShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextInt(4, 6 + 1)
);
setData(ship);
});
buttonLeft.setForeground(new Color(0, 0, 0, 0));
buttonRight.setForeground(new Color(0, 0, 0, 0));
buttonUp.setForeground(new Color(0, 0, 0, 0));
buttonDown.setForeground(new Color(0, 0, 0, 0));
buttonLeft.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Down);
repaint();
}
});
createAdvancedButton.addActionListener(e -> {
Random rnd = new Random();
var ship = new DrawingMotorShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextInt(4, 6 + 1),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextBoolean(),
rnd.nextBoolean()
);
setData(ship);
});
comboBoxSelectorMap.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
switch (e.getItem().toString()) {
case "Простая карта" -> _abstractMap = new SimpleMap();
case "Лесная карта" -> _abstractMap = new WaterMap();
}
}
});
}
private void setData(DrawingShip ship) {
Random rnd = new Random();
ship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Скорость: %d", ship.ship.getSpeed()));
weightLabel.setText(String.format("Вес: %f", ship.ship.getWeight()));
colorLabel.setText(String.format("Цвет: %x", ship.getShip().getBodyColor().getRGB()));
bufferedImage = _abstractMap.createMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawingObjectShip(ship));
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
pictureBox.paintComponents(bufferedImage.getGraphics());
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
}