Лабораторная работа 1

This commit is contained in:
Evgehil 2024-04-13 13:12:16 +04:00
parent b22918cdf5
commit b637ff72fc
18 changed files with 435 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

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

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" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

9
.idea/modules.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-11_Gorskov_E.M._Hard.iml" filepath="$PROJECT_DIR$/.idea/PIbd-11_Gorskov_E.M._Hard.iml" />
<module fileurl="file://$PROJECT_DIR$/src/src.iml" filepath="$PROJECT_DIR$/src/src.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>

BIN
src/image/down.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

BIN
src/image/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

BIN
src/image/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

BIN
src/image/up.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

11
src/src.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$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

16
src/src/CanvasAirbus.java Normal file
View File

@ -0,0 +1,16 @@
import javax.swing.*;
import java.awt.*;
public class CanvasAirbus extends JComponent {
public DrawingAirbus _drawingAirbus;
public CanvasAirbus(){}
public void paintComponent(Graphics g) {
if (_drawingAirbus == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingAirbus.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,5 @@
public enum CountPortholes {
Ten,
Twenty,
Thirty;
}

View File

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

144
src/src/DrawingAirbus.java Normal file
View File

@ -0,0 +1,144 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingAirbus extends JPanel {
public EntityAirbus EntityAirbus;
public DrawingPortholes _portholes = null;
private Integer picture_width;
private Integer picture_height;
private Integer _StartPosX;
private Integer _StartPosY;
private int drawingAirbusWidth = 185;
private int drawingAirbusHeight = 100;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank) {
EntityAirbus = new EntityAirbus();
EntityAirbus.Init(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank);
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
_portholes = new DrawingPortholes();
Random rand = new Random();
int randomNum = rand.nextInt(4);
_portholes.SetCount(randomNum);
}
public boolean SetPictureSize(int width, int height) {
if (width < drawingAirbusWidth || height < drawingAirbusHeight) return false;
picture_width = width;
picture_height = height;
if (_StartPosX != null || _StartPosY != null) {
if (_StartPosX + drawingAirbusWidth > picture_width)
{
_StartPosX = _StartPosX - (_StartPosX + drawingAirbusWidth - picture_width);
}
else if (_StartPosX < 0) _StartPosX = 0;
if (_StartPosY + drawingAirbusHeight > picture_height)
{
_StartPosY = _StartPosY - (_StartPosY + drawingAirbusHeight - picture_height);
}
else if (_StartPosY < 0) _StartPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(picture_width != null && picture_height != null)) return;
if (x + drawingAirbusWidth > picture_width)
{
_StartPosX = x - (x + drawingAirbusWidth - picture_width);
}
else if (x < 0) _StartPosX = 0;
else _StartPosX = x;
if (y + drawingAirbusHeight > picture_height)
{
_StartPosY = y - (y + drawingAirbusHeight - picture_height);
}
else if (y < 0) _StartPosY = 0;
else _StartPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityAirbus == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) {
case Left:
if (_StartPosX - EntityAirbus.Step > 0) {
_StartPosX -= (int)EntityAirbus.Step;
}
return true;
case Up:
if (_StartPosY - EntityAirbus.Step > 0)
{
_StartPosY -= (int)EntityAirbus.Step;
}
return true;
case Right:
if (_StartPosX + drawingAirbusWidth + (int)EntityAirbus.Step < picture_width - EntityAirbus.Step)
{
_StartPosX += (int)EntityAirbus.Step;
}
return true;
case Down:
if (_StartPosY + drawingAirbusHeight + (int)EntityAirbus.Step < picture_height - EntityAirbus.Step)
{
_StartPosY += (int)EntityAirbus.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityAirbus == null) {
return;
}
// иллюминаторы
_portholes.Draw(g, _StartPosX+30, _StartPosY+34);
g.setColor(Color.BLACK);
//Тело
g.drawRect(_StartPosX + 5, _StartPosY + 50, 170, 30);
g.drawArc(_StartPosX - 5, _StartPosY + 50, 20, 30, 90, 180);
//Заднее крыло
g.drawLine( _StartPosX, _StartPosY, _StartPosX + 50, _StartPosY + 50);
g.drawLine( _StartPosX, _StartPosY, _StartPosX, _StartPosY + 52);
//Заднее боковые крылья
g.drawOval(_StartPosX - 7, _StartPosY + 45, 30, 8);
g.fillOval(_StartPosX - 7, _StartPosY + 45, 30, 8);
//Нос
g.drawLine( _StartPosX + 175, _StartPosY + 50, _StartPosX + 200, _StartPosY + 65);
g.drawLine( _StartPosX + 200, _StartPosY + 65, _StartPosX + 175, _StartPosY + 80);
g.drawLine( _StartPosX + 175, _StartPosY + 50, _StartPosX + 175, _StartPosY + 80);
g.drawLine( _StartPosX + 175, _StartPosY + 65, _StartPosX + 200, _StartPosY + 65);
//Крылья
g.drawArc(_StartPosX + 49, _StartPosY + 62, 5, 5, 90, 180);
g.drawLine( _StartPosX + 51, _StartPosY + 62, _StartPosX + 140, _StartPosY + 62);
g.drawArc( _StartPosX + 137, _StartPosY + 62, 5, 5, 2790, 180);
g.drawLine( _StartPosX + 51, _StartPosY + 67, _StartPosX + 140, _StartPosY + 67);
//Задние шасси
g.drawLine(_StartPosX + 55, _StartPosY + 80, _StartPosX + 55, _StartPosY + 90);
g.drawOval(_StartPosX + 47, _StartPosY + 90, 5, 5);
g.drawOval( _StartPosX + 57, _StartPosY + 90, 5, 5);
//Передние шасси
g.drawLine( _StartPosX + 165, _StartPosY + 80, _StartPosX + 165, _StartPosY + 90);
g.drawOval( _StartPosX + 163, _StartPosY + 91, 5, 5);
//Пассажирсакий доп. отсек
if (EntityAirbus.getIsCompartmen())
{
g.setColor(EntityAirbus.getAdditionalColor());
g.drawArc(_StartPosX + 60, _StartPosY + 28, 115, 45, 0, 180);
g.fillArc(_StartPosX + 60, _StartPosY + 28, 115, 45, 0, 180);
}
// Доп. двигатели
if (EntityAirbus.getIsAdditionalEngine())
{
g.drawLine(_StartPosX + 95, _StartPosY + 68, _StartPosX + 95, _StartPosY + 75);
g.setColor(EntityAirbus.getAdditionalColor());
int[] xPolygon = { _StartPosX + 83, _StartPosX + 103, _StartPosX + 103, _StartPosX + 83, _StartPosX + 83};
int[] yPolygon = {_StartPosY + 78, _StartPosY + 73, _StartPosY + 93, _StartPosY + 88, _StartPosY + 78};
g.drawPolygon(xPolygon, yPolygon, xPolygon.length);
g.fillPolygon(xPolygon, yPolygon, xPolygon.length);
}
}
}

View File

@ -0,0 +1,63 @@
import java.awt.*;
public class DrawingPortholes {
private CountPortholes _porthole;
public CountPortholes getCount() {
return _porthole;
}
public void SetCount(int count) {
switch (count) {
case 1: // 10
_porthole = CountPortholes.Ten;
break;
case 2: // 20
_porthole = CountPortholes.Twenty;
break;
case 3: // 30
_porthole = CountPortholes.Thirty;
break;
default:
_porthole = CountPortholes.Ten;
break;
}
}
public void Draw(Graphics2D g, int _startPosx, int _startPoxY) {
g.setColor(Color.BLACK);
if (_porthole == null) {
return;
}
for (int i = 0; i < 10; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx + 19 + i * 8, _startPoxY + 21, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 19 + i * 8, _startPoxY + 21, 3, 3);
}
if (_porthole != CountPortholes.Ten) {
for (int i = 0; i < 5; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx - 15 + i * 5, _startPoxY + 28, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx - 15 + i * 5, _startPoxY + 28, 3, 3);
}
for (int i = 0; i < 5; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx + 115 + i * 5, _startPoxY + 28, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 115 + i * 5, _startPoxY + 28, 3, 3);
}
}
if (_porthole == CountPortholes.Thirty) {
for (int i = 0; i < 10; ++i) {
g.setColor(Color.cyan);
g.fillOval(_startPosx + 19 + i * 8, _startPoxY + 37, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 19 + i * 8, _startPoxY + 37, 3, 3);
}
}
}
}

