118 lines
4.0 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.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormWarship extends JDialog{
private int Width;
private int Height;
DrawingWarship SelectedWarship;
private JButton ButtonDown;
private JButton ButtonRight;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonCreate;
private JButton ButtonCreateModif;
public JLabel SpeedLabel;
public JLabel WeightLabel;
public JLabel BodyColorLabel;
private JPanel PictureBox;
private JButton ButtonSelect;
private DrawingWarship _warship=null;
public FormWarship(){
setTitle("Военный корабль");
setContentPane(PictureBox);
setSize(1000,500);
setResizable(false);
ShowWindow();
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
private void SetData() {
Random rand=new Random();
_warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed());
WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight());
BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2));
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_warship != null) {
PictureBox.paintComponents(PictureBox.getGraphics());
_warship.DrawTransport(g);
}
}
private void ShowWindow(){
ButtonCreate.addActionListener(e -> {
Random rand=new Random();
_warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3));
SetData();
repaint();
});
ButtonCreateModif.addActionListener(e -> {
Random rand=new Random();
_warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3));
SetData();
repaint();
});
ButtonUp.addActionListener(e -> {
if(_warship == null)
return;
_warship.MoveTransport(Direction.Up);
repaint();
});
ButtonLeft.addActionListener(e -> {
if(_warship == null)
return;
_warship.MoveTransport(Direction.Left);
repaint();
});
ButtonRight.addActionListener(e -> {
if(_warship == null)
return;
_warship.MoveTransport(Direction.Right);
repaint();
});
ButtonDown.addActionListener(e -> {
if(_warship == null)
return;
_warship.MoveTransport(Direction.Down);
repaint();
});
ButtonSelect.addActionListener(e -> {
SelectedWarship=GetDrawingWarship();
JOptionPane.showMessageDialog(PictureBox, "Корабль добавлен.");
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e){
super.componentResized(e);
Width=getWidth();
Height=getHeight();
if (_warship!=null)
_warship.ChangeBorders(getWidth(),getHeight());
else return;
repaint();
setBounds(0,0,Width,Height);
}
});
}
public DrawingWarship GetDrawingWarship() {
return _warship;
}
public void SetDrawingWarship(DrawingWarship warship) {
_warship=warship;
SetData();
repaint();
}
}