PIbd-21_Markov_D.P._Contain.../FormShip.java

150 lines
6.3 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-08 21:14:01 +04:00
public class FormShip extends JDialog{
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;
2022-12-08 21:14:01 +04:00
private JButton ButtonSelect;
2022-12-02 00:06:03 +04:00
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-08 21:14:01 +04:00
protected DrawingShip SelectedShip;
public DrawingShip GetSelectedShip()
{
return SelectedShip;
}
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);
2022-12-08 18:53:50 +04:00
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(pictureBoxShip.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageOfShip,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
}
public FormShip() {
2022-12-08 21:14:01 +04:00
super(new Frame("Корабль"));
2022-12-01 23:30:57 +04:00
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){
2022-12-08 21:14:01 +04:00
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
_ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst);
2022-12-02 00:06:03 +04:00
_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) {
2022-12-08 21:14:01 +04:00
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
_ship=new DrawingContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean());
2022-12-02 00:06:03 +04:00
_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-08 21:14:01 +04:00
ButtonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SelectedShip=_ship;
dispose();
}
});
setModal(true);
getContentPane().add(Mainpanel);
2022-12-01 23:30:57 +04:00
}
}