diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/ProjectGasolineTankerHard.iml b/.idea/ProjectGasolineTankerHard.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/.idea/ProjectGasolineTankerHard.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e0844bc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..661d1b8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/CountWheels.java b/src/CountWheels.java new file mode 100644 index 0000000..ac135b8 --- /dev/null +++ b/src/CountWheels.java @@ -0,0 +1,13 @@ +public enum CountWheels { + Two(2), + Three(3), + Four(4); + + private final int Value; + CountWheels(int Count){ + Value=Count; + } + public int getCountWheels(){ + return Value; + } +} diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..5b70d63 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right; +} diff --git a/src/DrawingField.java b/src/DrawingField.java new file mode 100644 index 0000000..0fc63dd --- /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 FormGasolineTanker field; + DrawingGasolineTanker _gasolineTanker; + public DrawingField(FormGasolineTanker field) { + this.field = field; + } + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 =(Graphics2D)g; + if (_gasolineTanker!=null) + _gasolineTanker.DrawTransport(g2); + else return; + } + public void UpButtonAction(){ + if (_gasolineTanker!=null) + _gasolineTanker.MoveTransport(Direction.Up); + else + return; + } + public void DownButtonAction(){ + if (_gasolineTanker!=null) + _gasolineTanker.MoveTransport(Direction.Down); + else + return; + } + public void RightButtonAction(){ + if (_gasolineTanker!=null) + _gasolineTanker.MoveTransport(Direction.Right); + else + return; + } + public void LeftButtonAction(){ + if (_gasolineTanker!=null) + _gasolineTanker.MoveTransport(Direction.Left); + else + return; + } + public void CreateButtonAction(){ + Random rnd=new Random(); + _gasolineTanker=new DrawingGasolineTanker(); + _gasolineTanker.Init(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256))); + _gasolineTanker.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight()); + field.SpeedLabel.setText("Speed: "+_gasolineTanker.getGasolineTanker().getSpeed()); + field.WeightLabel.setText("Weight: "+_gasolineTanker.getGasolineTanker().getWeight()); + field.BodyColorLabel.setText("Color: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2)); + } + public void ResizeField(){ + if (_gasolineTanker!=null) + _gasolineTanker.ChangeBorders(getWidth(),getHeight()); + else return; + } +} diff --git a/src/DrawingGasolineTanker.java b/src/DrawingGasolineTanker.java new file mode 100644 index 0000000..8320d93 --- /dev/null +++ b/src/DrawingGasolineTanker.java @@ -0,0 +1,129 @@ +import java.awt.*; + +public class DrawingGasolineTanker { + public EntityGasolineTanker GasolineTanker; + public EntityGasolineTanker getGasolineTanker() { + return GasolineTanker; + } + public void setGasolineTanker(EntityGasolineTanker GasolineTanker) { + this.GasolineTanker = GasolineTanker; + } + + public DrawingWheels Wheels; + private int _startPosX; + private int _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _gasolineTankerWidth = 160; + private final int _gasolineTankerHeight = 55; + + public void Init(int speed, float weight, Color bodyColor) + { + GasolineTanker = new EntityGasolineTanker(); + GasolineTanker.Init(speed, weight, bodyColor); + Wheels = new DrawingWheels(); + Wheels.SetCountWheels((int)(2 + Math.random() + Math.random()*2)); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (x >= 0 && x+_gasolineTankerWidth <= width && y >= 0 && y+_gasolineTankerHeight <= 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 + _gasolineTankerWidth + GasolineTanker.Step < _pictureWidth) + { + _startPosX+=GasolineTanker.Step; + } + break; + + case Left: + if (_startPosX- GasolineTanker.Step >= 0) + { + _startPosX -= GasolineTanker.Step; + } + break; + + case Up: + if (_startPosY - GasolineTanker.Step >= 0) + { + _startPosY -= GasolineTanker.Step; + } + break; + + case Down: + if (_startPosY + _gasolineTankerHeight + GasolineTanker.Step < _pictureHeight) + { + _startPosY += GasolineTanker.Step; + } + break; + } + } + public void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null) + { + return; + } + + Graphics2D g2 = (Graphics2D) g; + + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX + 130, _startPosY + 35, 20, 20); + g2.fillOval(_startPosX + 130, _startPosY + 35, 20, 20); + Wheels.DrawWheels(g2, _startPosX, _startPosY); + + g2.setColor(Color.yellow); + g2.drawOval(_startPosX + 150, _startPosY + 30, 10, 10); + g2.fillOval(_startPosX + 150, _startPosY + 30, 10, 10); + + g2.setColor(Color.RED); + g2.drawOval(_startPosX+5, _startPosY + 35, 10, 10); + g2.fillOval(_startPosX+5, _startPosY + 35, 10, 10); + + g2.setColor(GasolineTanker.getBodyColor()); + g2.drawRect(_startPosX + 115, _startPosY+5, 40, 40); + g2.fillRect(_startPosX + 115, _startPosY+5, 40, 40); + g2.drawRect(_startPosX + 10, _startPosY + 35, 140, 10); + g2.fillRect(_startPosX + 10, _startPosY + 35, 140, 10); + + Color lightBlue = new Color(82, 186, 255); + g2.setColor(lightBlue); + g2.drawRect(_startPosX + 120, _startPosY + 10, 25, 25); + g2.fillRect(_startPosX + 120, _startPosY + 10, 25, 25); + } + public void ChangeBorders(int width,int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth<=_gasolineTankerWidth||_pictureHeight<=_gasolineTankerHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _gasolineTankerWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _gasolineTankerWidth; + } + if (_startPosY + _gasolineTankerHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _gasolineTankerHeight; + } + } +} diff --git a/src/DrawingWheels.java b/src/DrawingWheels.java new file mode 100644 index 0000000..53f93b4 --- /dev/null +++ b/src/DrawingWheels.java @@ -0,0 +1,41 @@ +import java.awt.*; + +public class DrawingWheels { + + private CountWheels _wheels; + + public void SetCountWheels(int Count){ + for (CountWheels temp: CountWheels.values()) + if (temp.getCountWheels() == Count){ + _wheels=temp; + return; + } + } + + public void DrawWheels(Graphics2D g,int _startPosX, int _startPosY) { + switch (_wheels.getCountWheels()) + { + case 2: + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 10, _startPosY + 35, 20, 20); + g.fillOval(_startPosX + 10, _startPosY + 35, 20, 20); + break; + case 3: + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 10, _startPosY + 35, 20, 20); + g.fillOval(_startPosX + 10, _startPosY + 35, 20, 20); + g.drawOval(_startPosX + 30, _startPosY + 35, 20, 20); + g.fillOval(_startPosX + 30, _startPosY + 35, 20, 20); + break; + case 4: + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 10, _startPosY + 35, 20, 20); + g.fillOval(_startPosX + 10, _startPosY + 35, 20, 20); + g.drawOval(_startPosX + 30, _startPosY + 35, 20, 20); + g.fillOval(_startPosX + 30, _startPosY + 35, 20, 20); + g.drawOval(_startPosX + 50, _startPosY + 35, 20, 20); + g.fillOval(_startPosX + 50, _startPosY + 35, 20, 20); + break; + } + } +} diff --git a/src/EntityGasolineTanker.java b/src/EntityGasolineTanker.java new file mode 100644 index 0000000..9d9221f --- /dev/null +++ b/src/EntityGasolineTanker.java @@ -0,0 +1,41 @@ +import java.awt.*; +import java.util.Random; + +public class EntityGasolineTanker { + + private int Speed; + public int getSpeed() { + return Speed; + } + public void setSpeed(int Speed) { + this.Speed = Speed; + } + + private float Weight; + public float getWeight() { + return Weight; + } + public void setWeight(float Weight) { + this.Weight = Weight; + } + + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + public void setBodyColor(Color BodyColor) { + this.BodyColor = BodyColor; + } + + public float Step; + + public void Init(int speed, float weight, Color bodyColor){ + + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50)+10 : speed; + Weight = weight <= 0 ? rnd.nextInt(100)+500 : weight; + BodyColor = bodyColor; + Step = Speed * 100/ (int)Weight; + } +} + diff --git a/src/FormGasolineTanker.java b/src/FormGasolineTanker.java new file mode 100644 index 0000000..d7dec78 --- /dev/null +++ b/src/FormGasolineTanker.java @@ -0,0 +1,137 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +public class FormGasolineTanker 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("Speed: "); + JLabel WeightLabel = new JLabel("Weight: "); + JLabel BodyColorLabel = new JLabel("Color: "); + + DrawingField field = new DrawingField(this); + + JButton ButtonCreate=new JButton("Create"); + + Icon iconUp = new ImageIcon("C:\\Users\\Максим\\Desktop\\Универ\\3 семестр\\РПП\\Материал\\KeyUp.png"); + JButton ButtonUp=new JButton(iconUp); + + Icon iconDown = new ImageIcon("C:\\Users\\Максим\\Desktop\\Универ\\3 семестр\\РПП\\Материал\\KeyDown.png"); + JButton ButtonDown=new JButton(iconDown); + + Icon iconRight = new ImageIcon("C:\\Users\\Максим\\Desktop\\Универ\\3 семестр\\РПП\\Материал\\KeyRight.png"); + JButton ButtonRight=new JButton(iconRight); + + Icon iconLeft = new ImageIcon("C:\\Users\\Максим\\Desktop\\Универ\\3 семестр\\РПП\\Материал\\KeyLeft.png"); + JButton ButtonLeft=new JButton(iconLeft); + + + public FormGasolineTanker(){ + super("Gasoline Tanker"); + setSize(800,600); + 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 db0fcda..1528646 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args){ - + new FormGasolineTanker(); } }