2023-12-02 19:56:24 +04:00
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
public class FrameAirBomber extends JFrame {
|
2023-12-30 09:28:39 +04:00
|
|
|
|
private DrawingAir drawingPlane;
|
2023-12-02 20:06:46 +04:00
|
|
|
|
private AbstractStrategy abstractStrategy;
|
2023-12-30 09:28:39 +04:00
|
|
|
|
public JButton selectPlaneButton;
|
|
|
|
|
|
|
|
|
|
private DrawingAir selectedPlane;
|
|
|
|
|
public DrawingAir getSelectedPlane() {
|
|
|
|
|
return selectedPlane;
|
|
|
|
|
}
|
2023-12-02 20:06:46 +04:00
|
|
|
|
private JComboBox comboBoxStrategy;
|
2023-12-02 19:56:24 +04:00
|
|
|
|
private final JComponent pictureBox;
|
|
|
|
|
public FrameAirBomber() throws IOException {
|
|
|
|
|
super("Бомбардировщик");
|
|
|
|
|
setSize(new Dimension(900,500));
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
2023-12-02 20:06:46 +04:00
|
|
|
|
//components initialisation
|
2023-12-02 19:56:24 +04:00
|
|
|
|
pictureBox = new JComponent(){
|
|
|
|
|
public void paintComponent(Graphics graphics){
|
|
|
|
|
super.paintComponent(graphics);
|
|
|
|
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
2023-12-30 09:28:39 +04:00
|
|
|
|
if (drawingPlane != null) drawingPlane.drawTransport(graphics2D);
|
2023-12-02 19:56:24 +04:00
|
|
|
|
super.repaint();
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-12-02 20:06:46 +04:00
|
|
|
|
comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"});
|
|
|
|
|
JButton stepButton = new JButton("Шаг");
|
|
|
|
|
JButton createPlaneButton = new JButton("Создать самолет");
|
|
|
|
|
JButton createAirBomberButton = new JButton("Создать бомбардировщик");
|
2023-12-02 19:56:24 +04:00
|
|
|
|
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
|
|
|
|
|
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
|
|
|
|
|
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
|
|
|
|
|
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
|
2023-12-30 09:28:39 +04:00
|
|
|
|
selectPlaneButton = new JButton("Выбрать самолет");
|
2023-12-02 19:56:24 +04:00
|
|
|
|
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
2023-12-02 20:06:46 +04:00
|
|
|
|
//ActionListeners and ActionCommand addition
|
|
|
|
|
createPlaneButton.addActionListener(e -> buttonCreatePlaneClick());
|
|
|
|
|
createAirBomberButton.addActionListener(e -> buttonCreateAirBomberClick());
|
|
|
|
|
stepButton.addActionListener(e -> buttonStepClick());
|
2023-12-02 19:56:24 +04:00
|
|
|
|
rightButton.setActionCommand("right");
|
|
|
|
|
rightButton.addActionListener(this::buttonMoveClick);
|
|
|
|
|
leftButton.setActionCommand("left");
|
|
|
|
|
leftButton.addActionListener(this::buttonMoveClick);
|
|
|
|
|
upButton.setActionCommand("up");
|
|
|
|
|
upButton.addActionListener(this::buttonMoveClick);
|
|
|
|
|
downButton.setActionCommand("down");
|
|
|
|
|
downButton.addActionListener(this::buttonMoveClick);
|
|
|
|
|
//component addition
|
|
|
|
|
setLayout(new BorderLayout());
|
2023-12-02 20:06:46 +04:00
|
|
|
|
JPanel panelAirBomber = new JPanel(new BorderLayout());
|
2023-12-02 19:56:24 +04:00
|
|
|
|
JPanel rightPanel = new JPanel(new BorderLayout());
|
2023-12-02 20:06:46 +04:00
|
|
|
|
JPanel leftPanel = new JPanel(new BorderLayout());
|
2023-12-02 19:56:24 +04:00
|
|
|
|
GridBagConstraints constraints = new GridBagConstraints();
|
2023-12-02 20:06:46 +04:00
|
|
|
|
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
|
|
|
|
//createPanel
|
|
|
|
|
JPanel createPanel = new JPanel(new GridBagLayout());
|
|
|
|
|
constraints.gridx = 0;
|
|
|
|
|
constraints.gridy = 0;
|
|
|
|
|
createPanel.add(createPlaneButton, constraints);
|
|
|
|
|
constraints.gridx = 1;
|
|
|
|
|
constraints.gridy = 0;
|
|
|
|
|
createPanel.add(createAirBomberButton, constraints);
|
2023-12-30 09:28:39 +04:00
|
|
|
|
constraints.gridx = 2;
|
|
|
|
|
constraints.gridy = 0;
|
|
|
|
|
createPanel.add(selectPlaneButton, constraints);
|
2023-12-02 20:06:46 +04:00
|
|
|
|
//movementPanel
|
|
|
|
|
JPanel movementPanel = new JPanel(new GridBagLayout());
|
|
|
|
|
rightButton.setPreferredSize(new Dimension(30,30));
|
2023-12-02 19:56:24 +04:00
|
|
|
|
constraints.gridx = 2;
|
|
|
|
|
constraints.gridy = 1;
|
|
|
|
|
movementPanel.add(rightButton, constraints);
|
|
|
|
|
leftButton.setPreferredSize(new Dimension(30,30));
|
|
|
|
|
constraints.gridx = 0;
|
|
|
|
|
constraints.gridy = 1;
|
|
|
|
|
movementPanel.add(leftButton, constraints);
|
|
|
|
|
upButton.setPreferredSize(new Dimension(30,30));
|
|
|
|
|
constraints.gridx = 1;
|
|
|
|
|
constraints.gridy = 0;
|
|
|
|
|
movementPanel.add(upButton, constraints);
|
|
|
|
|
downButton.setPreferredSize(new Dimension(30,30));
|
|
|
|
|
constraints.gridx = 1;
|
|
|
|
|
constraints.gridy = 1;
|
|
|
|
|
movementPanel.add(downButton, constraints);
|
2023-12-02 20:06:46 +04:00
|
|
|
|
//stepPanel
|
|
|
|
|
JPanel stepPanel = new JPanel(new GridBagLayout());
|
|
|
|
|
constraints.gridx = 0;
|
|
|
|
|
constraints.gridy = 0;
|
|
|
|
|
stepPanel.add(comboBoxStrategy, constraints);
|
|
|
|
|
constraints.gridx = 0;
|
|
|
|
|
constraints.gridy = 1;
|
|
|
|
|
stepPanel.add(stepButton, constraints);
|
|
|
|
|
//addition to frame
|
2023-12-02 19:56:24 +04:00
|
|
|
|
add(pictureBox);
|
2023-12-02 20:06:46 +04:00
|
|
|
|
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
|
|
|
|
rightPanel.add(stepPanel, BorderLayout.NORTH);
|
|
|
|
|
leftPanel.add(createPanel, BorderLayout.SOUTH);
|
|
|
|
|
panelAirBomber.add(rightPanel, BorderLayout.EAST);
|
|
|
|
|
panelAirBomber.add(leftPanel, BorderLayout.WEST);
|
|
|
|
|
add(panelAirBomber,BorderLayout.CENTER);
|
2023-12-02 19:56:24 +04:00
|
|
|
|
setVisible(true);
|
|
|
|
|
}
|
2023-12-02 20:06:46 +04:00
|
|
|
|
private void buttonCreateAirBomberClick() {
|
2023-12-02 19:56:24 +04:00
|
|
|
|
Random random = new Random();
|
|
|
|
|
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
2023-12-30 09:28:39 +04:00
|
|
|
|
Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
|
|
|
|
Color additionalColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
|
|
|
|
drawingPlane = new DrawingAirBomber(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor,
|
|
|
|
|
additionalColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
|
|
|
|
|
drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
2023-12-02 19:56:24 +04:00
|
|
|
|
draw();
|
|
|
|
|
}
|
2023-12-02 20:06:46 +04:00
|
|
|
|
private void buttonCreatePlaneClick(){
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
2023-12-30 09:28:39 +04:00
|
|
|
|
Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
|
|
|
|
drawingPlane = new DrawingAir(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor,
|
2023-12-02 20:06:46 +04:00
|
|
|
|
pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
|
2023-12-30 09:28:39 +04:00
|
|
|
|
drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
2023-12-02 20:06:46 +04:00
|
|
|
|
draw();
|
|
|
|
|
}
|
|
|
|
|
private void buttonStepClick(){
|
2023-12-30 09:28:39 +04:00
|
|
|
|
if (drawingPlane == null) {
|
2023-12-02 20:06:46 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (comboBoxStrategy.isEnabled()) {
|
|
|
|
|
;
|
|
|
|
|
switch(comboBoxStrategy.getSelectedIndex()) {
|
|
|
|
|
case 0:
|
|
|
|
|
abstractStrategy = new MoveToCenter();
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
abstractStrategy = new MoveToBorder();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
abstractStrategy = null;
|
|
|
|
|
break;
|
|
|
|
|
};
|
|
|
|
|
if (abstractStrategy == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-30 09:28:39 +04:00
|
|
|
|
abstractStrategy.SetData(new DrawingObjectPlane(drawingPlane), pictureBox.getWidth(),
|
2023-12-02 20:06:46 +04:00
|
|
|
|
pictureBox.getHeight());
|
|
|
|
|
comboBoxStrategy.setEnabled(false);
|
|
|
|
|
}
|
|
|
|
|
if (abstractStrategy == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
abstractStrategy.MakeStep();
|
|
|
|
|
draw();
|
|
|
|
|
if (abstractStrategy.GetStatus() == Status.FINISH)
|
|
|
|
|
{
|
|
|
|
|
comboBoxStrategy.setEnabled(true);
|
|
|
|
|
abstractStrategy = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-02 19:56:24 +04:00
|
|
|
|
private void buttonMoveClick(ActionEvent event) {
|
2023-12-30 09:28:39 +04:00
|
|
|
|
if(drawingPlane == null || drawingPlane.getEntityAir() == null)
|
2023-12-02 19:56:24 +04:00
|
|
|
|
return;
|
|
|
|
|
switch (event.getActionCommand())
|
|
|
|
|
{
|
|
|
|
|
case "left":
|
2023-12-30 09:28:39 +04:00
|
|
|
|
drawingPlane.moveTransport(DirectionType.LEFT);
|
2023-12-02 19:56:24 +04:00
|
|
|
|
break;
|
|
|
|
|
case "right":
|
2023-12-30 09:28:39 +04:00
|
|
|
|
drawingPlane.moveTransport(DirectionType.RIGHT);
|
2023-12-02 19:56:24 +04:00
|
|
|
|
break;
|
|
|
|
|
case "up":
|
2023-12-30 09:28:39 +04:00
|
|
|
|
drawingPlane.moveTransport(DirectionType.UP);
|
2023-12-02 19:56:24 +04:00
|
|
|
|
break;
|
|
|
|
|
case "down":
|
2023-12-30 09:28:39 +04:00
|
|
|
|
drawingPlane.moveTransport(DirectionType.DOWN);
|
2023-12-02 19:56:24 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
draw();
|
|
|
|
|
}
|
|
|
|
|
private void draw() {
|
2023-12-30 09:28:39 +04:00
|
|
|
|
if (drawingPlane == null)
|
2023-12-02 19:56:24 +04:00
|
|
|
|
return;
|
|
|
|
|
pictureBox.repaint();
|
|
|
|
|
}
|
2023-12-30 09:28:39 +04:00
|
|
|
|
public void select(){
|
|
|
|
|
if (drawingPlane == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
selectedPlane = drawingPlane;
|
|
|
|
|
}
|
2023-12-02 19:56:24 +04:00
|
|
|
|
}
|