120 lines
4.2 KiB
Java
120 lines
4.2 KiB
Java
import javax.imageio.ImageIO;
|
||
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.awt.event.ComponentAdapter;
|
||
import java.awt.event.ComponentEvent;
|
||
import java.util.Random;
|
||
|
||
public class FormAirbus
|
||
{
|
||
protected DrawingPlane plane = new DrawingPlane();
|
||
|
||
public JPanel MainPanel;
|
||
private JButton ButtonCreate;
|
||
private JButton ButtonLeft;
|
||
private JButton ButtonDown;
|
||
private JButton ButtonRight;
|
||
private JButton ButtonUp;
|
||
private JToolBar StatusStrip;
|
||
private JPanel PictureBox;
|
||
private JLabel LabelSpeed = new JLabel();
|
||
private JLabel LabelWeight = new JLabel();
|
||
private JLabel LabelColor = new JLabel();
|
||
|
||
public FormAirbus()
|
||
{
|
||
//создание строки отображения скорости, веса и цвета объекта
|
||
Box LableBox = Box.createHorizontalBox();
|
||
LableBox.setMinimumSize(new Dimension(1, 20));
|
||
LableBox.add(LabelSpeed);
|
||
LableBox.add(LabelWeight);
|
||
LableBox.add(LabelColor);
|
||
StatusStrip.add(LableBox);
|
||
|
||
try
|
||
{
|
||
Image img = ImageIO.read(getClass().getResource("resourses/Up.png"));
|
||
ButtonUp.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(getClass().getResource("resourses/Left.png"));
|
||
ButtonLeft.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(getClass().getResource("resourses/Down.png"));
|
||
ButtonDown.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(getClass().getResource("resourses/Right.png"));
|
||
ButtonRight.setIcon(new ImageIcon(img));
|
||
} catch (Exception ex){
|
||
System.out.println(ex.getMessage());
|
||
}
|
||
|
||
|
||
ButtonCreate.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
plane = new DrawingPlane();
|
||
Random rnd = new Random();
|
||
plane.Initial(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||
plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
|
||
PictureBox.getWidth(), PictureBox.getHeight());
|
||
LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " ");
|
||
LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " ");
|
||
LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() +
|
||
" b = " + plane.GetPlane().GetColor().getBlue());
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
//обновление размеров формы
|
||
MainPanel.addComponentListener(new ComponentAdapter() {
|
||
@Override
|
||
public void componentResized(ComponentEvent e) {
|
||
super.componentResized(e);
|
||
plane.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight());
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
ButtonUp.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
plane.MoveTransport(Direction.Up);
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
ButtonLeft.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
plane.MoveTransport(Direction.Left);
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
ButtonDown.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
plane.MoveTransport(Direction.Down);
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
ButtonRight.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
plane.MoveTransport(Direction.Right);
|
||
Draw();
|
||
}
|
||
});
|
||
}
|
||
|
||
public void Draw()
|
||
{
|
||
if(plane.GetPlane() == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
plane.DrawTransport(PictureBox.getGraphics());
|
||
}
|
||
} |