Emelyanov A.S. LabWork01 #1

Closed
Emelyanov535 wants to merge 1 commits from LabWork01 into master
17 changed files with 470 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

11
.idea/ProjectAirbus.iml Normal file
View 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 Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View 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/ProjectAirbus.iml" filepath="$PROJECT_DIR$/.idea/ProjectAirbus.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

7
Direction.java Normal file
View File

@ -0,0 +1,7 @@
public enum Direction {
Up(1),
Down(2),
Left(3),
Right(4);
Direction(int value){}
}

36
DrawningIlluminator.java Normal file
View File

@ -0,0 +1,36 @@
import javax.swing.*;
import java.awt.*;
public class DrawningIlluminator extends JComponent {
private IlluminatorCount _Illuminator;
public void SetIlluminatorCount(int numOfIllum) {
_Illuminator = IlluminatorCount.GetIlluminatorCount(numOfIllum);
}
public void DrawIlluminator(Graphics g, int _startPosX, int _startPosY) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
int numOfIlluminator = 0;
switch (_Illuminator)
{
case Ten:
numOfIlluminator = 10;
break;
case Twenty:
numOfIlluminator = 20;
break;
case Thirty:
numOfIlluminator = 30;
break;
}
for(int i = numOfIlluminator; i >= 1; --i){
g2d.setColor(Color.CYAN);
g2d.fillOval(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3);
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3);
}
}
}

145
DrawningPlane.java Normal file
View File

@ -0,0 +1,145 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.random.RandomGenerator;
public class DrawningPlane extends JPanel {
private EntityPlane Plane;
public EntityPlane GetPlane(){
return Plane;
}
private int _startPosX;
private int _startPosY;
public DrawningIlluminator IlluminatorDraw;
public Integer _pictureWidth = null;
public Integer _pictureHeight = null;
private final int _PlaneWidth = 130;
private final int _PlaneHeight = 70;
public void SetIlluminator() {
Random r = new Random();
int numIllum = r.nextInt(1,4);
numIllum = numIllum * 10;
IlluminatorDraw.SetIlluminatorCount(numIllum);
}
public void Init(int speed, float weight, Color bodyColor)
{
Plane = new EntityPlane();
Plane.Init(speed, weight, bodyColor);
IlluminatorDraw = new DrawningIlluminator();
SetIlluminator();
}
public void SetPosition(int x, int y, int width, int height)
{
if (x >= 0 && x + _PlaneWidth <= width && y >= 0 && y + _PlaneHeight <= height) {
_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() {
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
if (GetPlane() == null) {
return;
}
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX, _startPosY + 30, 20, 20);
g2d.drawRect(_startPosX + 10, _startPosY + 30, 100, 20);
g2d.drawLine(_startPosX + 110, _startPosY + 30, _startPosX + 130, _startPosY+40);
g2d.drawLine(_startPosX + 110, _startPosY+50, _startPosX + 130, _startPosY+40);
g2d.drawLine(_startPosX, _startPosY, _startPosX, _startPosY+40);
g2d.drawLine(_startPosX, _startPosY, _startPosX + 30, _startPosY+30);
g2d.drawLine(_startPosX + 40, _startPosY + 50, _startPosX + 40, _startPosY+55);
g2d.drawLine(_startPosX + 100, _startPosY + 50, _startPosX + 100, _startPosY+55);
g2d.drawOval(_startPosX + 95, _startPosY + 55, 10, 10);
g2d.drawOval(_startPosX + 29, _startPosY + 55, 10, 10);
g2d.drawOval(_startPosX + 41, _startPosY + 55, 10, 10);
g2d.setPaint(Plane.GetBodyColor());
g2d.fillOval(_startPosX, _startPosY + 31, 20, 19);
g2d.fillRect(_startPosX + 10, _startPosY + 31, 100, 19);
g2d.setPaint(Color.BLACK);
g2d.fillOval(_startPosX + 40, _startPosY + 40, 60, 5);
g2d.fillOval(_startPosX - 5, _startPosY + 25, 30, 10);
IlluminatorDraw.DrawIlluminator(g, _startPosX, _startPosY);
}
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;
}
}
}

29
EntityPlane.java Normal file
View File

@ -0,0 +1,29 @@
import java.awt.*;
import java.util.Random;
public class EntityPlane {
private int Speed;
public int GetSpeed() {
return Speed;
}
private float Weight;
public float GetWeight() {
return Weight;
}
private Color BodyColor;
public Color GetBodyColor() {
return 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(40,70) : weight;
BodyColor = bodyColor;
Step = Speed * 100 / (int)Weight;
}
}

103
FormPlane.form Normal file
View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormPlane">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" 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>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties>
<opaque value="true"/>
<preferredSize width="800" height="600"/>
</properties>
<border type="none"/>
<children>
<component id="2ea16" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<text value=""/>
</properties>
</component>
<component id="4f60a" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<text value=""/>
</properties>
</component>
<component id="4eb88" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<horizontalAlignment value="0"/>
<text value=""/>
</properties>
</component>
<component id="9f55" class="DrawningPlane" binding="PictureBoxPlane">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="fb937" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<text value=""/>
</properties>
</component>
<toolbar id="e747d" binding="StatusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="20"/>
<preferred-size width="-1" height="20"/>
<maximum-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children/>
</toolbar>
<hspacer id="d6bea">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="d86ff">
<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>
<component id="9e2ce" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
</children>
</grid>
</form>

80
FormPlane.java Normal file
View File

@ -0,0 +1,80 @@
import javax.imageio.ImageIO;
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 FormPlane {
public JPanel Mainpanel;
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
protected DrawningPlane PictureBoxPlane;
private JToolBar StatusStrip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
public void Draw() {
if (PictureBoxPlane.GetPlane() == null) {
return;
}
PictureBoxPlane.DrawTransport();
}
public FormPlane() {
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
StatusStrip.add(LabelBox);
try {
Image img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowUp.jpg"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowDown.jpg"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowLeft.jpg"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowRight.jpg"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ButtonCreate.addActionListener(e -> {
Random random = new Random();
PictureBoxPlane.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
PictureBoxPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
JLabelSpeed.setText("орость: " + PictureBoxPlane.GetPlane().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + PictureBoxPlane.GetPlane().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + PictureBoxPlane.GetPlane().GetBodyColor() + " "));
Draw();
});
PictureBoxPlane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
PictureBoxPlane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
Draw();
}
});
ButtonUp.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Up);
Draw();
});
ButtonDown.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Down);
Draw();
});
ButtonRight.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Right);
Draw();
});
ButtonLeft.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Left);
Draw();
});
}
}

22
IlluminatorCount.java Normal file
View File

@ -0,0 +1,22 @@
public enum IlluminatorCount {
Ten,
Twenty,
Thirty;
public static IlluminatorCount GetIlluminatorCount(int Value)
{
switch(Value)
{
case 10:
return Ten;
case 20:
return Twenty;
case 30:
return Thirty;
}
return null;
}
}

14
Main.java Normal file
View File

@ -0,0 +1,14 @@
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Корабль");
frame.setContentPane(new FormPlane().Mainpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(800, 600);
frame.setVisible(true);
}
}

BIN
Resource/arrowDown.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
Resource/arrowLeft.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
Resource/arrowRight.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
Resource/arrowUp.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB