2023-11-27 23:03:39 +04:00

167 lines
5.2 KiB
Java

package AirBomber;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
public class BomberForm extends JFrame
{
private BomberRenderer _bomberRenderer;
public BomberForm()
{
InitializeComponent();
}
private void Draw()
{
BufferedImage bmp = new BufferedImage(
BomberPictureBox.getWidth(),
BomberPictureBox.getHeight(),
BufferedImage.TYPE_INT_ARGB
);
Graphics2D g = bmp.createGraphics();
_bomberRenderer.DrawEntity(g);
BomberPictureBox.setIcon(new ImageIcon(bmp));
}
private void ButtonCreate_Click(ActionEvent e)
{
Random random = new Random();
_bomberRenderer = new BomberRenderer();
_bomberRenderer.Init(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
true,
true,
BomberPictureBox.getWidth(),
BomberPictureBox.getHeight()
);
_bomberRenderer.SetPosition(random.nextInt(20, 100), random.nextInt(20, 100));
Draw();
}
private void ButtonMove_Click(ActionEvent e)
{
if (_bomberRenderer == null)
return;
String ButtonName = ((JButton)e.getSource()).getName();
switch (ButtonName)
{
case "ButtonUp":
_bomberRenderer.MoveEntity(DirectionType.Up);
break;
case "ButtonDown":
_bomberRenderer.MoveEntity(DirectionType.Down);
break;
case "ButtonLeft":
_bomberRenderer.MoveEntity(DirectionType.Left);
break;
case "ButtonRight":
_bomberRenderer.MoveEntity(DirectionType.Right);
break;
}
Draw();
}
private void InitializeComponent()
{
BomberPictureBox = new JLabel();
CreateButton = new JButton();
ButtonRight = new JButton();
ButtonDown = new JButton();
ButtonLeft = new JButton();
ButtonUp = new JButton();
//
// BomberPictureBox
//
BomberPictureBox.setBounds(0, 0, 884, 461);
//
// CreateButton
//
CreateButton.setName("CreateButton");
CreateButton.setBounds(12, 419, 80, 30);
CreateButton.setText("Создать");
CreateButton.setBackground(new Color(225, 225, 225));
CreateButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
CreateButton.setFocusable(false);
CreateButton.addActionListener(e -> ButtonCreate_Click(e));
//
// ButtonRight
//
ButtonRight.setName("ButtonRight");
ButtonRight.setBounds(842, 419, 30, 30);
ButtonRight.setBackground(new Color(225, 225, 225));
ButtonRight.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonRight.setFocusable(false);
ButtonRight.setIcon(new ImageIcon("src/Resources/ArrowRight.png"));
ButtonRight.addActionListener(e -> ButtonMove_Click(e));
//
// ButtonDown
//
ButtonDown.setName("ButtonDown");
ButtonDown.setBounds(806, 419, 30, 30);
ButtonDown.setBackground(new Color(225, 225, 225));
ButtonDown.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonDown.setFocusable(false);
ButtonDown.setIcon(new ImageIcon("src/Resources/ArrowDown.png"));
ButtonDown.addActionListener(e -> ButtonMove_Click(e));
//
// ButtonLeft
//
ButtonLeft.setName("ButtonLeft");
ButtonLeft.setBounds(770, 419, 30, 30);
ButtonLeft.setBackground(new Color(225, 225, 225));
ButtonLeft.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonLeft.setFocusable(false);
ButtonLeft.setIcon(new ImageIcon("src/Resources/ArrowLeft.png"));
ButtonLeft.addActionListener(e -> ButtonMove_Click(e));
//
// ButtonUp
//
ButtonUp.setName("ButtonUp");
ButtonUp.setBounds(806, 383, 30, 30);
ButtonUp.setBackground(new Color(225, 225, 225));
ButtonUp.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonUp.setFocusable(false);
ButtonUp.setIcon(new ImageIcon("src/Resources/ArrowUp.png"));
ButtonUp.addActionListener(e -> ButtonMove_Click(e));
//
// BomberForm
//
setTitle("Бомбардировщик");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900, 500);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
add(ButtonUp);
add(ButtonLeft);
add(ButtonDown);
add(ButtonRight);
add(CreateButton);
add(BomberPictureBox);
}
private JLabel BomberPictureBox;
private JButton CreateButton;
private JButton ButtonRight;
private JButton ButtonDown;
private JButton ButtonLeft;
private JButton ButtonUp;
}