PIbd-22_Bondarenko_M.S._War.../FormShip.java
2022-11-29 00:17:16 +04:00

149 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.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class FormShip extends JFrame{
private JToolBar statusStrip;
public JPanel Mainpanel;
private DrawingShip ship;
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonDown;
private JPanel GraphicsOutput;
private JButton buttonCreateModif;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
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();
}
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() + " "));
}
private void ButtonMove_Click(String name)
{
if (ship == null) return;
switch (name)
{
case "buttonLeft":
ship.MoveTransport(Direction.Left);
break;
case "buttonUp":
ship.MoveTransport(Direction.Up);
break;
case "buttonRight":
ship.MoveTransport(Direction.Right);
break;
case "buttonDown":
ship.MoveTransport(Direction.Down);
break;
}
GraphicsOutput.revalidate();
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();
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();
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");
}
});
GraphicsOutput.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
if (ship == null || ship.GetWarmlyShip() == null) return;
ship.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
GraphicsOutput.revalidate();
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();
}
});
}
}