This commit is contained in:
marusya 2023-12-23 12:21:29 +04:00
parent 74259131b5
commit a919a51967
14 changed files with 601 additions and 3 deletions

1
.idea/.name generated Normal file
View File

@ -0,0 +1 @@
Main.java

BIN
img/arrowDown.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 702 B

BIN
img/arrowLeft.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 B

BIN
img/arrowRight.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 702 B

BIN
img/arrowUp.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 B

230
src/DrawingUsta.java Normal file
View File

@ -0,0 +1,230 @@
import java.awt.*;
import java.awt.geom.Path2D;
import java.awt.geom.Arc2D;
public class DrawingUsta {
public EntityUsta EntityUsta;
private DrawingWheel _drawingWheel;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _ustaWidth = 170;
private final int _ustaHeight = 90;
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor,
boolean bodyKit, boolean pushka, int width, int height)
{
if (width < _ustaWidth || height < _ustaHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityUsta = new EntityUsta();
_drawingWheel = new DrawingWheel();
EntityUsta.Init(speed, weight, bodyColor, additionalColor, bodyKit, pushka);
return true;
}
public void SetWheelsCount(int weelsCount) {
_drawingWheel.SetWheelsCount(weelsCount);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _ustaWidth > _pictureWidth)
{
x = 20;
}
if (y < 0 || y + _ustaHeight > _pictureHeight)
{
y = 20;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DyrectionType direction){
if(EntityUsta == null) return;
switch(direction)
{
case Up -> {
if(_startPosY - EntityUsta.Step() >= 5)
_startPosY -= (int) EntityUsta.Step();
}
case Down -> {
if(_startPosY + EntityUsta.Step() + _ustaHeight <= _pictureHeight)
_startPosY += (int) EntityUsta.Step();
}
case Left -> {
if(_startPosX - EntityUsta.Step() >= 0)
_startPosX -= (int) EntityUsta.Step();
}
case Right -> {
if(_startPosX + EntityUsta.Step() + _ustaWidth <= _pictureWidth)
_startPosX += (int) EntityUsta.Step();
}
}
}
public void DrawTransport(Graphics2D g) {
if (EntityUsta == null) {
return;
}
Color bodyColor = EntityUsta.BodyColor;
Color additionalColor = EntityUsta.AdditionalColor;
Color blackBrush = Color.BLACK;
Color color1 = new Color(65, 72, 51);
Color color2 = new Color(47, 69, 56);
int cornerRadius1 = 12;
_drawingWheel.DrawWheels(g, additionalColor, _startPosX, _startPosY);
g.setColor(color1);
g.fillRect(_startPosX + 45, _startPosY + 20, 40, 20);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 45, _startPosY + 20, 40, 20);
g.setColor(color1);
g.fillRect(_startPosX + 10, _startPosY + 40, 120, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 10, _startPosY + 40, 120, 10);
if(EntityUsta.BodyKit)
{
Path2D path1 = new Path2D.Double();
path1.moveTo(_startPosX + 15, _startPosY + 40);
path1.lineTo(_startPosX + 15, _startPosY + 30);
path1.lineTo(_startPosX + 15, _startPosY + 35);
path1.lineTo(_startPosX + 25, _startPosY + 35);
path1.lineTo(_startPosX + 30, _startPosY + 30);
path1.lineTo(_startPosX + 35, _startPosY + 40);
path1.closePath();
g.setColor(Color.BLACK);
g.draw(path1);
g.setColor(color2);
g.fill(path1);
Path2D path2 = new Path2D.Double();
path2.moveTo(_startPosX + 15, _startPosY + 30);
path2.lineTo(_startPosX + 40, _startPosY + 20);
path2.lineTo(_startPosX + 40, _startPosY + 25);
path2.lineTo(_startPosX + 25, _startPosY + 35);
path2.closePath();
g.setColor(Color.BLACK);
g.draw(path2);
g.setColor(additionalColor);
g.fill(path2);
Path2D path3 = new Path2D.Double();
path3.moveTo(_startPosX + 10, _startPosY + 32);
path3.lineTo(_startPosX, _startPosY + 10);
path3.lineTo(_startPosX + 40, _startPosY - 7);
path3.lineTo(_startPosX + 50, _startPosY + 16);
path3.closePath();
g.setColor(Color.BLACK);
g.draw(path3);
g.setColor(bodyColor);
g.fill(path3);
Path2D path4 = new Path2D.Double();
path4.moveTo(_startPosX, _startPosY + 10);
path4.lineTo(_startPosX + 40, _startPosY - 7);
path4.lineTo(_startPosX + 43, _startPosY - 5);
path4.lineTo(_startPosX + 41, _startPosY - 3);
path4.lineTo(_startPosX + 2, _startPosY + 13);
path4.closePath();
g.setColor(Color.BLACK);
g.draw(path4);
g.setColor(additionalColor);
g.fill(path4);
Path2D path5 = new Path2D.Double();
path5.moveTo(_startPosX + 3, _startPosY + 15);
path5.lineTo(_startPosX + 45, _startPosY - 2);
path5.lineTo(_startPosX + 46, _startPosY - 2);
path5.lineTo(_startPosX + 49, _startPosY - 1);
path5.lineTo(_startPosX + 46, _startPosY + 3);
path5.lineTo(_startPosX + 6, _startPosY + 20);
path5.closePath();
g.setColor(Color.BLACK);
g.draw(path5);
g.setColor(bodyColor);
g.fill(path5);
Path2D path6 = new Path2D.Double();
path6.moveTo(_startPosX + 5, _startPosY + 22);
path6.lineTo(_startPosX + 47, _startPosY + 5);
path6.lineTo(_startPosX + 51, _startPosY + 5);
path6.lineTo(_startPosX + 53, _startPosY + 7);
path6.lineTo(_startPosX + 8, _startPosY + 25);
path6.closePath();
g.setColor(Color.BLACK);
g.draw(path6);
g.setColor(additionalColor);
g.fill(path6);
Path2D path7 = new Path2D.Double();
path7.moveTo(_startPosX + 7, _startPosY + 27);
path7.lineTo(_startPosX + 46, _startPosY + 11);
path7.lineTo(_startPosX + 51, _startPosY + 9);
path7.lineTo(_startPosX + 56, _startPosY + 11);
path7.lineTo(_startPosX + 10, _startPosY + 31);
path7.closePath();
g.setColor(Color.BLACK);
g.draw(path7);
g.setColor(bodyColor);
g.fill(path7);
}
if(EntityUsta.Pushka)
{
g.fillRect(_startPosX + 80, _startPosY + 25, 10, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 80, _startPosY + 25, 10, 10);
g.setColor(color1);
g.fillRect(_startPosX + 90, _startPosY + 28, 40, 5);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 90, _startPosY + 28, 40, 5);
g.setColor(additionalColor);
g.fillRect(_startPosX + 130, _startPosY + 27, 5, 7);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 130, _startPosY + 27, 5, 7);
}
}
}

