Engines enum and draw engine class are added!

This commit is contained in:
Yuee Shiness 2022-10-24 19:41:28 +04:00
parent 42f4d2676f
commit daea9003f6
12 changed files with 626 additions and 0 deletions

8
App.java Normal file
View File

@ -0,0 +1,8 @@
import Form.FormAirFighter;
public class App
{
public static void main(String[] args) {
new FormAirFighter();
}
}

11
Classes/Direction.java Normal file
View File

@ -0,0 +1,11 @@
package Classes;
public enum Direction
{
Up,
Down,
Left,
Right,
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}
}
}

32
Classes/Engines.java Normal file
View File

@ -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;
}
}

View File

@ -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;
}
}

129
Form/FormAirFighter.form Normal file
View File

@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Form.FormAirFighter">
<grid id="27dc6" binding="Form" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<xy x="20" y="20" width="825" height="664"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="889df" binding="label" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="0">
<constraints border-constraint="South"/>
<properties/>
<border type="none"/>
<children>
<component id="e93c7" class="javax.swing.JTextArea" binding="speedLabel">
<constraints/>
<properties>
<editable value="false"/>
<margin top="0" left="0" bottom="0" right="10"/>
<text value="Speed:"/>
</properties>
</component>
<component id="1b5c5" class="javax.swing.JTextArea" binding="weightLabel">
<constraints/>
<properties>
<editable value="false"/>
<margin top="0" left="0" bottom="0" right="10"/>
<text value="Weight:"/>
</properties>
</component>
<component id="14465" class="javax.swing.JTextArea" binding="colorLabel">
<constraints/>
<properties>
<editable value="false"/>
<margin top="0" left="0" bottom="0" right="10"/>
<text value="BodyColor:"/>
</properties>
</component>
</children>
</grid>
<grid id="53a7f" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="Center"/>
<properties/>
<border type="none"/>
<children>
<component id="f5248" class="javax.swing.JButton" binding="moveRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalTextPosition value="0"/>
<icon value="Resources/rightarrow.png"/>
<name value="right"/>
<text value=""/>
</properties>
</component>
<hspacer id="69b03">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="2a7c5">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="71509" class="javax.swing.JButton" binding="moveDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalTextPosition value="0"/>
<icon value="Resources/bottomarrow.png"/>
<name value="down"/>
<text value=""/>
</properties>
</component>
<component id="fb1e5" class="javax.swing.JButton" binding="moveLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalTextPosition value="0"/>
<icon value="Resources/leftarrow.png"/>
<name value="left"/>
<text value=""/>
</properties>
</component>
<component id="cc503" class="javax.swing.JButton" binding="moveUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalTextPosition value="0"/>
<icon value="Resources/upbutton.png"/>
<name value="up"/>
<text value=""/>
</properties>
</component>
<component id="50728" class="javax.swing.JButton" binding="btnCreate">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Create"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

109
Form/FormAirFighter.java Normal file
View File

@ -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();
}
});
}
}

BIN
Resources/bottomarrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 489 B

BIN
Resources/leftarrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 B

BIN
Resources/rightarrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 493 B

BIN
Resources/upbutton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 489 B