Work is done

This commit is contained in:
Сергей Полевой 2022-09-25 19:20:16 +04:00
parent 05c9d6614e
commit 346581751d
10 changed files with 389 additions and 1 deletions

112
DrawingArtillery.java Normal file
View File

@ -0,0 +1,112 @@
import java.awt.*;
public class DrawingArtillery {
private EntityArtillery artillery;
private DrawingRollers drawingRollers;
private float _startPosX;
private float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private final int _artilleryWidth = 80;
private final int _artilleryHeight = 50;
public EntityArtillery getArtillery() {
return artillery;
}
public void Init(int speed, float weight, Color bodyColor, int rollersCount) {
artillery = new EntityArtillery();
artillery.Init(speed, weight, bodyColor);
drawingRollers = new DrawingRollers(rollersCount, bodyColor);
}
public void SetPosition(int x, int y, int width, int height) {
if (x < 0 || x + _artilleryWidth >= width)
{
return;
}
if (y < 0 || y + _artilleryHeight >= height)
{
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void moveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null)
{
return;
}
switch (direction)
{
case Right:
if (_startPosX + _artilleryWidth + artillery.getStep() < _pictureWidth)
{
_startPosX += artillery.getStep();
}
break;
case Left:
if (_startPosX - artillery.getStep() >= 0)
{
_startPosX -= artillery.getStep();
}
break;
case Up:
if (_startPosY - artillery.getStep() >= 0)
{
_startPosY -= artillery.getStep();
}
break;
case Down:
if (_startPosY + _artilleryHeight + artillery.getStep() < _pictureHeight)
{
_startPosY += artillery.getStep();
}
break;
}
}
public void drawTransport(Graphics2D g) {
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{
return;
}
g.setColor(artillery.getBodyColor() != null ? artillery.getBodyColor() : Color.BLACK);
g.fillRect((int) (_startPosX + _artilleryWidth / 8 * 2), (int) _startPosY, _artilleryWidth / 8 * 4, _artilleryWidth / 5);
g.fillRect((int) _startPosX, (int) (_startPosY + _artilleryHeight / 5), _artilleryWidth, _artilleryHeight / 3);
g.setColor(Color.GRAY);
g.fillOval((int) _startPosX, (int) (_startPosY + _artilleryHeight * 2 / 5), _artilleryWidth, _artilleryHeight * 2 / 5);
drawingRollers.draw(g, (int) _startPosX, (int) _startPosY, _artilleryWidth, _artilleryHeight);
}
public void changeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _artilleryWidth || _pictureHeight <= _artilleryHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _artilleryWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _artilleryWidth;
}
if (_startPosY + _artilleryHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _artilleryHeight;
}
}
}

39
DrawingRollers.java Normal file
View File

@ -0,0 +1,39 @@
import java.awt.*;
public class DrawingRollers {
private RollersCount rollersCount;
private Color color;
public DrawingRollers(int rollersCount, Color rollersColor) {
setRollersCount(rollersCount);
color = rollersColor;
}
public void setRollersCount(int num) {
if (num <= 4) {
rollersCount = RollersCount.Four;
} else if (num >= 6) {
rollersCount = RollersCount.Six;
} else {
rollersCount = RollersCount.Five;
}
}
public void draw(Graphics2D g, int x, int y, int artilleryWidth, int artilleryHeight) {
g.setColor(color != null ? color : Color.BLACK);
g.fillOval(x + artilleryWidth / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32);
g.fillOval(x + artilleryWidth * 15 / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32);
switch (rollersCount) {
case Six: {
g.fillOval(x + artilleryWidth * 8 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32);
}
case Five: {
g.fillOval(x + artilleryWidth * 10 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32);
}
case Four: {
g.fillOval(x + artilleryWidth * 5 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32);
g.fillOval(x + artilleryWidth * 12 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32);
}
}
}
}

152
FormArtillery.form Normal file
View File

@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormArtillery">
<grid id="27dc6" binding="artilleryPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" 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="664" height="436"/>
</constraints>
<properties>
<background color="-4210753"/>
<inheritsPopupMenu value="false"/>
<maximumSize width="640" height="480"/>
<minimumSize width="80" height="50"/>
<preferredSize width="640" height="480"/>
</properties>
<border type="none">
<font/>
<title-color color="-1908517"/>
</border>
<children>
<grid id="460f0" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="20"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="ec1f2" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="1ffd0" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="64b86" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="fa947" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="f89d0">
<constraints>
<grid row="1" column="2" 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="402e3">
<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>
<vspacer id="bbd8c">
<constraints>
<grid row="0" column="2" 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="271ef" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" 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>
<borderPainted value="true"/>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowUp.png"/>
<text value=""/>
</properties>
</component>
<component id="4b967" class="javax.swing.JButton" binding="buttonDown">
<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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowDown.png"/>
<text value=""/>
</properties>
</component>
<component id="2e663" class="javax.swing.JButton" binding="buttonLeft">
<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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowLeft.png"/>
<label value=""/>
<text value=""/>
</properties>
</component>
<component id="c2d76" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="5" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowRight.png"/>
<label value=""/>
<text value=""/>
</properties>
</component>
<component id="7bc68" class="javax.swing.JButton" binding="createButton">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<alignmentX value="0.0"/>
<margin top="0" left="0" bottom="0" right="0"/>
<text value="Создать"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

75
FormArtillery.java Normal file
View File

@ -0,0 +1,75 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormArtillery extends JFrame {
private JPanel artilleryPane;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JPanel pictureBox;
private JButton createButton;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private DrawingArtillery _artillery;
public FormArtillery() {
this.setTitle("Artillery");
this.setContentPane(artilleryPane);
createButton.addActionListener(e -> {
Random rnd = new Random();
_artillery = new DrawingArtillery();
_artillery.Init(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(
rnd.nextInt(0, 256),
rnd.nextInt(0, 256),
rnd.nextInt(0, 256)),
rnd.nextInt(4, 7)
);
_artillery.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed()));
weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight()));
colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB()));
repaint();
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (_artillery != null) _artillery.changeBorders(e.getComponent().getWidth(), e.getComponent().getHeight());
repaint();
}
});
buttonLeft.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Left);
repaint();
});
buttonRight.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Right);
repaint();
});
buttonUp.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Up);
repaint();
});
buttonDown.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Down);
repaint();
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) artilleryPane.getGraphics();
if (_artillery != null) {
_artillery.drawTransport(g2d);
}
}
}

10
Program.java Normal file
View File

@ -0,0 +1,10 @@
import javax.swing.*;
public class Program {
public static void main(String[] args) {
FormArtillery form = new FormArtillery();
form.setSize(640, 480);
form.setVisible(true);
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

BIN
Resources/ArrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
Resources/ArrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Resources/ArrowRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
Resources/ArrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -1,4 +1,4 @@
public enum WheelCount { public enum RollersCount {
Four, Four,
Five, Five,
Six Six