PIbd-22_Aleikin_A.M._AirBom.../FormAirBomber.java

116 lines
4.2 KiB
Java
Raw Permalink 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.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class FormAirBomber {
private JToolBar statusStrip;
public JPanel Mainpanel;
private DrawningBomber pictureBomber;
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonDown;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
if (pictureBomber.getAirBomber() == null) return;
pictureBomber.DrawTransport();
}
private void ButtonMove_Click(String name)
{
if (pictureBomber == null) return;
switch (name)
{
case "buttonLeft":
pictureBomber.MoveTransport(Direction.Left);
break;
case "buttonUp":
pictureBomber.MoveTransport(Direction.Up);
break;
case "buttonRight":
pictureBomber.MoveTransport(Direction.Right);
break;
case "buttonDown":
pictureBomber.MoveTransport(Direction.Down);
break;
}
Draw();
}
public FormAirBomber() {
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
statusStrip.add(LabelBox);
try {
Image img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowUp.jpg"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowLeft.jpg"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowDown.jpg"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowRight.jpg"));
buttonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
pictureBomber.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBomber.getWidth(), pictureBomber.getHeight());
JLabelSpeed.setText("орость: " + pictureBomber.getAirBomber().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " "));
Draw();
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonUp");
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonLeft");
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonDown");
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonRight");
}
});
pictureBomber.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
pictureBomber.ChangeBorders(pictureBomber.getWidth(), pictureBomber.getHeight());
Draw();
}
});
}
}