Karamushko M.K Lab work 1 #1

Closed
maxKarme wants to merge 3 commits from lab1_hard into master
14 changed files with 472 additions and 0 deletions

15
Canvas.java Normal file
View File

@ -0,0 +1,15 @@
import javax.swing.*;
import java.awt.*;
public class Canvas extends JComponent {
Form form;
public Canvas(Form form) {
this.form = form;
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
form.Draw(g2);
}
}

6
Direction.java Normal file
View File

@ -0,0 +1,6 @@
public enum Direction {
Up,
Right,
Left,
Down
}

153
DrawingAircraft.java Normal file
View File

@ -0,0 +1,153 @@
import java.awt.*;
import java.util.Random;
class DrawingAircraft
{
public EntityAircraft AirFighter;
public DrawingEngines drawingEngines = new DrawingEngines();
private float _startPosX;
private float _startPosY;
private int _pictureWidth = -1;
private int _pictureHeight = -1;
private int _airFighterWidth = 195;
private int _airFighterHeight = 166;
public void Init(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
AirFighter = new EntityAircraft();
AirFighter.Init(speed, weight, bodyColor);
drawingEngines.Init(rnd.nextInt(1, 8), bodyColor);
}
public void SetPosition(int x, int y, int width, int height)
{
if (width < _airFighterWidth || height < _airFighterHeight) return;
if (x + _airFighterWidth > width || x < 0) return;
if (y + _airFighterHeight > height || y < 0) return;
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (_pictureWidth == -1 || _pictureHeight == -1)
{
return;
}
switch (direction)
{
case Right:
if (_startPosX + _airFighterWidth + AirFighter.Step < _pictureWidth)
{
_startPosX += AirFighter.Step;
}
break;
case Left:
if (_startPosX - AirFighter.Step > 0)
{
_startPosX -= AirFighter.Step;
}
break;
case Up:
if (_startPosY - AirFighter.Step > 0)
{
_startPosY -= AirFighter.Step;
}
break;
case Down:
if (_startPosY + _airFighterHeight + AirFighter.Step < _pictureHeight)
{
_startPosY += AirFighter.Step;
}
break;
}
}
public void DrawTransport(Graphics2D g)
{
if (_pictureWidth == -1 || _pictureHeight == -1)
{
return;
}
g.setPaint(AirFighter.BodyColor);
Polygon front = new Polygon();
front.addPoint((int)(_startPosX + 160), (int)(_startPosY + 70));
front.addPoint((int)(_startPosX + 195), (int)(_startPosY + 83));
front.addPoint((int)(_startPosX + 160), (int)(_startPosY + 96));
Polygon tailTop = new Polygon();
tailTop.addPoint((int)(_startPosX), (int)(_startPosY + 30));
tailTop.addPoint((int)(_startPosX), (int)(_startPosY + 70));
tailTop.addPoint((int)(_startPosX + 25), (int)(_startPosY + 70));
tailTop.addPoint((int)(_startPosX + 25), (int)(_startPosY + 55));
Polygon tailBottom = new Polygon();
tailBottom.addPoint((int)(_startPosX), (int)(_startPosY + 96));
tailBottom.addPoint((int)(_startPosX), (int)(_startPosY + 136));
tailBottom.addPoint((int)(_startPosX + 25), (int)(_startPosY + 111));
tailBottom.addPoint((int)(_startPosX + 25), (int)(_startPosY + 96));
Polygon wingTop = new Polygon();
wingTop.addPoint((int)(_startPosX + 100), (int)(_startPosY));
wingTop.addPoint((int)(_startPosX + 100), (int)(_startPosY + 70));
wingTop.addPoint((int)(_startPosX + 75), (int)(_startPosY + 70));
wingTop.addPoint((int)(_startPosX + 90), (int)(_startPosY));
Polygon wingBottom = new Polygon();
wingBottom.addPoint((int)(_startPosX + 100), (int)(_startPosY + 96));
wingBottom.addPoint((int)(_startPosX + 100), (int)(_startPosY + 166));
wingBottom.addPoint((int)(_startPosX + 90), (int)(_startPosY + 166));
wingBottom.addPoint((int)(_startPosX + 75), (int)(_startPosY + 96));
g.fillPolygon(front);
g.drawPolygon(tailTop);
g.drawPolygon(tailBottom);
g.drawPolygon(wingTop);
g.drawPolygon(wingBottom);
g.drawRect((int)_startPosX, (int)_startPosY + 70, 160, 26);
drawingEngines.draw(g, (int)_startPosX, (int)_startPosY);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _airFighterWidth || _pictureHeight <= _airFighterHeight)
{
_pictureWidth = -1;
_pictureHeight = -1;
return;
}
if (_startPosX + _airFighterWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _airFighterWidth;
}
if (_startPosY + _airFighterHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _airFighterHeight;
}
}
}

33
DrawingEngines.java Normal file
View File

