130 lines
5.7 KiB
Java
Raw Normal View History

2022-12-01 23:30:57 +04:00
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;
2022-12-02 00:06:03 +04:00
import java.awt.image.BufferedImage;
2022-12-01 23:30:57 +04:00
import java.util.Random;
2022-12-02 00:06:03 +04:00
public class FormShip extends JFrame{
2022-12-01 23:30:57 +04:00
public JPanel Mainpanel;
2022-12-02 00:06:03 +04:00
private Random random = new Random();
2022-12-01 23:30:57 +04:00
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
2022-12-02 00:06:03 +04:00
private JButton ButtonCreateModif;
protected DrawingShip _ship;
private JPanel pictureBoxShip;
2022-12-01 23:30:57 +04:00
private JToolBar StatusStrip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
2022-12-02 00:06:03 +04:00
public void Draw(DrawingShip _ship) {
pictureBoxShip.removeAll();
BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
if (_ship != null) {
_ship.DrawTransport(gr);
JLabel imageOfLoco = new JLabel();
imageOfLoco.setPreferredSize(pictureBoxShip.getSize());
imageOfLoco.setMinimumSize(new Dimension(1, 1));
imageOfLoco.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageOfLoco,BorderLayout.CENTER);
2022-12-01 23:30:57 +04:00
}
2022-12-02 00:06:03 +04:00
validate();
2022-12-01 23:30:57 +04:00
}
2022-12-02 00:06:03 +04:00
2022-12-01 23:30:57 +04:00
public FormShip() {
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
StatusStrip.add(LabelBox);
try {
2022-12-02 00:06:03 +04:00
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
2022-12-01 23:30:57 +04:00
ButtonUp.setIcon(new ImageIcon(img));
2022-12-02 00:06:03 +04:00
img = ImageIO.read(FormShip.class.getResource("Images/2.png"));
2022-12-01 23:30:57 +04:00
ButtonDown.setIcon(new ImageIcon(img));
2022-12-02 00:06:03 +04:00
img = ImageIO.read(FormShip.class.getResource("Images/1.png"));
2022-12-01 23:30:57 +04:00
ButtonLeft.setIcon(new ImageIcon(img));
2022-12-02 00:06:03 +04:00
img = ImageIO.read(FormShip.class.getResource("Images/3.png"));
2022-12-01 23:30:57 +04:00
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ButtonCreate.addActionListener(new ActionListener() {
@Override
2022-12-02 00:06:03 +04:00
public void actionPerformed(ActionEvent e){
_ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
JLabelSpeed.setText("орость: " + _ship.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
Draw(_ship);
2022-12-01 23:30:57 +04:00
}
});
2022-12-02 00:06:03 +04:00
pictureBoxShip.addComponentListener(new ComponentAdapter() {
2022-12-01 23:30:57 +04:00
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
2022-12-02 00:06:03 +04:00
if(_ship!=null)
{
_ship.ChangeBorders(pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
pictureBoxShip.revalidate();
Draw(_ship);
}
2022-12-01 23:30:57 +04:00
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2022-12-02 00:06:03 +04:00
_ship.MoveTransport(Direction.Up);
pictureBoxShip.revalidate();
Draw(_ship);
2022-12-01 23:30:57 +04:00
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2022-12-02 00:06:03 +04:00
_ship.MoveTransport(Direction.Down);
pictureBoxShip.revalidate();
Draw(_ship);
2022-12-01 23:30:57 +04:00
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2022-12-02 00:06:03 +04:00
_ship.MoveTransport(Direction.Right);
pictureBoxShip.revalidate();
Draw(_ship);
2022-12-01 23:30:57 +04:00
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2022-12-02 00:06:03 +04:00
_ship.MoveTransport(Direction.Left);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_ship=new DrawingContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),random.nextBoolean(),random.nextBoolean());
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
JLabelSpeed.setText("орость: " + _ship.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
Draw(_ship);
2022-12-01 23:30:57 +04:00
}
});
}
}