PIbd-22. Shabunov O.A. Lab work 01 (Hard) #1

Closed
olshab wants to merge 6 commits from Lab1 into main
12 changed files with 500 additions and 5 deletions

View File

@ -0,0 +1,38 @@
package AirBomber;
import java.awt.Color;
public class BomberEntity
{
private int Speed;
private double Weight;
private Color BodyColor;
private Color AdditionalColor;
private boolean Bombs;
private boolean FuelTanks;
public double Step;
public void Init(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs)
{
this.Speed = Speed;
this.Weight = Weight;
this.BodyColor = BodyColor;
this.AdditionalColor = AdditionalColor;
this.FuelTanks = FuelTanks;
this.Bombs = Bombs;
this.Step = (double)Speed * 100 / Weight * 5 / 2;
}
public int GetSpeed() { return Speed; }
public double GetWeight() { return Weight; }
public Color GetBodyColor() { return BodyColor; }
public Color GetAdditionalColor() { return AdditionalColor; }
public boolean GetBombs() { return Bombs; }
public boolean GetFuelTanks() { return FuelTanks; }
}

View File

@ -0,0 +1,169 @@
package AirBomber;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
public class BomberForm extends JFrame
{
private BomberRenderer _bomberRenderer;
public BomberForm()
{
InitializeComponent();
}
private void Draw()
{
if (_bomberRenderer == null)
return;
BufferedImage bmp = new BufferedImage(
BomberPictureBox.getWidth(),
BomberPictureBox.getHeight(),
BufferedImage.TYPE_INT_ARGB
);
Graphics2D g = bmp.createGraphics();
_bomberRenderer.DrawEntity(g);
BomberPictureBox.setIcon(new ImageIcon(bmp));
}
private void ButtonCreate_Click(ActionEvent e)
{
Random random = new Random();
_bomberRenderer = new BomberRenderer();
_bomberRenderer.Init(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
true,
true,
BomberPictureBox.getWidth(),
BomberPictureBox.getHeight()
);
_bomberRenderer.SetPosition(random.nextInt(20, 100), random.nextInt(20, 100));
Draw();
}
private void ButtonMove_Click(ActionEvent e)
{
if (_bomberRenderer == null)
return;
String ButtonName = ((JButton)e.getSource()).getName();
switch (ButtonName)
{
case "ButtonUp":
_bomberRenderer.MoveEntity(DirectionType.Up);
break;
case "ButtonDown":
_bomberRenderer.MoveEntity(DirectionType.Down);
break;
case "ButtonLeft":
_bomberRenderer.MoveEntity(DirectionType.Left);
break;
case "ButtonRight":
_bomberRenderer.MoveEntity(DirectionType.Right);
break;
}
Draw();
}
private void InitializeComponent()
{
BomberPictureBox = new JLabel();
CreateButton = new JButton();
ButtonRight = new JButton();
ButtonDown = new JButton();
ButtonLeft = new JButton();
ButtonUp = new JButton();
//
// BomberPictureBox
//
BomberPictureBox.setBounds(0, 0, 884, 461);
//
// CreateButton
//
CreateButton.setName("CreateButton");
CreateButton.setBounds(12, 419, 80, 30);
CreateButton.setText("Создать");
CreateButton.setBackground(new Color(225, 225, 225));
CreateButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
CreateButton.setFocusable(false);
CreateButton.addActionListener(e -> ButtonCreate_Click(e));
//
// ButtonRight
//
ButtonRight.setName("ButtonRight");
ButtonRight.setBounds(842, 419, 30, 30);
ButtonRight.setBackground(new Color(225, 225, 225));
ButtonRight.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonRight.setFocusable(false);
ButtonRight.setIcon(new ImageIcon("src/AirBomber/Resources/ArrowRight.png"));
ButtonRight.addActionListener(e -> ButtonMove_Click(e));
//
// ButtonDown
//
ButtonDown.setName("ButtonDown");
ButtonDown.setBounds(806, 419, 30, 30);
ButtonDown.setBackground(new Color(225, 225, 225));
ButtonDown.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonDown.setFocusable(false);
ButtonDown.setIcon(new ImageIcon("src/AirBomber/Resources/ArrowDown.png"));
ButtonDown.addActionListener(e -> ButtonMove_Click(e));
//
// ButtonLeft
//
ButtonLeft.setName("ButtonLeft");
ButtonLeft.setBounds(770, 419, 30, 30);
ButtonLeft.setBackground(new Color(225, 225, 225));
ButtonLeft.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonLeft.setFocusable(false);
ButtonLeft.setIcon(new ImageIcon("src/AirBomber/Resources/ArrowLeft.png"));
ButtonLeft.addActionListener(e -> ButtonMove_Click(e));
//
// ButtonUp
//
ButtonUp.setName("ButtonUp");
ButtonUp.setBounds(806, 383, 30, 30);
ButtonUp.setBackground(new Color(225, 225, 225));
ButtonUp.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonUp.setFocusable(false);
ButtonUp.setIcon(new ImageIcon("src/AirBomber/Resources/ArrowUp.png"));
ButtonUp.addActionListener(e -> ButtonMove_Click(e));
//
// BomberForm
//
setTitle("Бомбардировщик");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900, 500);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
add(ButtonUp);
add(ButtonLeft);
add(ButtonDown);
add(ButtonRight);
add(CreateButton);
add(BomberPictureBox);
}
private JLabel BomberPictureBox;
private JButton CreateButton;
private JButton ButtonRight;
private JButton ButtonDown;
private JButton ButtonLeft;
private JButton ButtonUp;
}

