131 lines
4.9 KiB
Java
131 lines
4.9 KiB
Java
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 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 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();
|
||
ship.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
|
||
ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
|
||
JLabelSpeed.setText("Cкорость: " + ship.GetWarmlyShip().GetSpeed() + " ");
|
||
JLabelWeight.setText("Вес: " + ship.GetWarmlyShip().GetWeight() + " ");
|
||
JLabelColor.setText(("Цвет: " + ship.GetWarmlyShip().GetBodyColor() + " "));
|
||
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();
|
||
}
|
||
});
|
||
}
|
||
}
|