PIbd-21_Markov_D.P._Contain.../FormShip.java
2022-12-02 00:06:03 +04:00

130 lines
5.7 KiB
Java
Raw 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.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.awt.image.BufferedImage;
import java.util.Random;
public class FormShip extends JFrame{
public JPanel Mainpanel;
private Random random = new Random();
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
private JButton ButtonCreateModif;
protected DrawingShip _ship;
private JPanel pictureBoxShip;
private JToolBar StatusStrip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
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);
}
validate();
}
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 {
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/2.png"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/1.png"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/3.png"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ButtonCreate.addActionListener(new ActionListener() {
@Override
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);
}
});
pictureBoxShip.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
if(_ship!=null)
{
_ship.ChangeBorders(pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
pictureBoxShip.revalidate();
Draw(_ship);
}
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_ship.MoveTransport(Direction.Up);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_ship.MoveTransport(Direction.Down);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_ship.MoveTransport(Direction.Right);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_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);
}
});
}
}