diff --git a/.idea/WarshipHard.iml b/.idea/WarshipHard.iml index c90834f..353864a 100644 --- a/.idea/WarshipHard.iml +++ b/.idea/WarshipHard.iml @@ -3,6 +3,7 @@ + diff --git a/src/DrawingField.java b/src/DrawingField.java index 966b0a7..0275530 100644 --- a/src/DrawingField.java +++ b/src/DrawingField.java @@ -16,6 +16,11 @@ public class DrawingField extends JPanel { _warship.DrawTransport(g2); else return; } + public void DrawWarship(Graphics g){ + if (_warship!=null) + _warship.DrawTransport(g); + else return; + } public void UpButtonAction(){ if (_warship!=null) _warship.MoveTransport(Direction.Up); diff --git a/src/FormWarship.form b/src/FormWarship.form new file mode 100644 index 0000000..447e9ae --- /dev/null +++ b/src/FormWarship.form @@ -0,0 +1,156 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormWarship.java b/src/FormWarship.java index 4104172..752b5b5 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -1,120 +1,67 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; + public class FormWarship extends JFrame{ + JPanel BottomPanel; + JPanel CreatePanel; + JPanel DimentionPanel; + + JLabel SpeedLabel; + JLabel WeightLabel; + JLabel BodyColorLabel; + private int Width; private int Height; - - JPanel BottomPanel = new JPanel(); - JPanel CreatePanel = new JPanel(); - JPanel BottomAndCreatePanel = new JPanel(); - JPanel DimentionPanel = new JPanel(); - JPanel UPanel = new JPanel(); - JPanel DPanel = new JPanel(); - JPanel LRPanel = new JPanel(); - - JLabel SpeedLabel = new JLabel("Скорость: "); - JLabel WeightLabel = new JLabel("Вес: "); - JLabel BodyColorLabel = new JLabel("Цвет: "); - DrawingField field = new DrawingField(this); - JButton ButtonCreate=new JButton("Создать"); - - Icon iconUp = new ImageIcon("Resource\\arrowUp.jpg"); - JButton ButtonUp=new JButton(iconUp); - - Icon iconDown = new ImageIcon("Resource\\arrowDown.jpg"); - JButton ButtonDown=new JButton(iconDown); - - Icon iconRight = new ImageIcon("Resource\\arrowRight.jpg"); - JButton ButtonRight=new JButton(iconRight); - - Icon iconLeft = new ImageIcon("Resource\\arrowLeft.jpg"); - JButton ButtonLeft=new JButton(iconLeft); + JButton ButtonCreate; + JButton ButtonUp; + JButton ButtonDown; + JButton ButtonRight; + JButton ButtonLeft; + JPanel PictureBox; public FormWarship(){ super("Военный корабль"); + setContentPane(PictureBox); setSize(700,400); Width=getWidth(); Height=getHeight(); ShowWindow(); - RefreshWindow(); + field.setBounds(0,0,Width,Height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void ShowWindow(){ - Dimension dimen=new Dimension(30,30); - - ButtonUp.setPreferredSize(dimen); ButtonUp.addActionListener(e->{ field.UpButtonAction(); - repaint(); + ReDraw(); }); - ButtonDown.setPreferredSize(dimen); ButtonDown.addActionListener(e->{ field.DownButtonAction(); - repaint(); + ReDraw(); }); - ButtonRight.setPreferredSize(dimen); ButtonRight.addActionListener(e->{ field.RightButtonAction(); - repaint(); + ReDraw(); }); - ButtonLeft.setPreferredSize(dimen); ButtonLeft.addActionListener(e->{ field.LeftButtonAction(); - repaint(); + ReDraw(); }); - LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0)); - LRPanel.setBackground(new Color(0,0,0,0)); - LRPanel.add(ButtonLeft); - LRPanel.add(ButtonRight); - - UPanel.setLayout(new FlowLayout()); - UPanel.setBackground(new Color(0,0,0,0)); - UPanel.add(ButtonUp); - - DPanel.setLayout(new FlowLayout()); - DPanel.setBackground(new Color(0,0,0,0)); - DPanel.add(ButtonDown); - - DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS)); - DimentionPanel.setBackground(new Color(0,0,0,0)); - DimentionPanel.add(UPanel); - DimentionPanel.add(LRPanel); - DimentionPanel.add(DPanel); - add(DimentionPanel); - - CreatePanel.setLayout(new FlowLayout()); - CreatePanel.setBackground(new Color(0,0,0,0)); - CreatePanel.add(ButtonCreate); ButtonCreate.addActionListener(e->{ field.CreateButtonAction(); - repaint(); + ReDraw(); }); - - BottomPanel.setLayout(new FlowLayout()); - BottomPanel.setBackground(new Color(0,0,0,0)); - BottomPanel.add(SpeedLabel); - BottomPanel.add(WeightLabel); - BottomPanel.add(BodyColorLabel); - - BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS)); - BottomAndCreatePanel.setBackground(new Color(0,0,0,0)); - BottomAndCreatePanel.add(CreatePanel); - BottomAndCreatePanel.add(BottomPanel); - - add(BottomAndCreatePanel); - add(field); - addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { @@ -123,14 +70,17 @@ public class FormWarship extends JFrame{ Height=getHeight(); field.ResizeField(); - repaint(); - RefreshWindow(); + field.setBounds(0,0,Width,Height); + ReDraw(); } }); } - public void RefreshWindow(){ - field.setBounds(0,0,Width,Height); - BottomAndCreatePanel.setBounds(-220,Height-110,Width,80); - DimentionPanel.setBounds(Width-170,Height-170,190,140); + private void ReDraw() + { + Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); + graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + PictureBox.paintComponents(graphics); + field.DrawWarship(graphics); } } + diff --git a/src/Main.java b/src/Main.java index f00cbba..ec64b71 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,4 @@ public class Main { - public static void main(String[] args) { new FormWarship(); }