PIbd-21_Markov_D.P._Contain.../FormShip.java
2022-12-08 21:14:01 +04:00

150 lines
6.3 KiB
Java
Raw Permalink 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 JDialog{
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;
private JButton ButtonSelect;
protected DrawingShip _ship;
private JPanel pictureBoxShip;
private JToolBar StatusStrip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
protected DrawingShip SelectedShip;
public DrawingShip GetSelectedShip()
{
return SelectedShip;
}
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 imageOfShip = new JLabel();
imageOfShip.setPreferredSize(pictureBoxShip.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageOfShip,BorderLayout.CENTER);
}
validate();
}
public FormShip() {
super(new Frame("Корабль"));
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){
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
_ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst);
_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) {
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());
_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);
}
});
ButtonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SelectedShip=_ship;
dispose();
}
});
setModal(true);
getContentPane().add(Mainpanel);
}
}