140
src/DrawingWheel.java Normal file
View File

@ -0,0 +1,140 @@
import java.awt.*;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
public class DrawingWheel {
private WheelsCount _wheelsCount;
public void SetWheelsCount(int enginesCount) {
for (WheelsCount val : WheelsCount.values()) {
if (val.count == enginesCount) {
this._wheelsCount = val;
return;
}
}
}
public void DrawWheels(Graphics2D g, Color color, int startPosX, int startPosY) {
if (_wheelsCount == null) {
return;
}
int cornerRadius1 = 12;
Color color1 = new Color(65, 72, 51);
if (_wheelsCount.count >= _wheelsCount.Four.count) {
Path2D path = new Path2D.Double();
path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 180, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 110, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 270, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 110, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 0, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 90, 90, Arc2D.OPEN), true);
path.closePath();
g.setColor(color1);
g.fill(path);
g.setColor(Color.BLACK);
g.draw(path);
/* наверху гусеницы*/
g.fillOval(startPosX + 47, startPosY + 44, 13, 13);
g.fillOval(startPosX + 62, startPosY + 44, 13, 13);
g.fillOval(startPosX + 77, startPosY + 44, 13, 13);
/* по середине гусеницы*/
g.drawOval(startPosX + 40, startPosY + 59, 13, 13);
g.drawOval(startPosX + 55, startPosY + 59, 13, 13);
g.drawOval(startPosX + 70, startPosY + 59, 13, 13);
g.drawOval(startPosX + 85, startPosY + 59, 13, 13);
/* два крайних на гусенице*/
g.fillOval(startPosX + 10, startPosY + 50, 25, 25);
g.fillOval(startPosX + 105, startPosY + 50, 25, 25);
}
if (_wheelsCount.count >= _wheelsCount.Five.count) {
Path2D path = new Path2D.Double();
path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 180, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 125, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 270, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 125, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 0, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 90, 90, Arc2D.OPEN), true);
path.closePath();
g.setColor(color1);
g.fill(path);
g.setColor(Color.BLACK);
g.draw(path);
/* наверху гусеницы*/
g.fillOval(startPosX + 47, startPosY + 44, 13, 13);
g.fillOval(startPosX + 62, startPosY + 44, 13, 13);
g.fillOval(startPosX + 77, startPosY + 44, 13, 13);
g.fillOval(startPosX + 92, startPosY + 44, 13, 13);
/* по середине гусеницы*/
g.drawOval(startPosX + 40, startPosY + 59, 13, 13);
g.drawOval(startPosX + 55, startPosY + 59, 13, 13);
g.drawOval(startPosX + 70, startPosY + 59, 13, 13);
g.drawOval(startPosX + 85, startPosY + 59, 13, 13);
g.drawOval(startPosX + 100, startPosY + 59, 13, 13);
/* два крайних на гусенице*/
g.fillOval(startPosX + 10, startPosY + 50, 25, 25);
g.fillOval(startPosX + 120, startPosY + 50, 25, 25);
}
if (_wheelsCount.count >= _wheelsCount.Six.count) {
Path2D path = new Path2D.Double();
path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 180, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 140, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 270, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 140, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 0, 90, Arc2D.OPEN), true);
path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 90, 90, Arc2D.OPEN), true);
path.closePath();
g.setColor(color1);
g.fill(path);
g.setColor(Color.BLACK);
g.draw(path);
/* наверху гусеницы*/
g.fillOval(startPosX + 47, startPosY + 44, 13, 13);
g.fillOval(startPosX + 62, startPosY + 44, 13, 13);
g.fillOval(startPosX + 77, startPosY + 44, 13, 13);
g.fillOval(startPosX + 92, startPosY + 44, 13, 13);
g.fillOval(startPosX + 107, startPosY + 44, 13, 13);
/* по середине гусеницы*/
g.drawOval(startPosX + 40, startPosY + 59, 13, 13);
g.drawOval(startPosX + 55, startPosY + 59, 13, 13);
g.drawOval(startPosX + 70, startPosY + 59, 13, 13);
g.drawOval(startPosX + 85, startPosY + 59, 13, 13);
g.drawOval(startPosX + 100, startPosY + 59, 13, 13);
g.drawOval(startPosX + 115, startPosY + 59, 13, 13);
/* два крайних на гусенице*/
g.fillOval(startPosX + 10, startPosY + 50, 25, 25);
g.fillOval(startPosX + 135, startPosY + 50, 25, 25);
}
}
}