@ -0,0 +1,33 @@
import java.awt.*;
public class DrawingEngines {
Review

Нет свойства, принимающее число на вход

Нет свойства, принимающее число на вход
private EnginesCount enginesCount;
private Color color;
public void Init(int count, Color bodyColor) {
setCount(count);
color = bodyColor;
}
public void setCount(int count) {
if(count <= 2) enginesCount = EnginesCount.Two;
else if(count >= 6) enginesCount = EnginesCount.Six;
else enginesCount = EnginesCount.Four;
}
public void draw(Graphics2D g, int startPosX, int startPosY) {
g.setPaint(color);
g.fillOval(startPosX + 90, startPosY + 10, 30, 15);
g.fillOval(startPosX + 90, startPosY + 141, 30, 15);
if(enginesCount == EnginesCount.Two) return;
g.fillOval(startPosX + 90, startPosY + 30, 30, 15);
g.fillOval(startPosX + 90, startPosY + 121, 30, 15);
if(enginesCount == EnginesCount.Four) return;
g.fillOval(startPosX + 90, startPosY + 50, 30, 15);
g.fillOval(startPosX + 90, startPosY + 101, 30, 15);
}
}

5
EnginesCount.java Normal file
View File

@ -0,0 +1,5 @@
public enum EnginesCount {
Two,
Four,
Six
}

22
EntityAircraft.java Normal file
View File

@ -0,0 +1,22 @@
import java.awt.*;
import java.util.Random;
class EntityAircraft
{
public int Speed;
public float Weight;
public Color BodyColor;
public float Step;
public void Init(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
Weight = weight <= 0 ? rnd.nextInt(50, 70) : weight;
BodyColor = bodyColor;
Step = Speed * 100 / Weight;
}
}

5
Form.java Normal file
View File

@ -0,0 +1,5 @@
import java.awt.*;
public interface Form {
void Draw(Graphics2D g);
}

132
FormAircraft.form Normal file
View File

@ -0,0 +1,132 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormAircraft">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="3" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="745" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="c3c1c" binding="DrawPlace" layout-manager="CardLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="7" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="7cdea" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="7" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="20"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5d314" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес: "/>
</properties>
</component>
<component id="fa09a" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость: "/>
</properties>
</component>
<component id="9494b" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет"/>
</properties>
</component>
</children>
</grid>
<grid id="b5cf" layout-manager="GridLayoutManager" row-count="2" 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>
<grid row="1" column="0" row-span="1" col-span="7" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="eb27d">
<constraints>
<grid row="1" 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>
<component id="df504" class="javax.swing.JButton" binding="createButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="создание"/>
</properties>
</component>
<component id="b31a8" class="javax.swing.JButton" binding="downButton" default-binding="true">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
<icon value="Resources/down.png"/>
<text value=""/>
</properties>
</component>
<component id="1d949" class="javax.swing.JButton" binding="leftButton" default-binding="true">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
<icon value="Resources/left.png"/>
<text value=""/>
</properties>
</component>
<component id="527d2" class="javax.swing.JButton" binding="rightButton" default-binding="true">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
<icon value="Resources/right.png"/>
<text value=""/>
</properties>
</component>
<component id="5bf4f" class="javax.swing.JButton" binding="upButton" default-binding="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
<icon value="Resources/up.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

96
FormAircraft.java Normal file
View File

@ -0,0 +1,96 @@
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormAircraft implements Form {
private JButton createButton;
private JButton upButton;
private JButton rightButton;
private JButton downButton;
private JButton leftButton;
private JPanel mainPanel;
private JPanel DrawPlace;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
DrawingAircraft _airFighter;
private JFrame jframe = getFrame();
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
public void run() {
jframe.add(mainPanel);
Canvas canv = new Canvas(this);
DrawPlace.add(canv);
createButton.addActionListener(e -> {
Dimension canvSize = canv.getSize();
Random rnd = new Random();
_airFighter = new DrawingAircraft();
_airFighter.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
_airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height);
Color bodyColor = _airFighter.AirFighter.BodyColor;
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
speedLabel.setText("Скорость: " + _airFighter.AirFighter.Speed + " ");
weightLabel.setText("Вес: " + _airFighter.AirFighter.Weight + " ");
colorLabel.setText("Цвет: " + colorString);
canv.repaint();
});
jframe.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if(_airFighter == null) return;
_airFighter.ChangeBorders(canv.getSize().width, canv.getSize().height);
jframe.revalidate();
}
});
upButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Up);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Right);
canv.repaint();
});
downButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Down);
canv.repaint();
});
leftButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Left);
canv.repaint();
});
}
@Override
public void Draw(Graphics2D g) {
if(_airFighter == null) return;
_airFighter.DrawTransport(g);
}
}

5
Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
new FormAircraft().run();
}
}

BIN
Resources/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 B

BIN
Resources/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 B

BIN
Resources/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 366 B

BIN
Resources/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 B