diff --git a/src/DrawingField.java b/src/DrawingField.java new file mode 100644 index 0000000..b46e965 --- /dev/null +++ b/src/DrawingField.java @@ -0,0 +1,63 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawingField extends JPanel{ + private final FormWarship Field; + DrawingWarship _warship=null; + public DrawingField(FormWarship field) { + Field = field; + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 =(Graphics2D)g; + if (_warship!=null) + _warship.DrawTransport(g2); + else return; + } + public void DirectionButtonAction(Direction side){ + if(_warship == null) + return; + _warship.MoveTransport(side); + } + private void SetData() { + Random rand=new Random(); + _warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); + Field.SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed()); + Field.WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight()); + Field.BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2)); + } + public void CreateButtonAction(){ + Random rand=new Random(); + Color color1 = JColorChooser.showDialog(Field, "Выберите цвет тела корабля", null); + if(color1==null) + color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + _warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3)); + SetData(); + } + public void CreateModifButtonAction(){ + Random rand=new Random(); + Color color1=JColorChooser.showDialog(Field, "Выберите цвет тела корабля",null); + Color color2=JColorChooser.showDialog(Field, "Выборите цвет модификаций корабля",null); + if(color1==null) + color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + if (color2==null) + color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + _warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1, + color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); + SetData(); + } + public void ResizeField(){ + if (_warship!=null) + _warship.ChangeBorders(getWidth(),getHeight()); + else return; + } + + public void Draw(Graphics2D graphics) { + if (_warship!=null) + _warship.DrawTransport(graphics); + else return; + } +} diff --git a/src/FormMap.form b/src/FormMap.form index 0617004..ebc0e7f 100644 --- a/src/FormMap.form +++ b/src/FormMap.form @@ -170,15 +170,6 @@ - - - - - - - - - diff --git a/src/FormWarship.form b/src/FormWarship.form new file mode 100644 index 0000000..741a544 --- /dev/null +++ b/src/FormWarship.form @@ -0,0 +1,164 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormWarship.java b/src/FormWarship.java new file mode 100644 index 0000000..08b77e7 --- /dev/null +++ b/src/FormWarship.java @@ -0,0 +1,80 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; + +public class FormWarship extends JFrame{ + private int Width; + private int Height; + DrawingField field = new DrawingField(this); + 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; + + public FormWarship(){ + super("Военный корабль"); + setContentPane(PictureBox); + setSize(1000,700); + Width = getWidth(); + Height = getHeight(); + ShowWindow(); + field.setBounds(0,0,Width,Height); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void ShowWindow(){ + + ButtonCreate.addActionListener(e -> { + field.CreateButtonAction(); + ReDraw(); + }); + ButtonCreateModif.addActionListener(e -> { + field.CreateModifButtonAction(); + ReDraw(); + }); + ButtonUp.addActionListener(e -> { + field.DirectionButtonAction(Direction.Up); + ReDraw(); + }); + ButtonLeft.addActionListener(e -> { + field.DirectionButtonAction(Direction.Left); + ReDraw(); + }); + ButtonRight.addActionListener(e -> { + field.DirectionButtonAction(Direction.Right); + ReDraw(); + }); + ButtonDown.addActionListener(e -> { + field.DirectionButtonAction(Direction.Down); + ReDraw(); + }); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e){ + super.componentResized(e); + Width=getWidth(); + Height=getHeight(); + + field.ResizeField(); + ReDraw(); + field.setBounds(0,0,Width,Height); + } + }); + } + private void ReDraw() + { + Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); + graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + PictureBox.paintComponents(graphics); + field.Draw(graphics); + } +} diff --git a/src/Main.java b/src/Main.java index d27a952..7b927cf 100644 --- a/src/Main.java +++ b/src/Main.java @@ -2,4 +2,4 @@ public class Main { public static void main(String[] args) { new FormMap(); } -} +} \ No newline at end of file