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

158 lines
6.1 KiB
Java
Raw Normal View History

2022-11-28 00:15:39 +04:00
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
2022-12-13 21:37:45 +04:00
import java.awt.image.*;
2022-11-28 00:15:39 +04:00
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;
2022-12-13 21:37:45 +04:00
private JPanel GraphicsOutput;
private JButton buttonCreateModif;
2022-11-28 00:15:39 +04:00
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
if (pictureBomber.getAirBomber() == null) return;
2022-12-13 21:37:45 +04:00
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() + " "));
2022-11-28 00:15:39 +04:00
}
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();
2022-12-13 20:24:12 +04:00
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)));
2022-12-13 21:37:45 +04:00
SetData();
2022-11-28 00:15:39 +04:00
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");
}
});
2022-12-13 21:37:45 +04:00
/*pictureBomber.addComponentListener(new ComponentAdapter() {
2022-11-28 00:15:39 +04:00
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
pictureBomber.ChangeBorders(pictureBomber.getWidth(), pictureBomber.getHeight());
Draw();
}
2022-12-13 21:37:45 +04:00
});*/
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();
}
2022-11-28 00:15:39 +04:00
});
}
}