Добавлена форма, класс-сущность, класс-отрисовка, перечисление для направлений движения

This commit is contained in:
Никита Потапов 2023-09-13 11:06:28 +04:00
parent bc30cc85fd
commit ba99ffcca9
11 changed files with 355 additions and 0 deletions

View File

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

View File

@ -0,0 +1,128 @@
package ProjectStormtrooper;
import java.awt.*;
public class DrawingStormtrooper {
public EntityStormtrooper EntityStormtrooper;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _stormtrooperWidth = 110;
private final int _stormtrooperHeight = 110;
public boolean Init(int speed, double weight, Color bodyColor,
Color additionalColor, boolean rockets, boolean bombs,
int width, int height) {
if (width < _stormtrooperWidth && height < _stormtrooperHeight) {
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityStormtrooper = new EntityStormtrooper();
EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rockets, bombs);
return true;
}
public void SetPosition(int x, int y) {
if (x < 0) {
x = 0;
} else if (x > _pictureWidth - _stormtrooperWidth) {
x = _pictureWidth - _stormtrooperWidth;
}
_startPosX = x;
if (y < 0) {
y = 0;
} else if (y > _pictureHeight - _stormtrooperHeight) {
y = _pictureHeight - _stormtrooperHeight;
}
_startPosY = y;
}
public void MoveTransport(DirectionType direction) {
if (EntityStormtrooper == null) {
return;
}
switch (direction) {
case Up -> {
if (_startPosY - EntityStormtrooper.Step() >= 0) {
_startPosY -= (int) EntityStormtrooper.Step();
}
}
case Down -> {
if (_startPosY + _stormtrooperHeight + EntityStormtrooper.Step() <= _pictureHeight) {
_startPosY += (int) EntityStormtrooper.Step();
}
}
case Left -> {
if (_startPosX - EntityStormtrooper.Step() >= 0) {
_startPosX -= (int) EntityStormtrooper.Step();
}
}
case Right -> {
if (_startPosX + _stormtrooperWidth + EntityStormtrooper.Step() <= _pictureWidth) {
_startPosX += (int) EntityStormtrooper.Step();
}
}
}
}
public void DrawTransport(Graphics g) {
if (EntityStormtrooper == null) {
return;
}
// TODO: реализовать отрисовку
Graphics2D g2d = (Graphics2D) g;
Color bodyColor = EntityStormtrooper.BodyColor;
Color additionalColor = EntityStormtrooper.AdditionalColor;
Color blackColor = Color.BLACK;
Color redColor = Color.RED;
g2d.setColor(blackColor);
g2d.drawRect(_startPosX, _startPosY, _stormtrooperWidth, _stormtrooperHeight);
// Длина фюзеляжа
int bodyHeight = _stormtrooperHeight / 9;
int bodyWidth = _stormtrooperWidth - _stormtrooperWidth / 8;
// Рисуем бомбы
if (EntityStormtrooper.Bombs) {
// todo
}
// Рисуем ракеты
if (EntityStormtrooper.Rockets) {
// todo
}
// Рисуем нос
// todo
// Рисуем крылья
// todo
// Рисуем хвостовое оперение
// todo
// Рисуем фюзеляж
// todo
g2d.setColor(bodyColor);
g2d.fillRect(
_startPosX + _stormtrooperWidth / 8,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2,
bodyWidth,
bodyHeight
);
g2d.setColor(blackColor);
g2d.drawRect(
_startPosX + _stormtrooperWidth / 8,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2,
bodyWidth,
bodyHeight
);
}
}

View File

@ -0,0 +1,23 @@
package ProjectStormtrooper;
import java.awt.*;
public class EntityStormtrooper {
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean Rockets;
public boolean Bombs;
public double Step() {
return (double) Speed * 250 / Weight;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rockets, boolean bombs) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Rockets = rockets;
Bombs = bombs;
}
}

View File

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectStormtrooper.FormStormtrooper">
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" 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="900" height="500"/>
</constraints>
<properties>
<minimumSize width="900" height="500"/>
<preferredSize width="900" height="500"/>
</properties>
<border type="none"/>
<children>
<component id="35f01" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<hspacer id="f9ba0">
<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>
<vspacer id="ce5ea">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="ac2ff" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" 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>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<icon value="ProjectStormtrooper/img/arrowDOWN.png"/>
<text value=""/>
</properties>
</component>
<component id="b1382" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" 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>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<icon value="ProjectStormtrooper/img/arrowUP.png"/>
<text value=""/>
</properties>
</component>
<component id="8b2ff" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" 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>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<icon value="ProjectStormtrooper/img/arrowLEFT.png"/>
<text value=""/>
</properties>
</component>
<component id="25771" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" 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>
<horizontalTextPosition value="0"/>
<icon value="ProjectStormtrooper/img/arrowRIGHT.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,81 @@
package ProjectStormtrooper;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormStormtrooper {
DrawingStormtrooper _drawingStormtrooper = new DrawingStormtrooper();
private JButton buttonCreate;
private JPanel pictureBox;
private JButton buttonDown;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
public JPanel getPictureBox() {
return pictureBox;
}
public FormStormtrooper() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
buttonCreate.addActionListener(e -> {
_drawingStormtrooper = new DrawingStormtrooper();
Random random = new Random();
_drawingStormtrooper.Init(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(),
random.nextBoolean(),
pictureBox.getWidth(),
pictureBox.getHeight()
);
_drawingStormtrooper.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
});
ActionListener buttonMoveClickedListener = e -> {
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp") -> {
_drawingStormtrooper.MoveTransport(DirectionType.Up);
}
case ("buttonDown") -> {
_drawingStormtrooper.MoveTransport(DirectionType.Down);
}
case ("buttonLeft") -> {
_drawingStormtrooper.MoveTransport(DirectionType.Left);
}
case ("buttonRight") -> {
_drawingStormtrooper.MoveTransport(DirectionType.Right);
}
}
Draw();
};
buttonUp.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonRight.addActionListener(buttonMoveClickedListener);
}
public void Draw() {
if (_drawingStormtrooper.EntityStormtrooper == null) {
return;
}
Graphics g = pictureBox.getGraphics();
pictureBox.paint(g);
_drawingStormtrooper.DrawTransport(g);
}
}

View File

@ -0,0 +1,7 @@
package ProjectStormtrooper;
public class Main {
public static void main(String[] args) {
MainFrameStormtrooper mainFrameStormtrooper = new MainFrameStormtrooper();
}
}

View File

@ -0,0 +1,18 @@
package ProjectStormtrooper;
import javax.swing.*;
public class MainFrameStormtrooper extends JFrame {
private FormStormtrooper _formStormtrooper;
public MainFrameStormtrooper() {
super();
setTitle("Штурмовик");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formStormtrooper = new FormStormtrooper();
setContentPane(_formStormtrooper.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
setVisible(true);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB