From bad7c423b4ec8a3485e0cba8ad5dfa8f15150680 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Sat, 10 Sep 2022 23:20:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.=20=D0=A3=D1=81?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F.=20?= =?UTF-8?q?=D0=91=D0=B5=D0=B7=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Direction.java | 7 +++ src/DrawingField.java | 57 +++++++++++++++++ src/DrawingWarship.java | 130 ++++++++++++++++++++++++++++++++++++++ src/EntityWarship.java | 30 +++++++++ src/FormWarship.java | 136 ++++++++++++++++++++++++++++++++++++++++ src/Main.java | 2 +- 6 files changed, 361 insertions(+), 1 deletion(-) create mode 100644 src/Direction.java create mode 100644 src/DrawingField.java create mode 100644 src/DrawingWarship.java create mode 100644 src/EntityWarship.java create mode 100644 src/FormWarship.java diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..c7b3e68 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,7 @@ +public enum Direction { + Up(1), + Down(2), + Left(3), + Right(4); + Direction(int value){} +} diff --git a/src/DrawingField.java b/src/DrawingField.java new file mode 100644 index 0000000..3c4fa0f --- /dev/null +++ b/src/DrawingField.java @@ -0,0 +1,57 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawingField extends JPanel { + private final FormWarship field; + DrawingWarship _warship; + public DrawingField(FormWarship field) { + this.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 UpButtonAction(){ + if (_warship!=null) + _warship.MoveTransport(Direction.Up); + else + return; + } + public void DownButtonAction(){ + if (_warship!=null) + _warship.MoveTransport(Direction.Down); + else + return; + } + public void RightButtonAction(){ + if (_warship!=null) + _warship.MoveTransport(Direction.Right); + else + return; + } + public void LeftButtonAction(){ + if (_warship!=null) + _warship.MoveTransport(Direction.Left); + else + return; + } + public void CreateButtonAction(){ + Random rand=new Random(); + _warship=new DrawingWarship(); + _warship.Init(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256))); + _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 ResizeField(){ + if (_warship!=null) + _warship.ChangeBorders(getWidth(),getHeight()); + else return; + } +} diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java new file mode 100644 index 0000000..9c929f0 --- /dev/null +++ b/src/DrawingWarship.java @@ -0,0 +1,130 @@ +import java.awt.*; + +public class DrawingWarship { + private EntityWarship Warship; + public EntityWarship GetWarship(){ + return Warship; + } + private int _startPosX; + private int _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _warshipWidth = 120; + private final int _warshipHeight = 40; + + public void Init(int speed, float weight, Color bodyColor) + { + Warship = new EntityWarship(); + Warship.Init(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (x >= 0 && x+_warshipWidth <= width && y >= 0 && y+_warshipHeight <= height) + { + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + else return; + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + switch (direction) + { + case Right: + if (_startPosX + _warshipWidth + Warship.Step < _pictureWidth) + { + _startPosX+=Warship.Step; + } + break; + + case Left: + if (_startPosX- Warship.Step >= 0) + { + _startPosX -= Warship.Step; + } + break; + + case Up: + if (_startPosY - Warship.Step >= 0) + { + _startPosY -= Warship.Step; + } + break; + + case Down: + if (_startPosY + _warshipHeight + Warship.Step < _pictureHeight) + { + _startPosY += Warship.Step; + } + break; + } + } + + public void DrawTransport(Graphics g) + { + Graphics2D g2 = (Graphics2D) g; + int [] x ={_startPosX+5,_startPosX+80,_startPosX+120,_startPosX+80,_startPosX+5,_startPosX+5}; + int [] y ={_startPosY,_startPosY,_startPosY+20,_startPosY+40,_startPosY+40,_startPosY}; + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null) + { + return; + } + + g2.setColor(Warship.GetBodyColor()); + + g2.fillPolygon(x,y,6); + + g2.setColor(Color.BLACK); + + g2.drawLine(_startPosX + 5, _startPosY, _startPosX + 80, _startPosY); + g2.drawLine(_startPosX + 80, _startPosY, _startPosX + 120, _startPosY + 20); + g2.drawLine(_startPosX + 120, _startPosY + 20, _startPosX + 80, _startPosY + 40); + g2.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 5, _startPosY + 40); + g2.drawLine(_startPosX + 5, _startPosY + 40, _startPosX + 5, _startPosY); + + g2.setColor(Color.BLACK); + + g2.fillRect(_startPosX, _startPosY + 5, 5, 10); + g2.fillRect(_startPosX, _startPosY + 25, 5, 10); + + g2.setColor(Color.DARK_GRAY); + + g2.drawRect(_startPosX + 30, _startPosY + 15, 20, 10); + g2.fillRect(_startPosX+30, _startPosY + 15, 20, 10); + g2.drawRect(_startPosX + 50, _startPosY + 10, 10, 20); + g2.fillRect(_startPosX + 50, _startPosY + 10, 10, 20); + + g2.setColor(Color.BLUE); + + g2.drawOval(_startPosX + 70, _startPosY + 15, 10, 10); + g2.fillOval(_startPosX + 70, _startPosY + 15, 10, 10); + } + + public void ChangeBorders(int width,int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth<=_warshipWidth||_pictureHeight<=_warshipHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _warshipWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _warshipWidth; + } + if (_startPosY + _warshipHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _warshipHeight; + } + } +} diff --git a/src/EntityWarship.java b/src/EntityWarship.java new file mode 100644 index 0000000..157944d --- /dev/null +++ b/src/EntityWarship.java @@ -0,0 +1,30 @@ +import java.awt.*; +import java.util.Random; + +public class EntityWarship { + private int Speed; + public int GetSpeed() { + return Speed; + } + + private float Weight; + public float GetWeight() { + return Weight; + } + + private Color BodyColor; + public Color GetBodyColor() { + return BodyColor; + } + + public int Step; + + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(60)+10 : speed; + Weight = weight <= 0 ? rnd.nextInt(23000)+20000 : weight; + BodyColor= bodyColor; + Step = Speed * 2000 / (int)Weight; + } +} diff --git a/src/FormWarship.java b/src/FormWarship.java new file mode 100644 index 0000000..4104172 --- /dev/null +++ b/src/FormWarship.java @@ -0,0 +1,136 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +public class FormWarship extends JFrame{ + 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); + + + public FormWarship(){ + super("Военный корабль"); + setSize(700,400); + Width=getWidth(); + Height=getHeight(); + ShowWindow(); + RefreshWindow(); + 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(); + }); + + ButtonDown.setPreferredSize(dimen); + ButtonDown.addActionListener(e->{ + field.DownButtonAction(); + repaint(); + }); + + ButtonRight.setPreferredSize(dimen); + ButtonRight.addActionListener(e->{ + field.RightButtonAction(); + repaint(); + }); + + ButtonLeft.setPreferredSize(dimen); + ButtonLeft.addActionListener(e->{ + field.LeftButtonAction(); + repaint(); + }); + + 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(); + }); + + 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) { + super.componentResized(e); + Width=getWidth(); + Height=getHeight(); + + field.ResizeField(); + repaint(); + RefreshWindow(); + } + }); + } + public void RefreshWindow(){ + field.setBounds(0,0,Width,Height); + BottomAndCreatePanel.setBounds(-220,Height-110,Width,80); + DimentionPanel.setBounds(Width-170,Height-170,190,140); + } +} diff --git a/src/Main.java b/src/Main.java index 1ef8157..f00cbba 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - + new FormWarship(); } }