PIbd-21_Yaruskin_S.A_AirBom.../src/FrameAirBomber.java

108 lines
5.0 KiB
Java
Raw Normal View History

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 {
private DrawingAirBomber drawingAirBomber;
private final JComponent pictureBox;
public FrameAirBomber() throws IOException {
super("Бомбардировщик");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingAirBomber != null) drawingAirBomber.drawTransport(graphics2D);
super.repaint();
}
};
JButton createButton = new JButton("Создать");
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"))));
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
createButton.addActionListener(e -> buttonCreateClick());
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());
JPanel panelBattleship = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new BorderLayout());
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
createPanel.add(createButton, BorderLayout.SOUTH);
JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(movementPanel, BorderLayout.SOUTH);
rightButton.setPreferredSize(new Dimension(30,30));
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 2;
constraints.gridy = 1;
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
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);
add(pictureBox);
panelBattleship.add(rightPanel, BorderLayout.EAST);
panelBattleship.add(createPanel, BorderLayout.WEST);
add(panelBattleship,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateClick() {
Random random = new Random();
drawingAirBomber = new DrawingAirBomber();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingAirBomber.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(),
(random.nextInt(3)+1)*2);
drawingAirBomber.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonMoveClick(ActionEvent event) {
if(drawingAirBomber == null || drawingAirBomber.getEntityAirBomber() == null)
return;
switch (event.getActionCommand())
{
case "left":
drawingAirBomber.moveTransport(DirectionType.LEFT);
break;
case "right":
drawingAirBomber.moveTransport(DirectionType.RIGHT);
break;
case "up":
drawingAirBomber.moveTransport(DirectionType.UP);
break;
case "down":
drawingAirBomber.moveTransport(DirectionType.DOWN);
break;
}
draw();
}
private void draw() {
if (drawingAirBomber == null)
return;
pictureBox.repaint();
}
}