Pibd-23_Zhelovanov_D.Y._Bat.../FormMap.java

135 lines
5.5 KiB
Java
Raw Permalink Normal View History

2022-12-06 17:23:28 +04:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormMap extends JComponent {
private AbstractMap _abstractMap;
private DrawningBattleship _battleship;
private BufferedImage bufferedImage;
2022-12-20 21:50:09 +04:00
2022-12-06 17:23:28 +04:00
public FormMap() {
JFrame form = new JFrame("Карта");
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setSize(1000, 500);
form.setVisible(true);
form.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new FlowLayout());
setLayout(new BorderLayout());
form.add(statusPanel, BorderLayout.SOUTH);
Label speedLabel = new Label("Скорость: ");
Label weightLabel = new Label("Вес: ");
Label colorLabel = new Label("Цвет: ");
String[] maps = {
2022-12-16 00:04:08 +04:00
"Простая карта",
"Морская карта"
2022-12-06 17:23:28 +04:00
};
JComboBox mapSelectComboBox = new JComboBox(maps);
mapSelectComboBox.setEditable(true);
mapSelectComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String item = (String)mapSelectComboBox.getSelectedItem();
if (item == null) return;
switch (item) {
2022-12-16 00:04:08 +04:00
case ("Простая карта"):
2022-12-06 17:23:28 +04:00
_abstractMap = new SimpleMap();
break;
2022-12-16 00:04:08 +04:00
case ("Морская карта"):
2022-12-06 17:23:28 +04:00
_abstractMap = new SeaMap();
break;
}
}
});
JButton createButton = new JButton("Создать");
createButton.addActionListener(e -> {
Random rnd = new Random();
var battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
rnd.nextInt(0, 256)));
speedLabel.setText("Скорость: " + battleship.Battleship.GetSpeed());
weightLabel.setText("Вес: " + (int)battleship.Battleship.GetWeight());
colorLabel.setText("Цвет: " + battleship.Battleship.GetBodyColor().getRed() + " " +
battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() );
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship));
repaint();
});
JButton modifiedButton = new JButton("Модификация");
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
Color colorSecond = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
var battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst,
colorSecond,
rnd.nextBoolean(),
rnd.nextBoolean());
battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
2022-12-16 00:04:08 +04:00
speedLabel.setText("Скорость: " + battleship.Battleship.GetSpeed());
weightLabel.setText("Вес: " + battleship.Battleship.GetWeight());
colorLabel.setText("Цвет: " + battleship.Battleship.GetBodyColor().getRed() + " " + battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() );
2022-12-06 17:23:28 +04:00
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship));
repaint();
});
statusPanel.add(mapSelectComboBox);
statusPanel.add(createButton);
statusPanel.add(modifiedButton);
statusPanel.add(speedLabel);
statusPanel.add(weightLabel);
statusPanel.add(colorLabel);
JButton upButton = new JButton("");
JButton rightButton = new JButton("");
JButton leftButton = new JButton("");
JButton downButton = new JButton("");
upButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Up);
repaint();
});
rightButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Right);
repaint();
});
leftButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Left);
repaint();
});
downButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Down);
repaint();
});
statusPanel.add(leftButton);
statusPanel.add(upButton);
statusPanel.add(rightButton);
statusPanel.add(downButton);
form.getContentPane().add(this);
super.repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,1000,490,null);
super.repaint();
}
}