Добавлена отрисовка, движение и прочее

This commit is contained in:
ekallin 2023-09-14 20:54:49 +04:00
parent e8bd8ec31e
commit f940bf3dbc
11 changed files with 363 additions and 4 deletions

View File

@ -0,0 +1,169 @@
package ProjectElectricLocomotive;
import java.awt.*;
public class DrawingElectricLocomotive {
public EntityElectricLocomotive EntityElectricLocomotive;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _locoWidth = 150;
private final int _locoHeight = 50;
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor,
boolean horns, boolean seifbatteries, int width, int height)
{
if (width < _locoWidth || height < _locoHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityElectricLocomotive = new EntityElectricLocomotive();
EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns, seifbatteries);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _locoWidth > _pictureWidth)
{
x = 20;
}
if (y < 0 || y + _locoHeight > _pictureHeight)
{
y = 20;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DyrectionType direction){
if(EntityElectricLocomotive == null) return;
switch(direction)
{
case Up -> {
if(_startPosY - EntityElectricLocomotive.Step() >= 0)
_startPosY -= (int) EntityElectricLocomotive.Step();
}
case Down -> {
if(_startPosY + EntityElectricLocomotive.Step() + _locoHeight <= _pictureHeight)
_startPosY += (int) EntityElectricLocomotive.Step();
}
case Left -> {
if(_startPosX - EntityElectricLocomotive.Step() >= 0)
_startPosX -= (int) EntityElectricLocomotive.Step();
}
case Right -> {
if(_startPosX + EntityElectricLocomotive.Step() + _locoWidth <= _pictureWidth)
_startPosX += (int) EntityElectricLocomotive.Step();
}
}
}
public void DrawTransport(Graphics g) {
if (EntityElectricLocomotive == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Color bodyColor = EntityElectricLocomotive.BodyColor;
Color additionalColor = EntityElectricLocomotive.AdditionalColor;
Color blackBrush = Color.BLACK;
Color windowsColor = Color.BLUE;
if(EntityElectricLocomotive.Horns)
{
g2d.fillRect(_startPosX + 30, _startPosY + 15, 20, 5);
g2d.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10);
g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY);
g2d.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10);
g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY);
g2d.setColor(blackBrush);
}
if(EntityElectricLocomotive.SeifBatteries)
{
g2d.drawRect(_startPosX + 50, _startPosY + 25, 20, 10);
g2d.setColor(blackBrush);
}
//locomotive
Polygon loco = new Polygon();
loco.addPoint(_startPosX, _startPosY + 40);
loco.addPoint(_startPosX, _startPosY + 30);
loco.addPoint(_startPosX + 20, _startPosY + 20);
loco.addPoint(_startPosX + 70, _startPosY + 20);
loco.addPoint(_startPosX +80, _startPosY + 30);
loco.addPoint(_startPosX +80, _startPosY + 40);
loco.addPoint(_startPosX +75, _startPosY + 45);
loco.addPoint(_startPosX +5, _startPosY + 45);
loco.addPoint(_startPosX, _startPosY + 40);
g2d.setColor(blackBrush);
g2d.drawPolygon(loco);
g2d.setColor(bodyColor);
g2d.fillPolygon(loco);
// windows
Polygon window = new Polygon();
window.addPoint(_startPosX + 10, _startPosY + 30);
window.addPoint(_startPosX +15, _startPosY + 25);
window.addPoint(_startPosX + 20, _startPosY + 25);
window.addPoint(_startPosX + 20, _startPosY + 30);
window.addPoint(_startPosX +10, _startPosY + 30);
g2d.setColor(blackBrush);
g2d.drawPolygon(window);
g2d.setColor(windowsColor);
g2d.fillPolygon(window);
g2d.fillRect(_startPosX + 25, _startPosY + 25, 10, 5);
g2d.setColor(windowsColor);
g2d.drawRect(_startPosX + 25, _startPosY + 25, 10, 5);
g2d.setColor(blackBrush);
//locomotive
//обязательные колеса
//loco
g2d.fillOval(_startPosX + 10, _startPosY + 45, 5, 5);
g2d.fillOval(_startPosX + 25, _startPosY + 45, 5, 5);
g2d.fillOval(_startPosX + 50, _startPosY + 45, 5, 5);
g2d.fillOval(_startPosX + 65, _startPosY + 45, 5, 5);
g2d.setColor(EntityElectricLocomotive.BodyColor);
g2d.fillRect(_startPosX, _startPosY, _locoWidth, _locoHeight);
//telega
g2d.setColor(blackBrush);
g2d.fillOval(_startPosX + 95, _startPosY + 45, 5, 5);
g2d.fillOval(_startPosX + 140, _startPosY + 45, 5, 5);
//telejka
Polygon telega = new Polygon();
telega.addPoint(_startPosX + 90, _startPosY + 25);
telega.addPoint(_startPosX + 95, _startPosY + 20);
telega.addPoint(_startPosX + 145, _startPosY + 20);
telega.addPoint(_startPosX + 150, _startPosY + 25);
telega.addPoint(_startPosX + 150, _startPosY + 45);
telega.addPoint(_startPosX + 90, _startPosY + 45);
telega.addPoint(_startPosX + 90, _startPosY + 25);
g2d.setColor(additionalColor);
g2d.fillPolygon(telega);
g2d.setColor(blackBrush);
g2d.drawPolygon(telega);
//telejka
//телега окна
g2d.setColor(blackBrush);
g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40);
g2d.setColor(windowsColor); g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5);
g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5);
g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5);
}
}

View File

@ -4,7 +4,7 @@ import java.awt.*;
public class EntityElectricLocomotive {
public int Speed;
public int Weight;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean Horns;
@ -13,7 +13,7 @@ public class EntityElectricLocomotive {
{
return (double) Speed * 100 / Weight;
}
public void Init(int speed, int weight, Color bodyColor, Color additionalColor,
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
boolean horns, boolean seifBatteries)
{
Speed = speed;

View File

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.FormElectricLocomotive">
<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="639" height="400"/>
</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="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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="ProjectElectricLocomotive/img/arrowDown.png"/>
<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="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>
<icon value="ProjectElectricLocomotive/img/arrowLeft.png"/>
<text value=""/>
</properties>
</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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="ProjectElectricLocomotive/img/arrowRight.png"/>
<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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<enabled value="true"/>
<hideActionText value="false"/>
<icon value="ProjectElectricLocomotive/img/arrowUP.png"/>
<text value=""/>
</properties>
<clientProperties>
<hideActionText class="java.lang.Boolean" value="false"/>
</clientProperties>
</component>
<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>
</children>
</grid>
</form>

View File

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

View File

@ -3,6 +3,6 @@ package ProjectElectricLocomotive;
public class Main {
public static void main(String[] args)
{
System.out.println("hello");
}
}

View File

@ -0,0 +1,21 @@
package ProjectElectricLocomotive;
import javax.swing.*;
public class MainFrameElectricLocomotive extends JFrame {
private FormElectricLocomotive _formElectricLocomotive;
public MainFrameElectricLocomotive()
{
super("Электролокомотив");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formElectricLocomotive = new FormElectricLocomotive();
setContentPane(_formElectricLocomotive.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(500, 50);
pack();
setVisible(true);
}
}
// MainFrameStormtrooper mainFrameStormtrooper = new MainFrameStormtrooper();
// }
//}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-20" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" project-jdk-name="openjdk-20" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>