ПИбд-23 Кислица Егор Дмитриевич Лабораторная работа №1 (усложненная) #1

Closed
Egor wants to merge 16 commits from Laba1 into main
8 changed files with 436 additions and 23 deletions
Showing only changes of commit 7a5f91114c - Show all commits

47
.gitignore vendored
View File

@ -1,26 +1,29 @@
# ---> Java
# Compiled class file
*.class
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
# Log file
*.log
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
# BlueJ files
*.ctxt
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View File

@ -1,2 +1,2 @@
# PIbd_23_Kislitsa_E.D_AirFighter_Hard
# PIbd-13-Salin-O.A.-Monorail-Hard

View File

@ -0,0 +1,9 @@
package AirFighterHard;
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,195 @@
package AirFighterHard;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningAirFighter {
JPanel AirFighterPanel;
private EntityAirFighter EntityAirFighter;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX = 0;
private int _startPosY = 0;
private int _airfighterWidth = 160;
private int _airfighterHeight = 68;
private int _airfighterwingkorpusHeight = 90;
private DrawningEngines DrawningEngines;
public EntityAirFighter EntityAirFighter(){
return EntityAirFighter;
}
public boolean Init(int speed, double weight,Color bodyColor, int engineNumb,
int width, int height, boolean racket, boolean wing, JPanel airfighterPanel){
if(width <= _airfighterWidth || height <= _airfighterHeight)
return false;
_startPosY=70;
_startPosX = 0;
airfighterPanel.setSize(width, height);
AirFighterPanel = airfighterPanel;
airfighterPanel.paint(AirFighterPanel.getGraphics());
_pictureWidth = width;
_pictureHeight = height;
EntityAirFighter = new EntityAirFighter();
EntityAirFighter.Init(speed, weight, bodyColor, engineNumb, racket, wing);
DrawningEngines = new DrawningEngines();
DrawningEngines.Init(_airfighterWidth, _airfighterHeight,_startPosX,_startPosY,airfighterPanel);
Random rand = new Random();
DrawningEngines.ChangeEnginesNumb(rand.nextInt(1, 4));
return true;
}
public void SetPosition(int x, int y){
if(EntityAirFighter == null)
return;
_startPosX = x;
_startPosY = y;
if (x + _airfighterWidth >= _pictureWidth || y + _airfighterHeight >= _pictureHeight) {
_startPosX = 0;
_startPosY = 70;
}
}
public void MoveTransport(DirectionType direction){
if (EntityAirFighter == null)
return;
AirFighterPanel.paint(AirFighterPanel.getGraphics());
switch (direction)
{
case Left:
if (_startPosX - EntityAirFighter.Step() >= 0)
_startPosX -= (int) EntityAirFighter.Step();
break;
case Up:
if (_startPosY - EntityAirFighter.Step() - _airfighterHeight > 0)
_startPosY -= (int) EntityAirFighter.Step();
break;
case Right:
if (_startPosX + EntityAirFighter.Step() + _airfighterWidth < _pictureWidth)
_startPosX += (int) EntityAirFighter.Step();
else
_startPosX = _pictureWidth - _airfighterWidth;
break;
case Down:
if (_startPosY + EntityAirFighter.Step() + _airfighterwingkorpusHeight < _pictureHeight)
_startPosY += (int) EntityAirFighter.Step();
break;
}
DrawningEngines.CurX = _startPosX;
DrawningEngines.CurY = _startPosY;
}
public void DrawAirFighter(){
Graphics2D g2d = (Graphics2D) AirFighterPanel.getGraphics();
if (EntityAirFighter == null)
return;
DrawningEngines.DrawEngines();
//тело самолёта
g2d.setColor(EntityAirFighter.BodyColor());
g2d.fillRect(_startPosX + 20,_startPosY +4,140,20);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX + 20,_startPosY +4,140,20);
//Нос самолёта
int[] xPointsArrNose = {_startPosX + 20,_startPosX + 20,_startPosX-3};
int[] yPointsArrNose = {_startPosY + 4,_startPosY + 24,_startPosY + 12};
g2d.setColor(Color.BLACK);
g2d.fillPolygon(xPointsArrNose, yPointsArrNose, xPointsArrNose.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrNose, yPointsArrNose, xPointsArrNose.length);
// Правое крыло
int[] xPointsArrRightWing = {_startPosX + 80,_startPosX+80,_startPosX+85,_startPosX + 100};
int[] yPointsArrRightWing = { _startPosY + 4,_startPosY - 66,_startPosY - 66, _startPosY + 4 };
g2d.setColor(EntityAirFighter.BodyColor());
g2d.fillPolygon(xPointsArrRightWing, yPointsArrRightWing, xPointsArrRightWing.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrRightWing, yPointsArrRightWing, xPointsArrRightWing.length);
// Левое крыло
int[] xPointsArrLeftWing = {_startPosX + 80,_startPosX + 100,_startPosX+85,_startPosX+80};
int[] yPointsArrLeftWing = {_startPosY + 24,_startPosY + 24,_startPosY + 94,_startPosY + 94};
g2d.setColor(EntityAirFighter.BodyColor());
g2d.fillPolygon(xPointsArrLeftWing, yPointsArrLeftWing, xPointsArrLeftWing.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrLeftWing, yPointsArrLeftWing, xPointsArrLeftWing.length);
// Правое задние крыло
int[] xPointsArrRightBackWing={_startPosX + 140,_startPosX + 160,_startPosX+160,_startPosX+140};
int[] yPointsArrRightBackWing={ _startPosY + 4,_startPosY + 4,_startPosY - 22,_startPosY -4};
g2d.setColor(EntityAirFighter.BodyColor());
g2d.fillPolygon(xPointsArrRightBackWing, yPointsArrRightBackWing, xPointsArrRightBackWing.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrRightBackWing, yPointsArrRightBackWing, xPointsArrRightBackWing.length);
// Заднее левое крыло
int[] xPointsArrLeftBackWing= {_startPosX + 140,_startPosX + 160,_startPosX+160,_startPosX+140};
int[] yPointsArrLeftBackWing= {_startPosY + 24,_startPosY + 24,_startPosY + 50,_startPosY + 32};
g2d.setColor(EntityAirFighter.BodyColor());
g2d.fillPolygon(xPointsArrLeftBackWing, yPointsArrLeftBackWing, xPointsArrLeftBackWing.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrLeftBackWing, yPointsArrLeftBackWing, xPointsArrLeftBackWing.length);
//Ракеты
if (EntityAirFighter.Racket())
{
g2d.setColor(Color.GRAY);
g2d.fillRect(_startPosX + 70, _startPosY - 15, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX + 70, _startPosY - 15, 10, 10);
int[] xPointsArrNoseRacket = {_startPosX + 70,_startPosX + 70,_startPosX + 60};
int[] yPointsArrNoseRacket = {_startPosY -5,_startPosY - 15,_startPosY -10};
g2d.setColor(Color.RED);
g2d.fillPolygon(xPointsArrNoseRacket, yPointsArrNoseRacket, xPointsArrNoseRacket.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrNoseRacket, yPointsArrNoseRacket, xPointsArrNoseRacket.length);
g2d.setColor(Color.GRAY);
g2d.fillRect(_startPosX + 70, _startPosY - 40, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX + 70, _startPosY - 40, 10, 10);
int[] xPoints2ArrNoseRacket = {_startPosX + 70,_startPosX + 70,_startPosX + 60};
int[] yPoints2ArrNoseRacket = {_startPosY -30,_startPosY - 40,_startPosY -35};
g2d.setColor(Color.RED);
g2d.fillPolygon(xPoints2ArrNoseRacket, yPoints2ArrNoseRacket, xPoints2ArrNoseRacket.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPoints2ArrNoseRacket, yPoints2ArrNoseRacket, xPoints2ArrNoseRacket.length);
g2d.setColor(Color.GRAY);
g2d.fillRect(_startPosX + 70, _startPosY + 59, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX + 70, _startPosY + 59, 10, 10);
int[] xPoints3ArrNoseRacket ={_startPosX + 70,_startPosX + 70,_startPosX + 60};
int[] yPoints3ArrNoseRacket ={_startPosY +59,_startPosY + 69,_startPosY + 64};
g2d.setColor(Color.RED);
g2d.fillPolygon(xPoints3ArrNoseRacket, yPoints3ArrNoseRacket, xPoints3ArrNoseRacket.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPoints3ArrNoseRacket, yPoints3ArrNoseRacket, xPoints3ArrNoseRacket.length);
g2d.setColor(Color.GRAY);
g2d.fillRect(_startPosX + 70, _startPosY + 34, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX + 70, _startPosY + 34, 10, 10);
int[] xPoints4ArrNoseRacket={_startPosX + 70,_startPosX + 70,_startPosX + 60};
int[] yPoints4ArrNoseRacket={_startPosY +34,_startPosY + 44,_startPosY + 39};
g2d.setColor(Color.RED);
g2d.fillPolygon(xPoints4ArrNoseRacket, yPoints4ArrNoseRacket, xPoints4ArrNoseRacket.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPoints4ArrNoseRacket, yPoints4ArrNoseRacket, xPoints4ArrNoseRacket.length);
}
}
}

