Morozov V.S. LabWork01_Hard #1

Closed
VoldemarProger wants to merge 3 commits from LabWork01 into main
7 changed files with 381 additions and 1 deletions
Showing only changes of commit 7042f498fb - Show all commits

View File

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

View File

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

View File

@ -0,0 +1,127 @@
import java.awt.*;
class DrawingEntityPlain
{
public EntityAirPlane Airplane;
private float _startPosX;
private float _startPosY;
private int _pictureWidth = -1;
private int _pictureHeight = -1;
private int _AirplaneWidth = 120;
private int _AirplaneHeight = 60;
public void Init(int speed, float weight, Color bodyColor)
{
Airplane = new EntityAirPlane();
Airplane.Init(speed, weight, bodyColor);
}
public void SetPosition(int x, int y, int width, int height)
{
if (width < _AirplaneWidth || height < _AirplaneHeight) return;
if (x + _AirplaneWidth > width || x < 0) return;
if (y + _AirplaneHeight > 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 + _AirplaneWidth + Airplane.Step < _pictureWidth)
{
_startPosX += Airplane.Step;
}
break;
case Left:
if (_startPosX - Airplane.Step > 0)
{
_startPosX -= Airplane.Step;
}
break;
case Up:
if (_startPosY - Airplane.Step > 0)
{
_startPosY -= Airplane.Step;
}
break;
case Down:
if (_startPosY + _AirplaneHeight + Airplane.Step < _pictureHeight)
{
_startPosY += Airplane.Step;
}
break;
}
}
public void DrawTransport(Graphics2D g)
{
if (_pictureWidth == -1 || _pictureHeight == -1)
{
return;
}
g.drawRect( (int)_startPosX, (int)_startPosY, 20, 30);
g.drawRect( (int)_startPosX, (int)_startPosY + 30, 100, 30);
g.drawRect( (int)_startPosX+100, (int)_startPosY + 40, 20, 15);
//koleso1
g.drawRect( (int)_startPosX + 30, (int)_startPosY + 60, 5, 10);
g.drawOval( (int)_startPosX+28,(int) _startPosY+70, 9, 9);
//koleso2
g.drawRect( (int)_startPosX + 80, (int)_startPosY + 60, 5, 10);
g.drawOval( (int)_startPosX + 78, (int)_startPosY + 70, 9, 9);
//Korpys
g.setPaint(Airplane.BodyColor);
g.fillRect((int)_startPosX+3, (int)_startPosY + 33, 94, 24);
g.fillRect( (int)_startPosX+1, (int)_startPosY+1, 19, 29);
//krilya
g.setPaint(Color.black);
g.fillRect((int)_startPosX + 30, (int)_startPosY + 40, 40, 8);
//cabina
g.setPaint(Color.blue);
g.fillRect( (int)_startPosX + 101, (int)_startPosY + 41, 19, 14);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _AirplaneWidth || _pictureHeight <= _AirplaneHeight)
{
_pictureWidth = -1;
_pictureHeight = -1;
return;
}
if (_startPosX + _AirplaneWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _AirplaneWidth;
}
if (_startPosY + _AirplaneHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _AirplaneHeight;
}
}
}

View File

@ -0,0 +1,22 @@
import java.awt.*;
import java.util.Random;
class EntityAirPlane
{
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;
}
}

View File

@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormAirPlane">
<grid id="27dc6" binding="mainPanel" layout-manager="GridBagLayout">
<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"/>
<gridbag weightx="2.0" weighty="2.0"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="df504" class="javax.swing.JButton" binding="createButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="создание"/>
</properties>
</component>
<grid id="e091d" layout-manager="GridBagLayout">
<constraints>
<grid row="1" column="3" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="2.0" weighty="0.0"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="323b6">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
</hspacer>
<vspacer id="cc4bd">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
</vspacer>
<component id="5bf4f" class="javax.swing.JButton" binding="upButton" default-binding="true">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="up"/>
</properties>
</component>
<component id="b31a8" class="javax.swing.JButton" binding="downButton" 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"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="down"/>
</properties>
</component>
<component id="1d949" class="javax.swing.JButton" binding="leftButton" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="left"/>
</properties>
</component>
<component id="527d2" class="javax.swing.JButton" binding="rightButton" 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"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="right"/>
</properties>
</component>
</children>
</grid>
<component id="fa09a" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="Скорость: "/>
</properties>
</component>
<component id="5d314" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="Вес: "/>
</properties>
</component>
<component id="9494b" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="2" column="5" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="Цвет"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,95 @@
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 FormAirPlane {
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;
DrawingEntityPlain _airPlane;
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();
_airPlane = new DrawingEntityPlain();
_airPlane.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
_airPlane.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height);
Color bodyColor = _airPlane.Airplane.BodyColor;
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
speedLabel.setText("Скорость: " + _airPlane.Airplane.Speed + " ");
weightLabel.setText("Вес: " + _airPlane.Airplane.Weight + " ");
colorLabel.setText("Цвет: " + colorString);
canv.repaint();
});
jframe.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if(_airPlane == null) return;
_airPlane.ChangeBorders(canv.getSize().width, canv.getSize().height);
jframe.revalidate();
}
});
upButton.addActionListener(e -> {
if(_airPlane == null) return;
_airPlane.MoveTransport(Direction.Up);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(_airPlane == null) return;
_airPlane.MoveTransport(Direction.Right);
canv.repaint();
});
downButton.addActionListener(e -> {
if(_airPlane == null) return;
_airPlane.MoveTransport(Direction.Down);
canv.repaint();
});
leftButton.addActionListener(e -> {
if(_airPlane == null) return;
_airPlane.MoveTransport(Direction.Left);
canv.repaint();
});
}
public void draw(Graphics2D g) {
if(_airPlane == null) return;
_airPlane.DrawTransport(g);
}
}

View File

@ -3,6 +3,6 @@ import java.awt.*;
public class Program {
public static void main(String[] args) {
System.out.println("Hello world");
new FormAirPlane().run();
}
}