View File

@ -0,0 +1,228 @@
package AirBomber;
import java.awt.*;
import java.util.Random;
public class BomberRenderer
{
private BomberEntity EntityBomber;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _bomberWidth = 200;
private int _bomberHeight = 200;
public EngineRenderer EngineRenderer;
public boolean Init(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs, int Width, int Height)
{
if (Width < _bomberWidth || Height < _bomberHeight)
return false;
_pictureWidth = Width;
_pictureHeight = Height;
EntityBomber = new BomberEntity();
EntityBomber.Init(Speed, Weight, BodyColor, AdditionalColor, FuelTanks, Bombs);
EngineRenderer = new EngineRenderer();
Random Random = new Random();
EngineRenderer.SetAmount(Random.nextInt(1, 7));
return true;
}
public BomberEntity GetEntityBomber() { return EntityBomber; }
public void SetPosition(int x, int y)
{
if (EntityBomber == null)
return;
if (x < 0)
x = 0;
else if (x + _bomberWidth > _pictureWidth)
x = _pictureWidth - _bomberWidth;
_startPosX = x;
if (y < 0)
y = 0;
else if (y + _bomberHeight > _pictureHeight)
y = _pictureHeight - _bomberHeight;
_startPosY = y;
}
public void MoveEntity(DirectionType Direction)
{
if (EntityBomber == null)
return;
switch (Direction)
{
case Up:
if (_startPosY - EntityBomber.Step > 0)
_startPosY -= (int)EntityBomber.Step;
break;
case Down:
if (_startPosY + _bomberHeight + EntityBomber.Step <= _pictureHeight)
_startPosY += (int)EntityBomber.Step;
break;
case Left:
if (_startPosX - EntityBomber.Step > 0)
_startPosX -= (int)EntityBomber.Step;
break;
case Right:
if (_startPosX + _bomberWidth + EntityBomber.Step <= _pictureWidth)
_startPosX += (int)EntityBomber.Step;
break;
}
}
public void DrawEntity(Graphics g)
{
if (EntityBomber == null)
return;
/** Отрисовка основной части */
g.setColor(EntityBomber.GetBodyColor());
int[] LeftWingX = {
_startPosX + 90,
_startPosX + 100,
_startPosX + 108,
_startPosX + 90,
};
int[] LeftWingY = {
_startPosY,
_startPosY,
_startPosY + 85,
_startPosY + 85,
};
g.drawPolygon(LeftWingX, LeftWingY, 4);
int[] RightWingX = {
_startPosX + 90,
_startPosX + 100,
_startPosX + 108,
_startPosX + 90,
};
int[] RightWingY = {
_startPosY + 200,
_startPosY + 200,
_startPosY + 115,
_startPosY + 115,
};
g.drawPolygon(RightWingX, RightWingY, 4);
int[] BodyX = {
_startPosX + 35,
_startPosX + 200,
_startPosX + 200,
_startPosX + 35,
};
int[] BodyY = {
_startPosY + 85,
_startPosY + 85,
_startPosY + 115,
_startPosY + 115,
};
g.drawPolygon(BodyX, BodyY, 4);
int[] NoseX = {
_startPosX,
_startPosX + 35,
_startPosX + 35,
};
int[] NoseY = {
_startPosY + 100,
_startPosY + 85,
_startPosY + 115,
};
g.fillPolygon(NoseX, NoseY, 3);
int[] BackLeftWingX = {
_startPosX + 170,
_startPosX + 200,
_startPosX + 200,
_startPosX + 170,
};
int[] BackLeftWingY = {
_startPosY + 70,
_startPosY + 40,
_startPosY + 85,
_startPosY + 85,
};
g.drawPolygon(BackLeftWingX, BackLeftWingY, 4);
int[] BackRightWingX = {
_startPosX + 170,
_startPosX + 200,
_startPosX + 200,
_startPosX + 170,
};
int[] BackRightWingY = {
_startPosY + 130,
_startPosY + 160,
_startPosY + 115,
_startPosY + 115,
};
g.drawPolygon(BackRightWingX, BackRightWingY, 4);
/** Отрисовка дополнительных элементов */
g.setColor(EntityBomber.GetAdditionalColor());
if (EntityBomber.GetFuelTanks())
{
int[] LeftGasTankX = {
_startPosX + 50,
_startPosX + 75,
_startPosX + 75,
_startPosX + 50,
};
int[] LeftGasTankY = {
_startPosY + 85,
_startPosY + 85,
_startPosY + 70,
_startPosY + 70,
};
g.fillPolygon(LeftGasTankX, LeftGasTankY, 4);
int[] RightGasTankX = {
_startPosX + 50,
_startPosX + 75,
_startPosX + 75,
_startPosX + 50,
};
int[] RightGasTankY = {
_startPosY + 115,
_startPosY + 115,
_startPosY + 130,
_startPosY + 130,
};
g.fillPolygon(RightGasTankX, RightGasTankY, 4);
}
if (EntityBomber.GetBombs())
{
g.fillOval(_startPosX + 110, _startPosY + 115, 50, 25);
g.fillOval(_startPosX + 110, _startPosY + 60, 50, 25);
}
EngineRenderer.DrawEngines(g, EntityBomber.GetBodyColor(), _startPosX, _startPosY);
}
}