View File

@ -0,0 +1,63 @@
package AirFighterHard;
import javax.swing.*;
import java.awt.*;
public class DrawningEngines {
private int WheelSz;
JPanel AirFighterPanel;
private NumberType EnginesNumb;
private Color EngineColor;
private int Width, Height;
public int CurX, CurY;
boolean Init(int width, int height, int curX, int curY, JPanel airfighterPanel){
Width = width;
Height = height;
CurX = curX;
CurY = curY;
EngineColor = Color.BLACK;
AirFighterPanel = airfighterPanel;
return true;
}
public void ChangeEnginesNumb(int x){
if(x ==1)
EnginesNumb = NumberType.Two;
if(x == 2)
EnginesNumb = NumberType.Four;
if(x == 3)
EnginesNumb = NumberType.Six;
}
public NumberType WheelsNumb(){
return EnginesNumb;
}
public void DrawEngines(){
Graphics2D g2d = (Graphics2D) AirFighterPanel.getGraphics();
g2d.setColor(EngineColor);
g2d.fillOval(CurX + 92,CurY - 15,25,10 );
g2d.fillOval(CurX + 92,CurY + 34,25,10 );
if (EnginesNumb == NumberType.Four || EnginesNumb == NumberType.Six)
{
g2d.fillOval(CurX + 86,CurY - 40,25,10 );
g2d.fillOval(CurX + 86,CurY +59,25,10 );
}
if (EnginesNumb == NumberType.Six)
{
g2d.fillOval(CurX + 128,CurY -7,25,10 );
g2d.fillOval(CurX + 128,CurY +25,25,10 );
}
}
}

