Port from base project

This commit is contained in:
ShabOl 2023-11-26 16:21:53 +04:00
parent 7f4a313f02
commit be6065021e
10 changed files with 414 additions and 5 deletions

View File

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

24
src/BomberEntity.java Normal file
View File

@ -0,0 +1,24 @@
import java.awt.Color;
public class BomberEntity
{
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean Bombs;
public 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;
}
}

163
src/BomberForm.java Normal file
View File

@ -0,0 +1,163 @@
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()
{
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/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/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/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/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;
}

214
src/BomberRenderer.java Normal file
View File

@ -0,0 +1,214 @@
import java.awt.*;
public class BomberRenderer
{
public BomberEntity EntityBomber;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _bomberWidth = 200;
private int _bomberHeight = 200;
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);
return true;
}
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.BodyColor);
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.AdditionalColor);
if (EntityBomber.FuelTanks)
{
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.Bombs)
{
g.fillOval(_startPosX + 110, _startPosY + 115, 50, 25);
g.fillOval(_startPosX + 110, _startPosY + 60, 50, 25);
}
}
}

7
src/DirectionType.java Normal file
View File

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

6
src/Program.java Normal file
View File

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

BIN
src/Resources/ArrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

BIN
src/Resources/ArrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 507 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 502 B

BIN
src/Resources/ArrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 539 B