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

158 lines
6.1 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.awt.image.*;
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 JPanel GraphicsOutput;
private JButton buttonCreateModif;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
if (pictureBomber.getAirBomber() == null) return;
GraphicsOutput.removeAll();
BufferedImage bmp = new BufferedImage(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
g.setColor(new Color(238, 238, 238));
g.fillRect(0, 0, GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
pictureBomber.DrawTransport(g);
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
//validate();
}
private void SetData()
{
Random random = new Random();
pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
JLabelSpeed.setText("орость: " + pictureBomber.getAirBomber().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " "));
}
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 = new DrawningBomber(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
SetData();
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();
}
});*/
GraphicsOutput.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
if (pictureBomber == null || pictureBomber.getAirBomber() == null) return;
pictureBomber.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
GraphicsOutput.revalidate();
Draw();
}
});
buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
pictureBomber = new DrawningWarplane(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)),
random.nextBoolean(), random.nextBoolean());
SetData();
Draw();
}
});
}
}