View File

@ -0,0 +1,40 @@
package AirFighterHard;
import java.awt.*;
public class EntityAirFighter {
private int Speed, EngineNumb;
private double Weight, Step;
private Color BodyColor;
private boolean Wing;
private boolean Racket;
public int Speed(){
return Speed;
}
public int WheelNumb(){
return EngineNumb;
}
public double Weight(){
return Weight;
}
public double Step(){
return Step;
}
public Color BodyColor(){
return BodyColor;
}
public boolean Racket(){return Racket;}
public boolean Wing(){return Wing;}
public void Init(int speed, double weight, Color bodyColor, int engineNumb,
boolean racket, boolean wing){
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
EngineNumb = engineNumb;
Racket = racket;
Wing = wing;
}
}

View File

@ -0,0 +1,96 @@
package AirFighterHard;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
JFrame AirFighterFrame = new JFrame();
JPanel AirFighterPanel = new JPanel();
AirFighterFrame.setLayout(new BorderLayout());
AirFighterFrame.setSize(900, 500);
AirFighterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AirFighterFrame.setLayout(new BorderLayout(1,1));
DrawningAirFighter DrawningAirFighter = new DrawningAirFighter();
AirFighterPanel.setLayout(null);
BufferedImage RightIcon = ImageIO.read(new File("RightButton.png"));
BufferedImage LeftIcon = ImageIO.read(new File("LeftButton.png"));
BufferedImage UpIcon = ImageIO.read(new File("UpButton.png"));
BufferedImage DownIcon = ImageIO.read(new File("DownButton.png"));
JButton RightButton = new JButton(new ImageIcon(RightIcon));
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
JButton UpButton = new JButton(new ImageIcon(UpIcon));
JButton DownButton = new JButton(new ImageIcon(DownIcon));
JButton CreateButton = new JButton();
CreateButton.setText("Создать");
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
AirFighterPanel.add(CreateButton);
AirFighterPanel.add(RightButton);
AirFighterPanel.add(LeftButton);
AirFighterPanel.add(UpButton);
AirFighterPanel.add(DownButton);
AirFighterFrame.add(AirFighterPanel, BorderLayout.CENTER);
Random random = new Random();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningAirFighter.Init(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextInt(1, 4),
AirFighterPanel.getWidth(), AirFighterPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), AirFighterPanel);
DrawningAirFighter.DrawAirFighter();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningAirFighter.EntityAirFighter() == null)
return;
DrawningAirFighter.MoveTransport(DirectionType.Right);
DrawningAirFighter.DrawAirFighter();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningAirFighter.EntityAirFighter() == null)
return;
DrawningAirFighter.MoveTransport(DirectionType.Left);
DrawningAirFighter.DrawAirFighter();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningAirFighter.EntityAirFighter() == null)
return;
DrawningAirFighter.MoveTransport(DirectionType.Up);
DrawningAirFighter.DrawAirFighter();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningAirFighter.EntityAirFighter() == null)
return;
DrawningAirFighter.MoveTransport(DirectionType.Down);
DrawningAirFighter.DrawAirFighter();
}
});
AirFighterFrame.setVisible(true);
}
}

View File

@ -0,0 +1,7 @@
package AirFighterHard;
public enum NumberType {
Two,
Four,
Six
}