First commit
This commit is contained in:
parent
e529b98d7b
commit
d36395cda6
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
11
.idea/Airbus.iml
generated
Normal file
11
.idea/Airbus.iml
generated
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Airbus.iml" filepath="$PROJECT_DIR$/.idea/Airbus.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
4
Direction.java
Normal file
4
Direction.java
Normal file
@ -0,0 +1,4 @@
|
||||
enum Direction
|
||||
{
|
||||
Right, Left, Up, Down
|
||||
}
|
165
DrawingPlane.java
Normal file
165
DrawingPlane.java
Normal file
@ -0,0 +1,165 @@
|
||||
import java.awt.*;
|
||||
public class DrawingPlane {
|
||||
public EntityPlane Plane;
|
||||
|
||||
public EntityPlane getPlane()
|
||||
{
|
||||
return Plane;
|
||||
}
|
||||
private float _startPosX;
|
||||
private float _startPosY;
|
||||
private Integer _pictureWidth = null;
|
||||
private Integer _pictureHeight = null;
|
||||
private final int _planeWidth = 125;
|
||||
private final int _planeHeight = 45;
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor, int ilNum, EntityPlane entity)
|
||||
{
|
||||
Plane = new EntityPlane();
|
||||
Plane.Init(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if (x < 0 || x + _planeWidth >= width)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (y < 0 || y + _planeHeight >= height)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_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 + _planeWidth + Plane.Step() < _pictureWidth)
|
||||
{
|
||||
_startPosX += Plane.Step();
|
||||
}
|
||||
break;
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - Plane.Step() > 0)
|
||||
{
|
||||
_startPosX -= Plane.Step();
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - Plane.Step() > 0)
|
||||
{
|
||||
_startPosY -= Plane.Step();
|
||||
}
|
||||
break;
|
||||
|
||||
//вниз
|
||||
case Down:
|
||||
if (_startPosY + _planeHeight + Plane.Step() < _pictureHeight)
|
||||
{
|
||||
_startPosY += Plane.Step();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
|
||||
if (_startPosX < 0 || _startPosY < 0
|
||||
|| _pictureHeight == null|| _pictureWidth == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval((int)_startPosX, (int)_startPosY + 20, 20, 20);
|
||||
|
||||
g.drawRect((int)_startPosX + 8, (int)_startPosY + 20, 100, 20);
|
||||
|
||||
g.drawPolygon
|
||||
(
|
||||
new int[] {(int)_startPosX + 108, (int)_startPosX + 125, (int)_startPosX + 108},
|
||||
new int[] {(int)_startPosY + 18, (int)_startPosY + 30, (int)_startPosY + 30}, 3
|
||||
);
|
||||
g.drawPolygon
|
||||
(
|
||||
new int[] {(int)_startPosX + 108, (int)_startPosX + 125, (int)_startPosX + 108},
|
||||
new int[] {(int)_startPosY + 30, (int)_startPosY + 30, (int)_startPosY + 42}, 3
|
||||
);
|
||||
g.drawPolygon
|
||||
(
|
||||
new int[] {(int)_startPosX + 5, (int)_startPosX + 5, (int)_startPosX + 35},
|
||||
new int[] {(int)_startPosY + 20, (int)_startPosY, (int)_startPosY + 20}, 3
|
||||
);
|
||||
|
||||
// корпус
|
||||
|
||||
g.setColor(Plane.getBodyColor());
|
||||
g.fillOval((int)_startPosX, (int)_startPosY + 20, 20, 20);
|
||||
|
||||
g.fillRect((int)_startPosX + 8, (int)_startPosY + 20, 100, 20);
|
||||
|
||||
g.fillPolygon
|
||||
(
|
||||
new int[] {(int)_startPosX + 108, (int)_startPosX + 125, (int)_startPosX + 108},
|
||||
new int[] {(int)_startPosY + 30, (int)_startPosY + 30, (int)_startPosY + 42}, 3
|
||||
);
|
||||
g.fillPolygon
|
||||
(
|
||||
new int[] {(int)_startPosX + 5, (int)_startPosX + 5, (int)_startPosX + 35},
|
||||
new int[] {(int)_startPosY + 20, (int)_startPosY, (int)_startPosY + 20}, 3
|
||||
);
|
||||
g.setColor(Color.blue);
|
||||
g.fillPolygon
|
||||
(
|
||||
new int[] {(int)_startPosX + 108, (int)_startPosX + 125, (int)_startPosX + 108},
|
||||
new int[] {(int)_startPosY + 18, (int)_startPosY + 30, (int)_startPosY + 30}, 3
|
||||
);
|
||||
g.setColor(Color.black);
|
||||
g.drawLine((int)_startPosX + 37, (int)_startPosY + 40, (int)_startPosX + 37, (int)_startPosY + 45);
|
||||
g.drawLine((int)_startPosX + 32, (int)_startPosY + 45, (int)_startPosX + 40, (int)_startPosY + 45);
|
||||
g.fillRect((int)_startPosX + 32, (int)_startPosY + 45, 3, 3);
|
||||
g.fillRect((int)_startPosX + 39, (int)_startPosY + 45, 3, 3);
|
||||
|
||||
g.drawLine((int)_startPosX + 102, (int)_startPosY + 40, (int)_startPosX + 102, (int)_startPosY + 45);
|
||||
g.fillRect((int)_startPosX + 101, (int)_startPosY + 45, 3, 3);
|
||||
|
||||
g.fillRect((int)_startPosX + 5, (int)_startPosY + 18, 18, 7);
|
||||
g.fillOval((int)_startPosX, (int)_startPosY + 18, 7, 7);
|
||||
g.fillOval((int)_startPosX + 20, (int)_startPosY + 18, 7, 7);
|
||||
|
||||
g.fillRect((int)_startPosX + 41, (int)_startPosY + 28, 42, 4);
|
||||
g.fillOval((int)_startPosX + 39, (int)_startPosY + 28, 4, 4);
|
||||
g.fillOval((int)_startPosX + 81, (int)_startPosY + 28, 4, 4);
|
||||
}
|
||||
|
||||
public void ChangeBorders(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureWidth <= _planeWidth || _pictureHeight <= _planeHeight)
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
return;
|
||||
}
|
||||
if (_startPosX + _planeWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _planeWidth;
|
||||
}
|
||||
if (_startPosY + _planeHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _planeHeight;
|
||||
}
|
||||
}
|
||||
}
|
22
EntityPlane.java
Normal file
22
EntityPlane.java
Normal file
@ -0,0 +1,22 @@
|
||||
import java.awt.Color;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityPlane {
|
||||
private int Speed;
|
||||
private float Weight;
|
||||
private Color BodyColor;
|
||||
|
||||
public int getSpeed(){return Speed;}
|
||||
public float getWeight(){return Weight;}
|
||||
public Color getBodyColor(){return BodyColor;}
|
||||
|
||||
public float Step(){return Speed * 100 / Weight;}
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Random rand = new Random();
|
||||
if (speed <= 0) speed = rand.nextInt(350, 550);
|
||||
if (weight <= 0) weight = rand.nextFloat(350, 550);
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
106
FormPlane.java
Normal file
106
FormPlane.java
Normal file
@ -0,0 +1,106 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormPlane extends JComponent
|
||||
{
|
||||
private DrawingPlane _plane;
|
||||
private EntityPlane _entity;
|
||||
|
||||
public FormPlane()
|
||||
{
|
||||
JFrame formFrame = new JFrame("Plane");
|
||||
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
formFrame.setSize(840, 560);
|
||||
formFrame.setVisible(true);
|
||||
formFrame.setLocationRelativeTo(null);
|
||||
|
||||
formFrame.addComponentListener(new ComponentListener()
|
||||
{
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e)
|
||||
{
|
||||
if (_plane != null) _plane.ChangeBorders(formFrame.getWidth(), formFrame.getHeight());
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentMoved(ComponentEvent e) {}
|
||||
|
||||
@Override
|
||||
public void componentShown(ComponentEvent e) {}
|
||||
|
||||
@Override
|
||||
public void componentHidden(ComponentEvent e) {}
|
||||
|
||||
});
|
||||
Panel statusPanel = new Panel();
|
||||
statusPanel.setBackground(Color.WHITE);
|
||||
statusPanel.setLayout(new GridLayout());
|
||||
setLayout(new BorderLayout());
|
||||
add(statusPanel, BorderLayout.SOUTH);
|
||||
|
||||
Label speedLabel = new Label("Speed:");
|
||||
Label weightLabel = new Label("Weight:");
|
||||
Label colorLabel = new Label("Color:");
|
||||
|
||||
JButton createButton = new JButton("Create");
|
||||
createButton.addActionListener(e -> {
|
||||
Random rand = new Random();
|
||||
_plane = new DrawingPlane();
|
||||
_entity = new EntityPlane();
|
||||
_plane.Init(300 + rand.nextInt( 100), 1000 + rand.nextInt(1000), Color.getHSBColor(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextInt(3), _entity);
|
||||
_plane.SetPosition(10 + rand.nextInt(90), 10 + rand.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
|
||||
speedLabel.setText("Speed: " + _plane.Plane.getSpeed());
|
||||
weightLabel.setText("Weight: " + (int)_plane.Plane.getWeight());
|
||||
colorLabel.setText("Color: " + _plane.Plane.getBodyColor().getRed() + " " + _plane.Plane.getBodyColor().getGreen() + " " + _plane.Plane.getBodyColor().getBlue() );
|
||||
repaint();
|
||||
});
|
||||
|
||||
statusPanel.add(createButton);
|
||||
statusPanel.add(speedLabel);
|
||||
statusPanel.add(weightLabel);
|
||||
statusPanel.add(colorLabel);
|
||||
|
||||
JButton moveRightButton = new JButton(">");
|
||||
moveRightButton.addActionListener(e -> {
|
||||
if (_plane != null) _plane.MoveTransport(Direction.Right);
|
||||
repaint();
|
||||
});
|
||||
JButton moveLeftButton = new JButton("<");
|
||||
moveLeftButton.addActionListener(e -> {
|
||||
if (_plane != null) _plane.MoveTransport(Direction.Left);
|
||||
repaint();
|
||||
});
|
||||
JButton moveUpButton = new JButton("^");
|
||||
moveUpButton.addActionListener(e -> {
|
||||
if (_plane != null) _plane.MoveTransport(Direction.Up);
|
||||
repaint();
|
||||
});
|
||||
JButton moveDownButton = new JButton("v");
|
||||
moveDownButton.addActionListener(e -> {
|
||||
if (_plane != null) _plane.MoveTransport(Direction.Down);
|
||||
repaint();
|
||||
});
|
||||
|
||||
statusPanel.add(moveUpButton);
|
||||
statusPanel.add(moveDownButton);
|
||||
statusPanel.add(moveLeftButton);
|
||||
statusPanel.add(moveRightButton);
|
||||
|
||||
formFrame.getContentPane().add(this);
|
||||
}
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
if (_plane != null) _plane.DrawTransport(g2);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new FormPlane();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user