PIbd-22_Bondarenko_M.S._War.../FormShip.java

149 lines
5.7 KiB
Java
Raw Normal View History

2022-11-16 18:24:14 +04:00
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
2022-11-28 23:18:52 +04:00
public class FormShip extends JFrame{
2022-11-16 18:24:14 +04:00
private JToolBar statusStrip;
public JPanel Mainpanel;
2022-11-28 23:18:52 +04:00
private DrawingShip ship;
2022-11-16 18:24:14 +04:00
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonDown;
2022-11-28 23:18:52 +04:00
private JPanel GraphicsOutput;
private JButton buttonCreateModif;
2022-11-16 18:24:14 +04:00
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
2022-11-28 23:18:52 +04:00
if (ship == null || ship.GetWarmlyShip() == null) return;
GraphicsOutput.removeAll();
BufferedImage bmp = new BufferedImage(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
g.setColor(new Color(238, 238, 238));
g.fillRect(0, 0, GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
ship.DrawTransport(g);
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
validate();
2022-11-16 18:24:14 +04:00
}
private void SetData()
{
Random random = new Random();
ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
JLabelSpeed.setText("орость: " + ship.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + ship.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + ship.GetWarmlyShip().GetBodyColor() + " "));
}
2022-11-16 18:24:14 +04:00
private void ButtonMove_Click(String name)
{
2022-11-28 23:18:52 +04:00
if (ship == null) return;
2022-11-16 18:24:14 +04:00
switch (name)
{
case "buttonLeft":
2022-11-28 23:18:52 +04:00
ship.MoveTransport(Direction.Left);
2022-11-16 18:24:14 +04:00
break;
case "buttonUp":
2022-11-28 23:18:52 +04:00
ship.MoveTransport(Direction.Up);
2022-11-16 18:24:14 +04:00
break;
case "buttonRight":
2022-11-28 23:18:52 +04:00
ship.MoveTransport(Direction.Right);
2022-11-16 18:24:14 +04:00
break;
case "buttonDown":
2022-11-28 23:18:52 +04:00
ship.MoveTransport(Direction.Down);
2022-11-16 18:24:14 +04:00
break;
}
2022-11-28 23:18:52 +04:00
GraphicsOutput.revalidate();
2022-11-16 18:24:14 +04:00
Draw();
}
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/totop.png"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/todown.png"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toright.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();
2022-11-28 23:26:56 +04:00
ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
SetData();
2022-11-16 18:24:14 +04:00
Draw();
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonUp");
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonLeft");
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonDown");
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonRight");
}
});
2022-11-28 23:18:52 +04:00
GraphicsOutput.addComponentListener(new ComponentAdapter() {
2022-11-16 18:24:14 +04:00
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
2022-11-28 23:18:52 +04:00
if (ship == null || ship.GetWarmlyShip() == null) return;
ship.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
GraphicsOutput.revalidate();
2022-11-16 18:24:14 +04:00
Draw();
}
});
buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
ship = new DrawningMotorShip(random.nextInt(100, 300), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean());
SetData();
Draw();
}
});
2022-11-16 18:24:14 +04:00
}
}