24
src/src/EntityAirbus.java Normal file
View File

@ -0,0 +1,24 @@
import java.awt.*;
public class EntityAirbus {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
private Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
private boolean IsCompartment;
public boolean getIsCompartmen() {return IsCompartment;}
private boolean IsAdditionalEngine;
public boolean getIsAdditionalEngine() {return IsAdditionalEngine;}
public double Step;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean isCompartment, boolean isAdditionalEngine)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
IsCompartment = isCompartment;
IsAdditionalEngine = isAdditionalEngine;
Step = Speed * 100 / Weight;
}
}

123
src/src/FormAirbus.java Normal file
View File

@ -0,0 +1,123 @@
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 FormAirbus extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasAirbus canvasAirbus = new CanvasAirbus();
private JButton CreateButton = new JButton("Create");;
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
public FormAirbus(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35;
CreateButton.setName("CREATE");
Icon iconUp = new ImageIcon("src\\image\\up.jpg");
UpButton.setIcon(iconUp);
UpButton.setName("UP");
DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("src\\image\\down.jpg");
DownButton.setIcon(iconDown);
LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("src\\image\\left.jpg");
LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("src\\image\\right.jpg");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
boolean sheepPipes = new Random().nextBoolean();
boolean fuelTank = new Random().nextBoolean();;
canvasAirbus._drawingAirbus = new DrawingAirbus();
canvasAirbus._drawingAirbus.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
canvasAirbus._drawingAirbus.SetPictureSize(Width, Height);
canvasAirbus._drawingAirbus.SetPosition( StartPositionX, StartPositionY);
canvasAirbus.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasAirbus._drawingAirbus == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "UP":
result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasAirbus.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasAirbus.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasAirbus);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasAirbus._drawingAirbus != null)
canvasAirbus._drawingAirbus.SetPictureSize(Width, Height);
canvasAirbus.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
}
});
}
}

8
src/src/Main.java Normal file
View File

@ -0,0 +1,8 @@
import java.awt.*;
public class Main {
public static void main(String[] args) {
FormAirbus form = new FormAirbus("Самолёт", new Dimension(500, 500));
form.Init();
}
}