diff --git a/App.java b/App.java new file mode 100644 index 0000000..47fee84 --- /dev/null +++ b/App.java @@ -0,0 +1,8 @@ +import Form.FormAirFighter; + +public class App +{ + public static void main(String[] args) { + new FormAirFighter(); + } +} diff --git a/Classes/Direction.java b/Classes/Direction.java new file mode 100644 index 0000000..9eea621 --- /dev/null +++ b/Classes/Direction.java @@ -0,0 +1,11 @@ +package Classes; + +public enum Direction +{ + Up, + Down, + Left, + Right, +} + + diff --git a/Classes/DrawingAircraft.java b/Classes/DrawingAircraft.java new file mode 100644 index 0000000..d59be7b --- /dev/null +++ b/Classes/DrawingAircraft.java @@ -0,0 +1,225 @@ +package Classes; + +import java.awt.*; +import java.util.Random; + + +public class DrawingAircraft +{ + public EntityAircraft Plane; + private DrawingEngines _engines; + + private int _startPosX; + private int _startPosY; + + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + + protected final int _aircraftWidth = 100; + protected final int _aircraftHeight = 120; + + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Plane = new EntityAircraft(); + Plane.Init(speed,weight,bodyColor); + _engines = new DrawingEngines(); + _engines.setEngines(rnd.nextInt(2,7)); + } + + public void SetPosition(int x,int y,int width,int height) + { + if (x + _aircraftWidth > width || x < 0 || y + _aircraftHeight > height || y < 0) + { + return; + } + else + { + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + + switch (direction) + { + case Right: + { + if (_startPosX + _aircraftWidth + Plane.getStep() < _pictureWidth) + { + _startPosX += Plane.getStep(); + } + } + break; + case Left: + { + if (_startPosX - Plane.getStep() > 0) + { + _startPosX -= Plane.getStep(); + } + } + break; + case Up: + { + if (_startPosY - Plane.getStep() > 0) + { + _startPosY -= Plane.getStep(); + } + } + break; + case Down: + { + if (_startPosY + _aircraftHeight + Plane.getStep() < _pictureHeight) + { + _startPosY += Plane.getStep(); + } + } + break; + } + } + + public void DrawTransport(Graphics g) + { + + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) + { + return; + } + + //Aircraft Body + g.setColor(Plane.BodyColor); + g.fillRect((int)(_startPosX + 20), (int)(_startPosY + 45), 80, 14); + + g.setColor(Color.BLACK); + g.drawRect((int)(_startPosX + 20), (int)(_startPosY + 45), 80, 14); + + //Wings + Polygon pathWing1 = new Polygon(); + + Point point1B = new Point((int)(_startPosX + 50), (int)(_startPosY + 45)); + Point point2B = new Point(point1B.x, point1B.y - 40); + Point point3B = new Point(point2B.x + 6, point2B.y); + Point point4B = new Point(point3B.x + 27, point1B.y); + + pathWing1.addPoint(point1B.x,point1B.y); + pathWing1.addPoint(point2B.x,point2B.y); + pathWing1.addPoint(point3B.x,point3B.y); + pathWing1.addPoint(point4B.x,point4B.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathWing1); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathWing1); + + Polygon pathWing2 = new Polygon(); + + Point point1B2 = new Point((int)(_startPosX + 50), (int)(_startPosY + 60)); + Point point2B2 = new Point(point1B2.x, point1B2.y + 40); + Point point3B2 = new Point(point2B2.x + 6, point2B2.y); + Point point4B2 = new Point(point3B2.x + 27, point1B2.y); + + pathWing2.addPoint(point1B2.x,point1B2.y); + pathWing2.addPoint(point2B2.x,point2B2.y); + pathWing2.addPoint(point3B2.x,point3B2.y); + pathWing2.addPoint(point4B2.x,point4B2.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathWing2); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathWing2); + + + //BackWings + Polygon pathBackWing1 = new Polygon(); + + Point point1W = new Point((int)(_startPosX + 85), (int)(_startPosY + 45)); + Point point2W = new Point(point1W.x, point1W.y - 7); + Point point3W = new Point(point2W.x + 15, point2W.y - 16); + Point point4W = new Point(point3W.x, point1W.y); + + + pathBackWing1.addPoint(point1W.x, point1W.y); + pathBackWing1.addPoint(point2W.x, point2W.y); + pathBackWing1.addPoint(point3W.x, point3W.y); + pathBackWing1.addPoint(point4W.x, point4W.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathBackWing1); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathBackWing1); + + Polygon pathBackWing2 = new Polygon(); + + Point point1W2 = new Point((int)(_startPosX + 85), (int)(_startPosY + 60)); + Point point2W2 = new Point(point1W2.x, point1W2.y + 7); + Point point3W2 = new Point(point2W2.x + 15, point2W2.y +16); + Point point4W2 = new Point(point3W2.x, point1W2.y); + + pathBackWing2.addPoint(point1W2.x, point1W2.y); + pathBackWing2.addPoint(point2W2.x, point2W2.y); + pathBackWing2.addPoint(point3W2.x, point3W2.y); + pathBackWing2.addPoint(point4W2.x, point4W2.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathBackWing2); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathBackWing2); + + //Nose + Polygon pathBoseBody = new Polygon(); + + Point point1N = new Point((int)(_startPosX + 20), (int)(_startPosY + 45)); + Point point2N = new Point(point1N.x - 15 ,point1N.y + 7); + Point point3N = new Point(point1N.x, point1N.y + 15); + + pathBoseBody.addPoint(point1N.x, point1N.y); + pathBoseBody.addPoint(point2N.x, point2N.y); + pathBoseBody.addPoint(point3N.x, point3N.y); + + + g.setColor(Color.BLACK); + g.drawPolygon(pathBoseBody); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathBoseBody); + + //Engines + _engines.drawEngines(g,_startPosX,_startPosY, Plane.BodyColor); + + + + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + + if (_pictureWidth <= _aircraftWidth || _pictureHeight <= _aircraftHeight) + { + _pictureHeight = null; + _pictureWidth = null; + return; + } + if (_startPosX + _aircraftWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _aircraftWidth; + } + if (_startPosY + _aircraftHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _aircraftHeight; + } + } +} diff --git a/Classes/DrawingEngines.java b/Classes/DrawingEngines.java new file mode 100644 index 0000000..30d0314 --- /dev/null +++ b/Classes/DrawingEngines.java @@ -0,0 +1,69 @@ +package Classes; + + +import java.awt.*; + +public class DrawingEngines +{ + private Engines enginesCount; + + + public void setEngines(int count) + { + enginesCount = Engines.getEnginesEnum(count); + } + + public void drawEngines(Graphics g,int startPosX,int startPosY,Color bodyColor) + { + if(enginesCount != null) + { + switch(enginesCount) + { + case TwoEngines -> { + g.setColor((Color.BLACK)); + g.drawOval(startPosX + 40,startPosY + 5,30,10); + g.drawOval(startPosX + 40,startPosY + 90,30,10); + g.setColor((bodyColor)); + g.fillOval(startPosX + 40,startPosY + 5,30,10); + g.fillOval(startPosX + 40,startPosY + 90,30,10); + } + + case FourEngines -> { + + g.setColor((Color.BLACK)); + g.drawOval(startPosX + 40,startPosY + 5,30,10); + g.drawOval(startPosX + 40,startPosY + 90,30,10); + g.drawOval(startPosX + 40,startPosY + 20,30,10); + g.drawOval(startPosX + 40,startPosY + 75,30,10); + g.setColor(bodyColor); + g.fillOval(startPosX + 40,startPosY + 5,30,10); + g.fillOval(startPosX + 40,startPosY + 90,30,10); + g.fillOval(startPosX + 40,startPosY + 20,30,10); + g.fillOval(startPosX + 40,startPosY + 75,30,10); + } + + case SixEngines -> { + + g.setColor((Color.BLACK)); + g.drawOval(startPosX + 40,startPosY + 5,30,10); + g.drawOval(startPosX + 40,startPosY + 90,30,10); + g.drawOval(startPosX + 40,startPosY + 20,30,10); + g.drawOval(startPosX + 40,startPosY + 75,30,10); + g.drawOval(startPosX + 40,startPosY + 35,30,10); + g.drawOval(startPosX + 40,startPosY + 60,30,10); + g.setColor(bodyColor); + g.fillOval(startPosX + 40,startPosY + 5,30,10); + g.fillOval(startPosX + 40,startPosY + 90,30,10); + g.fillOval(startPosX + 40,startPosY + 20,30,10); + g.fillOval(startPosX + 40,startPosY + 75,30,10); + g.fillOval(startPosX + 40,startPosY + 35,30,10); + g.fillOval(startPosX + 40,startPosY + 60,30,10); + } + } + } + + + + } + +} diff --git a/Classes/Engines.java b/Classes/Engines.java new file mode 100644 index 0000000..d99b67f --- /dev/null +++ b/Classes/Engines.java @@ -0,0 +1,32 @@ +package Classes; + +public enum Engines +{ + TwoEngines(2), + FourEngines(4), + SixEngines(6); + private final int value; + Engines(int value) + { + this.value = value; + } + + public int getEngines() + { + return value; + } + + public static Engines getEnginesEnum(int count) + { + for(Engines eng : Engines.values()) + { + if(eng.getEngines() == count) + { + return eng; + } + } + return null; + } + + +} diff --git a/Classes/EntityAircraft.java b/Classes/EntityAircraft.java new file mode 100644 index 0000000..6f46e84 --- /dev/null +++ b/Classes/EntityAircraft.java @@ -0,0 +1,43 @@ +package Classes; + +import java.awt.*; + +public class EntityAircraft +{ + public int Speed; + public float Weight; + public Color BodyColor; + public float Step; + + + public void Init(int speed,float weight,Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = speed * 100 / weight; + + } + + public int getSpeed() + { + return Speed; + } + + public float getStep() + { + return Step; + } + + public float getWeight() + { + return Weight; + } + + public Color getBodyColor() + { + return BodyColor; + } + + +} \ No newline at end of file diff --git a/Form/FormAirFighter.form b/Form/FormAirFighter.form new file mode 100644 index 0000000..cac5761 --- /dev/null +++ b/Form/FormAirFighter.form @@ -0,0 +1,129 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Form/FormAirFighter.java b/Form/FormAirFighter.java new file mode 100644 index 0000000..5df819a --- /dev/null +++ b/Form/FormAirFighter.java @@ -0,0 +1,109 @@ +package Form; +import Classes.Direction; +import Classes.DrawingAircraft; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormAirFighter extends JFrame +{ + private DrawingAircraft _aircraft; + private JPanel Form; + private JPanel label; + private JPanel pictureBox; + private JTextArea speedLabel; + private JTextArea weightLabel; + private JTextArea colorLabel; + + private JButton moveRight; + private JButton moveDown; + private JButton moveLeft; + private JButton moveUp; + private JButton btnCreate; + + private void moveButtonClick(ActionEvent event) { + if (_aircraft == null) return; + String name = ((JButton)event.getSource()).getName(); + switch (name) { + case "left" -> { + _aircraft.MoveTransport(Direction.Left); + } + case "right" -> { + _aircraft.MoveTransport(Direction.Right); + } + case "up" -> { + _aircraft.MoveTransport(Direction.Up); + } + case "down" -> { + _aircraft.MoveTransport(Direction.Down); + } + } + Draw(); + } + + private void CreateEntity() { + Random random = new Random(); + _aircraft = new DrawingAircraft(); + _aircraft.Init( + random.nextInt(10, 300), + random.nextFloat(1000, 2000), + new Color( + random.nextInt(0,256), + random.nextInt(0,256), + random.nextInt(0,256) + ) + ); + _aircraft.SetPosition( + random.nextInt(0, 100), + random.nextInt(0, 100), + Form.getWidth(), + Form.getHeight() + ); + speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed()); + weightLabel.setText("Weight: " + _aircraft.Plane.getWeight()); + colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor()); + + Draw(); + } + + private void resizeWindow() { + _aircraft.ChangeBorders(pictureBox.getWidth(), pictureBox.getHeight()); + Draw(); + } + + private void Draw() { + Graphics2D graphics = (Graphics2D) pictureBox.getGraphics(); + graphics.clearRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight()); + pictureBox.paintComponents(graphics); + _aircraft.DrawTransport(graphics); + } + + public FormAirFighter() { + super("AirFighter"); + setContentPane(Form); + setSize(new Dimension(500, 500)); + + // action move // + ActionListener listener = this::moveButtonClick; + moveRight.addActionListener(listener); + moveDown.addActionListener(listener); + moveLeft.addActionListener(listener); + moveUp.addActionListener(listener); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + btnCreate.addActionListener(e -> CreateEntity()); + Form.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + resizeWindow(); + } + }); + } +} diff --git a/Resources/bottomarrow.png b/Resources/bottomarrow.png new file mode 100644 index 0000000..9ac02da Binary files /dev/null and b/Resources/bottomarrow.png differ diff --git a/Resources/leftarrow.png b/Resources/leftarrow.png new file mode 100644 index 0000000..b06055b Binary files /dev/null and b/Resources/leftarrow.png differ diff --git a/Resources/rightarrow.png b/Resources/rightarrow.png new file mode 100644 index 0000000..fcac24b Binary files /dev/null and b/Resources/rightarrow.png differ diff --git a/Resources/upbutton.png b/Resources/upbutton.png new file mode 100644 index 0000000..3672f56 Binary files /dev/null and b/Resources/upbutton.png differ