3
src/DyrectionType.java Normal file
View File

@ -0,0 +1,3 @@
public enum DyrectionType {
Up, Down, Left, Right
}

24
src/EntityUsta.java Normal file
View File

@ -0,0 +1,24 @@
import java.awt.*;
public class EntityUsta {
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean BodyKit;
public boolean Pushka;
public double Step()
{
return (double) Speed * 100 / Weight;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
boolean bodyKit, boolean pushka)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Pushka = pushka;
}
}

View File

@ -1,5 +1,6 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
public static void main(String[] args)
{
MainFrameUsta mainFrameUsta = new MainFrameUsta();
}
}
}

18
src/MainFrameUsta.java Normal file
View File

@ -0,0 +1,18 @@
import javax.swing.*;
public class MainFrameUsta extends JFrame {
private SelfPropelledArtilleryUnit _formUsta;
public MainFrameUsta() {
super();
setTitle("ArtilleryUnit");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formUsta = new SelfPropelledArtilleryUnit();
setContentPane(_formUsta.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(500, 50);
setSize(500, 850);
pack();
setVisible(true);
}
}

View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="SelfPropelledArtilleryUnit">
<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="24" y="37" width="635" height="383"/>
</constraints>
<properties/>
<border type="none">
<font/>
</border>
<children>
<hspacer id="ce44b">
<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="17764">
<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="6cb47" 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>
<component id="bbbb" 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="1" indent="0" use-parent-layout="false">
<minimum-size width="50" height="50"/>
<preferred-size width="50" height="50"/>
<maximum-size width="50" height="50"/>
</grid>
</constraints>
<properties>
<enabled value="true"/>
<hideActionText value="false"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
<clientProperties>
<hideActionText class="java.lang.Boolean" value="false"/>
</clientProperties>
</component>
<component id="2fa92" 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="1" indent="0" use-parent-layout="false">
<minimum-size width="50" height="50"/>
<preferred-size width="50" height="50"/>
<maximum-size width="50" height="50"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
<component id="326c5" 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="50" height="50"/>
<preferred-size width="50" height="50"/>
<maximum-size width="50" height="50"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
<clientProperties>
<hideActionText class="java.lang.Boolean" value="false"/>
</clientProperties>
</component>
<component id="cc460" 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="1" indent="0" use-parent-layout="false">
<minimum-size width="50" height="50"/>
<preferred-size width="50" height="50"/>
<maximum-size width="50" height="50"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,78 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
public class SelfPropelledArtilleryUnit {
DrawingUsta _drawingUsta = new DrawingUsta();
private JButton buttonCreate;
private JPanel pictureBox;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
public JPanel getPictureBox() {
return pictureBox;
}
public SelfPropelledArtilleryUnit()
{
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
buttonCreate.addActionListener(e -> {
_drawingUsta = new DrawingUsta();
Random random = new Random();
_drawingUsta.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()
);
_drawingUsta.SetWheelsCount(random.nextInt(4, 7));
_drawingUsta.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
});
ActionListener buttonMoveClickedListener = e -> {
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp") -> {
_drawingUsta.MoveTransport(DyrectionType.Up);
}
case ("buttonDown") -> {
_drawingUsta.MoveTransport(DyrectionType.Down);
}
case ("buttonLeft") -> {
_drawingUsta.MoveTransport(DyrectionType.Left);
}
case ("buttonRight") -> {
_drawingUsta.MoveTransport(DyrectionType.Right);
}
}
Draw();
};
buttonUp.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonRight.addActionListener(buttonMoveClickedListener);
}
public void Draw() {
if (_drawingUsta.EntityUsta == null) {
return;
}
Graphics g = pictureBox.getGraphics();
pictureBox.paint(g);
_drawingUsta.DrawTransport((Graphics2D) g);
}
}

7
src/WheelsCount.java Normal file
View File

@ -0,0 +1,7 @@
public enum WheelsCount {
Four(4), Five(5), Six(6);
public final int count;
WheelsCount(int count) {
this.count = count;
}
}