View File

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

View File

@ -0,0 +1,40 @@
package AirBomber;
import java.awt.*;
public class EngineRenderer
{
private EnginesAmount Amount;
public void SetAmount(int Amount)
{
if (Amount <= 2)
this.Amount = EnginesAmount.Two;
else if (Amount > 2 && Amount <= 4)
this.Amount = EnginesAmount.Four;
else
this.Amount = EnginesAmount.Six;
}
public void DrawEngines(Graphics g, Color BodyColor, int _startPosX, int _startPosY)
{
g.setColor(BodyColor);
g.fillOval(_startPosX + 83, _startPosY + 50, 30, 15);
g.fillOval(_startPosX + 83, _startPosY + 135, 30, 15);
if (Amount == EnginesAmount.Two)
return;
g.fillOval(_startPosX + 83, _startPosY + 30, 30, 15);
g.fillOval(_startPosX + 83, _startPosY + 155, 30, 15);
if (Amount == EnginesAmount.Four)
return;
g.fillOval(_startPosX + 83, _startPosY + 10, 30, 15);
g.fillOval(_startPosX + 83, _startPosY + 175, 30, 15);
}
}

View File

@ -0,0 +1,8 @@
package AirBomber;
public enum EnginesAmount
{
Two,
Four,
Six
}

View File

@ -0,0 +1,8 @@
package AirBomber;
public class Program {
public static void main(String[] args) throws Exception
{
new BomberForm();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 507 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 502 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 539 B

View File

@ -1,5 +0,0 @@
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
}
}