Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
70651df390 | ||
|
366c6e94bc | ||
|
e67810013b | ||
|
ae45a3a851 | ||
|
c84d016721 | ||
|
ed022a2dd6 | ||
|
7d64623ac1 | ||
|
aa1469a83d | ||
|
eb9938a360 | ||
|
1d873b4a41 | ||
|
c2fee6a8f0 |
@ -3,6 +3,7 @@
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/Resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
|
BIN
Resources/arrowDown.png
Normal file
BIN
Resources/arrowDown.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 415 B |
BIN
Resources/arrowLeft.png
Normal file
BIN
Resources/arrowLeft.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 411 B |
BIN
Resources/arrowRight.png
Normal file
BIN
Resources/arrowRight.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 352 B |
BIN
Resources/arrowUp.png
Normal file
BIN
Resources/arrowUp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 412 B |
@ -1,6 +0,0 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
|
11
src/projectMonorail/DirectionType.java
Normal file
11
src/projectMonorail/DirectionType.java
Normal file
@ -0,0 +1,11 @@
|
||||
package projectMonorail;
|
||||
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
|
||||
Down,
|
||||
|
||||
Left,
|
||||
|
||||
Right
|
||||
}
|
234
src/projectMonorail/DrawingObjects/DrawingLocomotive.java
Normal file
234
src/projectMonorail/DrawingObjects/DrawingLocomotive.java
Normal file
@ -0,0 +1,234 @@
|
||||
package projectMonorail.DrawingObjects;
|
||||
|
||||
import projectMonorail.DirectionType;
|
||||
import projectMonorail.Entities.*;
|
||||
import projectMonorail.Extras.*;
|
||||
import projectMonorail.MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingLocomotive {
|
||||
|
||||
private EntityLocomotive entityLocomotive;
|
||||
|
||||
public EntityLocomotive getEntityLocomotive() {
|
||||
return entityLocomotive;
|
||||
}
|
||||
|
||||
protected void setEntityLocomotive(EntityLocomotive entityLocomotive) {
|
||||
this.entityLocomotive = entityLocomotive;
|
||||
}
|
||||
|
||||
private IDrawingWheels drawingWheels;
|
||||
|
||||
private int pictureWidth;
|
||||
|
||||
private int pictureHeight;
|
||||
|
||||
protected int startPosX;
|
||||
|
||||
protected int startPosY;
|
||||
|
||||
protected int locomotiveWidth = 117;
|
||||
|
||||
protected int locomotiveHeight = 56;
|
||||
|
||||
public int getPosX() {
|
||||
return startPosX;
|
||||
}
|
||||
|
||||
public int getPosY() {
|
||||
return startPosY;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return locomotiveWidth;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return locomotiveHeight;
|
||||
}
|
||||
|
||||
public IMoveableObject getMoveableObject() {
|
||||
return new DrawingObjectLocomotive(this);
|
||||
}
|
||||
|
||||
public DrawingLocomotive(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) {
|
||||
if (width < locomotiveWidth || height < locomotiveHeight) {
|
||||
return;
|
||||
}
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
entityLocomotive = new EntityLocomotive(speed, weight, mainColor);
|
||||
Random rand = new Random();
|
||||
drawingWheels = switch (rand.nextInt(0, 3)) {
|
||||
case 0 -> new DrawingNormalWheels();
|
||||
case 1 -> new DrawingSquarePatternWheels();
|
||||
case 2 -> new DrawingVintageWheels();
|
||||
default -> new DrawingNormalWheels();
|
||||
};
|
||||
drawingWheels.setWheelNumber(wheelNumber);
|
||||
}
|
||||
|
||||
protected DrawingLocomotive(int speed, double weight, Color mainColor,
|
||||
int width, int height, int locoWidth, int locoHeight, int wheelNumber) {
|
||||
if (width < locoWidth || height < locoHeight) {
|
||||
return;
|
||||
}
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
locomotiveWidth = locoWidth;
|
||||
locomotiveHeight = locoHeight;
|
||||
entityLocomotive = new EntityLocomotive(speed, weight, mainColor);
|
||||
Random rand = new Random();
|
||||
drawingWheels = switch (rand.nextInt(0, 3)) {
|
||||
case 0 -> new DrawingNormalWheels();
|
||||
case 1 -> new DrawingSquarePatternWheels();
|
||||
case 2 -> new DrawingVintageWheels();
|
||||
default -> new DrawingNormalWheels();
|
||||
};
|
||||
drawingWheels.setWheelNumber(wheelNumber);
|
||||
}
|
||||
|
||||
public DrawingLocomotive(EntityLocomotive entityLocomotive, int width, int height, int wheelNumber,
|
||||
IDrawingWheels drawingWheels) {
|
||||
if (width < locomotiveWidth || height < locomotiveHeight) {
|
||||
return;
|
||||
}
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
this.entityLocomotive = entityLocomotive;
|
||||
this.drawingWheels = drawingWheels;
|
||||
drawingWheels.setWheelNumber(wheelNumber);
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y) {
|
||||
if (x < 0 || x + locomotiveWidth > pictureWidth) {
|
||||
x = 0;
|
||||
}
|
||||
if (y < 0 || y + locomotiveHeight > pictureHeight) {
|
||||
y = 0;
|
||||
}
|
||||
|
||||
startPosX = x;
|
||||
startPosY = y;
|
||||
}
|
||||
|
||||
public boolean canMove(DirectionType direction) {
|
||||
if (entityLocomotive == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return switch (direction) {
|
||||
case Left -> startPosX - entityLocomotive.getStep() > 0;
|
||||
case Up -> startPosY - entityLocomotive.getStep() > 0;
|
||||
case Right -> startPosX + locomotiveWidth + entityLocomotive.getStep() < pictureWidth;
|
||||
case Down -> startPosY + locomotiveHeight + entityLocomotive.getStep() < pictureHeight;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
public void moveTransport(DirectionType direction) {
|
||||
if (!canMove(direction) || entityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (direction) {
|
||||
case Left:
|
||||
startPosX -= (int) entityLocomotive.getStep();
|
||||
break;
|
||||
|
||||
case Up:
|
||||
startPosY -= (int) entityLocomotive.getStep();
|
||||
break;
|
||||
|
||||
case Right:
|
||||
startPosX += (int) entityLocomotive.getStep();
|
||||
break;
|
||||
|
||||
case Down:
|
||||
startPosY += (int) entityLocomotive.getStep();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawTransport(Graphics2D g2d) {
|
||||
if (entityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Color mainColor = entityLocomotive.getMainColor();
|
||||
BasicStroke standardWidth = new BasicStroke(1);
|
||||
BasicStroke largerWidth = new BasicStroke(2);
|
||||
|
||||
//надстройка
|
||||
g2d.setStroke(largerWidth);
|
||||
g2d.setColor(mainColor);
|
||||
g2d.fillRect(startPosX + 55, startPosY, 25, 15);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(startPosX + 55, startPosY, 25, 15);
|
||||
|
||||
//корпус локомотива
|
||||
Polygon locoPolygon = new Polygon();
|
||||
locoPolygon.addPoint(startPosX + 29, startPosY + 15);
|
||||
locoPolygon.addPoint(startPosX + 112, startPosY + 15);
|
||||
locoPolygon.addPoint(startPosX + 112, startPosY + 46);
|
||||
locoPolygon.addPoint(startPosX + 25, startPosY + 46);
|
||||
locoPolygon.addPoint(startPosX + 25, startPosY + 31);
|
||||
|
||||
g2d.setColor(mainColor);
|
||||
g2d.fillPolygon(locoPolygon);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(locoPolygon);
|
||||
|
||||
g2d.setStroke(standardWidth);
|
||||
g2d.setColor(Color.BLUE);
|
||||
g2d.drawLine(startPosX + 25, startPosY + 31, startPosX + 112, startPosY + 31);
|
||||
|
||||
//дверь локомотива
|
||||
g2d.setColor(Color.GRAY);
|
||||
g2d.fillRect(startPosX + 54, startPosY + 21, 7, 20);
|
||||
|
||||
g2d.setStroke(largerWidth);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(startPosX + 54, startPosY + 21, 7, 20);
|
||||
|
||||
//окна локомотива
|
||||
g2d.setColor(Color.BLUE);
|
||||
|
||||
g2d.fillRect(startPosX + 32, startPosY + 18, 6, 9);
|
||||
g2d.fillRect(startPosX + 44, startPosY + 18, 6, 9);
|
||||
g2d.fillRect(startPosX + 103, startPosY + 18, 6, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
g2d.drawRect(startPosX + 32, startPosY + 18, 6, 9);
|
||||
g2d.drawRect(startPosX + 44, startPosY + 18, 6, 9);
|
||||
g2d.drawRect(startPosX + 103, startPosY + 18, 6, 9);
|
||||
|
||||
//колеса и тележка локомотива
|
||||
g2d.fillRect(startPosX + 23, startPosY + 47, 33, 6);
|
||||
g2d.fillRect(startPosX + 76, startPosY + 47, 30, 6);
|
||||
g2d.drawRect(startPosX + 23, startPosY + 47, 33, 6);
|
||||
g2d.drawRect(startPosX + 76, startPosY + 47, 30, 6);
|
||||
|
||||
drawingWheels.drawWheels(g2d, Color.WHITE, startPosX, startPosY);
|
||||
|
||||
Polygon bogiePolygon = new Polygon();
|
||||
bogiePolygon.addPoint(startPosX + 26, startPosY + 46);
|
||||
bogiePolygon.addPoint(startPosX + 24, startPosY + 54);
|
||||
bogiePolygon.addPoint(startPosX + 12, startPosY + 54);
|
||||
bogiePolygon.addPoint(startPosX + 8, startPosY + 51);
|
||||
bogiePolygon.addPoint(startPosX + 12, startPosY + 48);
|
||||
bogiePolygon.addPoint(startPosX + 18, startPosY + 46);
|
||||
|
||||
g2d.fillPolygon(bogiePolygon);
|
||||
|
||||
//соединение между кабинами
|
||||
g2d.drawRect(startPosX + 112, startPosY + 18, 5, 28);
|
||||
g2d.fillRect(startPosX + 112, startPosY + 18, 5, 28);
|
||||
}
|
||||
}
|
105
src/projectMonorail/DrawingObjects/DrawingMonorail.java
Normal file
105
src/projectMonorail/DrawingObjects/DrawingMonorail.java
Normal file
@ -0,0 +1,105 @@
|
||||
package projectMonorail.DrawingObjects;
|
||||
|
||||
import projectMonorail.Entities.*;
|
||||
import projectMonorail.Extras.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingMonorail extends DrawingLocomotive {
|
||||
|
||||
public DrawingMonorail(int speed, double weight, Color mainColor, Color additionalColor,
|
||||
boolean magneticRail, boolean extraCabin, int width, int height, int wheelNumber) {
|
||||
super(speed, weight, mainColor, width, height, 186, 92, wheelNumber);
|
||||
|
||||
if (!magneticRail && !extraCabin) {
|
||||
locomotiveWidth = 117;
|
||||
locomotiveHeight = 56;
|
||||
}
|
||||
if (!magneticRail && extraCabin) {
|
||||
locomotiveWidth = 183;
|
||||
locomotiveHeight = 56;
|
||||
}
|
||||
|
||||
if (getEntityLocomotive() != null) {
|
||||
setEntityLocomotive(new EntityMonorail(speed, weight, mainColor,
|
||||
additionalColor, magneticRail, extraCabin));
|
||||
}
|
||||
}
|
||||
|
||||
public DrawingMonorail(EntityMonorail entityMonorail, int width, int height, int wheelNumber,
|
||||
IDrawingWheels drawingWheels) {
|
||||
super(entityMonorail, width, height, wheelNumber, drawingWheels);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawTransport(Graphics2D g2d) {
|
||||
if (!(getEntityLocomotive() instanceof EntityMonorail monorail)) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.drawTransport(g2d);
|
||||
|
||||
Color mainColor = monorail.getMainColor();
|
||||
Color additionalColor = monorail.getAdditionalColor();
|
||||
BasicStroke standardWidth = new BasicStroke(1);
|
||||
BasicStroke largerWidth = new BasicStroke(2);
|
||||
|
||||
//магнитная рельса
|
||||
if (monorail.getMagneticRail()) {
|
||||
g2d.drawRect(startPosX + 2, startPosY + 58, 184, 18);
|
||||
g2d.setColor(Color.GRAY);
|
||||
g2d.fillRect(startPosX + 2, startPosY + 58, 184, 18);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(startPosX + 35 + 35 * i, startPosY + 77, 8, 15);
|
||||
g2d.setColor(Color.GRAY);
|
||||
g2d.fillRect(startPosX + 35 + 35 * i, startPosY + 77, 8, 15);
|
||||
}
|
||||
}
|
||||
|
||||
//дополнительная кабина
|
||||
if (monorail.getExtraCabin()) {
|
||||
//корпус дополнительной кабины
|
||||
g2d.setColor(additionalColor);
|
||||
g2d.fillRect(startPosX + 118, startPosY + 15, 65, 31);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(startPosX + 118, startPosY + 15, 65, 31);
|
||||
|
||||
g2d.setStroke(standardWidth);
|
||||
g2d.setColor(Color.BLUE);
|
||||
g2d.drawLine(startPosX + 118, startPosY + 31, startPosX + 183, startPosY + 31);
|
||||
|
||||
//дверь дополнительной кабины
|
||||
g2d.setColor(Color.GRAY);
|
||||
g2d.fillRect(startPosX + 146, startPosY + 21, 7, 20);
|
||||
|
||||
g2d.setStroke(largerWidth);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(startPosX + 146, startPosY + 21, 7, 20);
|
||||
|
||||
//окна дополнительной кабины
|
||||
g2d.setColor(Color.BLUE);
|
||||
g2d.fillRect(startPosX + 130, startPosY + 18, 6, 9);
|
||||
g2d.fillRect(startPosX + 169, startPosY + 18, 6, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(startPosX + 130, startPosY + 18, 6, 9);
|
||||
g2d.drawRect(startPosX + 169, startPosY + 18, 6, 9);
|
||||
|
||||
//колеса и тележка дополнительной кабины
|
||||
g2d.fillRect(startPosX + 126, startPosY + 47, 15, 6);
|
||||
g2d.fillRect(startPosX + 159, startPosY + 47, 15, 6);
|
||||
g2d.drawRect(startPosX + 126, startPosY + 47, 15, 6);
|
||||
g2d.drawRect(startPosX + 159, startPosY + 47, 15, 6);
|
||||
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillOval(startPosX + 128, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 161, startPosY + 47, 10, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(startPosX + 128, startPosY + 47, 10, 9);
|
||||
g2d.drawOval(startPosX + 161, startPosY + 47, 10, 9);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
34
src/projectMonorail/Entities/EntityLocomotive.java
Normal file
34
src/projectMonorail/Entities/EntityLocomotive.java
Normal file
@ -0,0 +1,34 @@
|
||||
package projectMonorail.Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLocomotive {
|
||||
|
||||
private int speed;
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
private double weight;
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
private Color mainColor;
|
||||
|
||||
public Color getMainColor() {
|
||||
return mainColor;
|
||||
}
|
||||
|
||||
public double getStep() {
|
||||
return (double) speed * 100 / weight;
|
||||
}
|
||||
|
||||
public EntityLocomotive(int speed, double weight, Color mainColor) {
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.mainColor = mainColor;
|
||||
}
|
||||
}
|
32
src/projectMonorail/Entities/EntityMonorail.java
Normal file
32
src/projectMonorail/Entities/EntityMonorail.java
Normal file
@ -0,0 +1,32 @@
|
||||
package projectMonorail.Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityMonorail extends EntityLocomotive {
|
||||
|
||||
private Color additionalColor;
|
||||
|
||||
public Color getAdditionalColor() {
|
||||
return additionalColor;
|
||||
}
|
||||
|
||||
private boolean magneticRail;
|
||||
|
||||
public boolean getMagneticRail() {
|
||||
return magneticRail;
|
||||
}
|
||||
|
||||
private boolean extraCabin;
|
||||
|
||||
public boolean getExtraCabin() {
|
||||
return extraCabin;
|
||||
}
|
||||
|
||||
public EntityMonorail(int speed, double weight, Color mainColor, Color
|
||||
additionalColor, boolean magneticRail, boolean extraCabin) {
|
||||
super(speed, weight, mainColor);
|
||||
this.additionalColor = additionalColor;
|
||||
this.magneticRail = magneticRail;
|
||||
this.extraCabin = extraCabin;
|
||||
}
|
||||
}
|
105
src/projectMonorail/Extras/DrawingNormalWheels.java
Normal file
105
src/projectMonorail/Extras/DrawingNormalWheels.java
Normal file
@ -0,0 +1,105 @@
|
||||
package projectMonorail.Extras;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingNormalWheels implements IDrawingWheels {
|
||||
|
||||
private WheelNumber wheelNumber;
|
||||
|
||||
@Override
|
||||
public WheelNumber getWheelNumber() {
|
||||
return wheelNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWheelNumber(int number) {
|
||||
switch (number) {
|
||||
case 2:
|
||||
wheelNumber = WheelNumber.Two;
|
||||
break;
|
||||
case 3:
|
||||
wheelNumber = WheelNumber.Three;
|
||||
break;
|
||||
case 4:
|
||||
wheelNumber = WheelNumber.Four;
|
||||
break;
|
||||
default:
|
||||
wheelNumber = WheelNumber.Two;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
switch (wheelNumber) {
|
||||
case Two:
|
||||
drawTwoWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
case Three:
|
||||
drawThreeWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
case Four:
|
||||
drawFourWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void drawTwoWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 45, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 75, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 95, startPosY + 47, 10, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
g2d.drawOval(startPosX + 25, startPosY + 47, 10, 9);
|
||||
g2d.drawOval(startPosX + 45, startPosY + 47, 10, 9);
|
||||
g2d.drawOval(startPosX + 75, startPosY + 47, 10, 9);
|
||||
g2d.drawOval(startPosX + 95, startPosY + 47, 10, 9);
|
||||
}
|
||||
|
||||
private void drawThreeWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 36, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 47, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 76, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 87, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 98, startPosY + 47, 8, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
g2d.drawOval(startPosX + 25, startPosY + 47, 8, 9);
|
||||
g2d.drawOval(startPosX + 36, startPosY + 47, 8, 9);
|
||||
g2d.drawOval(startPosX + 47, startPosY + 47, 8, 9);
|
||||
g2d.drawOval(startPosX + 76, startPosY + 47, 8, 9);
|
||||
g2d.drawOval(startPosX + 87, startPosY + 47, 8, 9);
|
||||
g2d.drawOval(startPosX + 98, startPosY + 47, 8, 9);
|
||||
}
|
||||
|
||||
private void drawFourWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 33, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 41, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 49, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 75, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 83, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 91, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 99, startPosY + 47, 6, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
g2d.drawOval(startPosX + 25, startPosY + 47, 6, 9);
|
||||
g2d.drawOval(startPosX + 33, startPosY + 47, 6, 9);
|
||||
g2d.drawOval(startPosX + 41, startPosY + 47, 6, 9);
|
||||
g2d.drawOval(startPosX + 49, startPosY + 47, 6, 9);
|
||||
g2d.drawOval(startPosX + 76, startPosY + 47, 6, 9);
|
||||
g2d.drawOval(startPosX + 84, startPosY + 47, 6, 9);
|
||||
g2d.drawOval(startPosX + 92, startPosY + 47, 6, 9);
|
||||
g2d.drawOval(startPosX + 100, startPosY + 47, 6, 9);
|
||||
}
|
||||
}
|
114
src/projectMonorail/Extras/DrawingSquarePatternWheels.java
Normal file
114
src/projectMonorail/Extras/DrawingSquarePatternWheels.java
Normal file
@ -0,0 +1,114 @@
|
||||
package projectMonorail.Extras;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingSquarePatternWheels implements IDrawingWheels {
|
||||
|
||||
private WheelNumber wheelNumber;
|
||||
|
||||
@Override
|
||||
public WheelNumber getWheelNumber() {
|
||||
return wheelNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWheelNumber(int number) {
|
||||
switch (number) {
|
||||
case 2:
|
||||
wheelNumber = WheelNumber.Two;
|
||||
break;
|
||||
case 3:
|
||||
wheelNumber = WheelNumber.Three;
|
||||
break;
|
||||
case 4:
|
||||
wheelNumber = WheelNumber.Four;
|
||||
break;
|
||||
default:
|
||||
wheelNumber = WheelNumber.Two;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
switch (wheelNumber) {
|
||||
case Two:
|
||||
drawTwoWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
case Three:
|
||||
drawThreeWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
case Four:
|
||||
drawFourWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void drawTwoWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 45, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 75, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 95, startPosY + 47, 10, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
drawWheel(g2d, startPosX, startPosY, 25, 47, 10, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 45, 47, 10, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 75, 47, 10, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 95, 47, 10, 9);
|
||||
}
|
||||
|
||||
private void drawThreeWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 36, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 47, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 76, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 87, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 98, startPosY + 47, 8, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
drawWheel(g2d, startPosX, startPosY, 25, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 36, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 47, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 76, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 87, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 98, 47, 8, 9);
|
||||
}
|
||||
|
||||
private void drawFourWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 33, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 41, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 49, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 75, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 83, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 91, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 99, startPosY + 47, 6, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
drawWheel(g2d, startPosX, startPosY, 25, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 33, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 41, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 49, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 75, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 83, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 91, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 99, 47, 6, 9);
|
||||
}
|
||||
|
||||
private void drawWheel(Graphics2D g2d, int startPosX, int startPosY, int xChange, int yChange, int width, int height) {
|
||||
g2d.drawOval(startPosX + xChange, startPosY + yChange, width, height);
|
||||
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.fillRect((int)(startPosX + xChange + width * 0.335), startPosY + yChange + height/3, width/2, height/2);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
}
|
||||
}
|
117
src/projectMonorail/Extras/DrawingVintageWheels.java
Normal file
117
src/projectMonorail/Extras/DrawingVintageWheels.java
Normal file
@ -0,0 +1,117 @@
|
||||
package projectMonorail.Extras;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingVintageWheels implements IDrawingWheels {
|
||||
|
||||
private WheelNumber wheelNumber;
|
||||
|
||||
@Override
|
||||
public WheelNumber getWheelNumber() {
|
||||
return wheelNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWheelNumber(int number) {
|
||||
switch (number) {
|
||||
case 2:
|
||||
wheelNumber = WheelNumber.Two;
|
||||
break;
|
||||
case 3:
|
||||
wheelNumber = WheelNumber.Three;
|
||||
break;
|
||||
case 4:
|
||||
wheelNumber = WheelNumber.Four;
|
||||
break;
|
||||
default:
|
||||
wheelNumber = WheelNumber.Two;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
switch (wheelNumber) {
|
||||
case Two:
|
||||
drawTwoWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
case Three:
|
||||
drawThreeWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
case Four:
|
||||
drawFourWheels(g2d, color, startPosX, startPosY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void drawTwoWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 45, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 75, startPosY + 47, 10, 9);
|
||||
g2d.fillOval(startPosX + 95, startPosY + 47, 10, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
drawWheel(g2d, startPosX, startPosY, 25, 47, 10, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 45, 47, 10, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 75, 47, 10, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 95, 47, 10, 9);
|
||||
}
|
||||
|
||||
private void drawThreeWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 36, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 47, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 76, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 87, startPosY + 47, 8, 9);
|
||||
g2d.fillOval(startPosX + 98, startPosY + 47, 8, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
drawWheel(g2d, startPosX, startPosY, 25, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 36, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 47, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 76, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 87, 47, 8, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 98, 47, 8, 9);
|
||||
}
|
||||
|
||||
private void drawFourWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
|
||||
g2d.setColor(color);
|
||||
|
||||
g2d.fillOval(startPosX + 25, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 33, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 41, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 49, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 75, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 83, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 91, startPosY + 47, 6, 9);
|
||||
g2d.fillOval(startPosX + 99, startPosY + 47, 6, 9);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
|
||||
drawWheel(g2d, startPosX, startPosY, 25, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 33, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 41, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 49, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 75, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 83, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 91, 47, 6, 9);
|
||||
drawWheel(g2d, startPosX, startPosY, 99, 47, 6, 9);
|
||||
}
|
||||
|
||||
private void drawWheel(Graphics2D g2d, int startPosX, int startPosY, int xChange, int yChange, int width, int height) {
|
||||
g2d.setStroke(new BasicStroke(1));
|
||||
g2d.drawOval(startPosX + xChange, startPosY + yChange, width, height);
|
||||
|
||||
g2d.drawLine(startPosX + xChange + width/2, startPosY + yChange, startPosX + xChange + width/2, startPosY + yChange + height);
|
||||
g2d.drawLine(startPosX + xChange, startPosY + yChange + height/2, startPosX + xChange + width, startPosY + yChange + height/2);
|
||||
g2d.drawLine(startPosX + xChange + width/8, startPosY + yChange + height/8,
|
||||
startPosX + xChange + width * 7/8, startPosY + yChange + height * 7/8);
|
||||
g2d.drawLine(startPosX + xChange + width * 7/8, startPosY + yChange + height/8,
|
||||
startPosX + xChange + width/8, startPosY + yChange + height * 7/8);
|
||||
}
|
||||
}
|
11
src/projectMonorail/Extras/IDrawingWheels.java
Normal file
11
src/projectMonorail/Extras/IDrawingWheels.java
Normal file
@ -0,0 +1,11 @@
|
||||
package projectMonorail.Extras;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingWheels {
|
||||
void setWheelNumber(int number);
|
||||
|
||||
WheelNumber getWheelNumber();
|
||||
|
||||
void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY);
|
||||
}
|
58
src/projectMonorail/Extras/RandomGeneric.java
Normal file
58
src/projectMonorail/Extras/RandomGeneric.java
Normal file
@ -0,0 +1,58 @@
|
||||
package projectMonorail.Extras;
|
||||
|
||||
import projectMonorail.DrawingObjects.DrawingLocomotive;
|
||||
import projectMonorail.DrawingObjects.DrawingMonorail;
|
||||
import projectMonorail.Entities.*;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class RandomGeneric<E extends EntityLocomotive, D extends IDrawingWheels> {
|
||||
private final E[] locomotives;
|
||||
|
||||
private int locomotivesCurrNum = 0;
|
||||
|
||||
private final D[] wheels;
|
||||
|
||||
private int wheelsCurrNum = 0;
|
||||
|
||||
private int pictureWidth;
|
||||
|
||||
private int pictureHeight;
|
||||
|
||||
public RandomGeneric(int locomotivesCount, int drawingsCount, int pictureWidth, int pictureHeight) {
|
||||
locomotives = (E[])new EntityLocomotive[locomotivesCount];
|
||||
wheels = (D[])new IDrawingWheels[drawingsCount];
|
||||
this.pictureWidth = pictureWidth;
|
||||
this.pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
public void addItem(E locomotive) {
|
||||
if (locomotive == null || locomotivesCurrNum >= locomotives.length)
|
||||
return;
|
||||
|
||||
locomotives[locomotivesCurrNum++] = locomotive;
|
||||
}
|
||||
|
||||
public void addItem(D drawingWheels) {
|
||||
if (drawingWheels == null || wheelsCurrNum >= wheels.length)
|
||||
return;
|
||||
|
||||
wheels[wheelsCurrNum++] = drawingWheels;
|
||||
}
|
||||
|
||||
public DrawingLocomotive getDrawingLocomotive() {
|
||||
if (locomotivesCurrNum == 0 || wheelsCurrNum == 0)
|
||||
return null;
|
||||
|
||||
EntityLocomotive locomotive = locomotives[ThreadLocalRandom.current().nextInt(0, locomotivesCurrNum)];
|
||||
IDrawingWheels drawingWheels = wheels[ThreadLocalRandom.current().nextInt(0, wheelsCurrNum)];
|
||||
|
||||
if (locomotive instanceof EntityMonorail) {
|
||||
return new DrawingMonorail((EntityMonorail) locomotive, pictureWidth, pictureHeight, ThreadLocalRandom.current().nextInt(2, 5),
|
||||
drawingWheels);
|
||||
}
|
||||
|
||||
return new DrawingLocomotive(locomotive, pictureWidth, pictureHeight, ThreadLocalRandom.current().nextInt(2, 5),
|
||||
drawingWheels);
|
||||
}
|
||||
}
|
9
src/projectMonorail/Extras/WheelNumber.java
Normal file
9
src/projectMonorail/Extras/WheelNumber.java
Normal file
@ -0,0 +1,9 @@
|
||||
package projectMonorail.Extras;
|
||||
|
||||
public enum WheelNumber {
|
||||
Two,
|
||||
|
||||
Three,
|
||||
|
||||
Four
|
||||
}
|
26
src/projectMonorail/FrameMonorail.java
Normal file
26
src/projectMonorail/FrameMonorail.java
Normal file
@ -0,0 +1,26 @@
|
||||
package projectMonorail;
|
||||
|
||||
import projectMonorail.DrawingObjects.DrawingLocomotive;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class FrameMonorail extends JDialog {
|
||||
|
||||
private PictureBox pictureBox;
|
||||
|
||||
public PictureBox getPictureBox() {
|
||||
return pictureBox;
|
||||
}
|
||||
|
||||
public FrameMonorail(JFrame parent, DrawingLocomotive locomotive) {
|
||||
super(parent ,"Monorail", true);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
|
||||
pictureBox = new PictureBox(locomotive);
|
||||
add(pictureBox);
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
336
src/projectMonorail/FrameMonorailCollection.java
Normal file
336
src/projectMonorail/FrameMonorailCollection.java
Normal file
@ -0,0 +1,336 @@
|
||||
package projectMonorail;
|
||||
|
||||
import projectMonorail.DrawingObjects.*;
|
||||
import projectMonorail.Generics.*;
|
||||
import projectMonorail.MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.NumberFormatter;
|
||||
import java.awt.*;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class FrameMonorailCollection extends JFrame {
|
||||
|
||||
private final LocomotivesGenericStorage storage;
|
||||
|
||||
private Queue<DrawingLocomotive> removedLocomotives;
|
||||
|
||||
private JPanel paddingTopPanel;
|
||||
|
||||
private JPanel mainPanel;
|
||||
|
||||
private PictureBoxCollection pictureBoxCollection;
|
||||
|
||||
private int pictureBoxCollectionWidth = 900;
|
||||
|
||||
private int pictureBoxCollectionHeight = 580;
|
||||
|
||||
private JPanel toolsPanel;
|
||||
|
||||
private JPanel setsPanel;
|
||||
|
||||
private JLabel nameToolsLabel;
|
||||
|
||||
private JLabel nameSetsLabel;
|
||||
|
||||
private JButton buttonAddMonorail;
|
||||
|
||||
private JButton buttonRemoveMonorail;
|
||||
|
||||
private JButton buttonRefreshCollection;
|
||||
|
||||
private JButton buttonRandomGeneration;
|
||||
|
||||
private JButton buttonAddSet;
|
||||
|
||||
private JButton buttonDelSet;
|
||||
|
||||
private JButton buttonShowRemoved;
|
||||
|
||||
private JTextField textFieldNameSet;
|
||||
|
||||
private JFormattedTextField textFieldNumber;
|
||||
|
||||
private JList<String> listSets;
|
||||
|
||||
private DefaultListModel<String> listModel;
|
||||
|
||||
public FrameMonorailCollection() {
|
||||
super("Monorail collection");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
listModel = new DefaultListModel<>();
|
||||
listSets = new JList<>(listModel);
|
||||
listSets.setMaximumSize(new Dimension(145, 94));
|
||||
listSets.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
listSets.addListSelectionListener(e -> {
|
||||
repaint();
|
||||
});
|
||||
|
||||
storage = new LocomotivesGenericStorage(pictureBoxCollectionWidth, pictureBoxCollectionHeight);
|
||||
pictureBoxCollection = new PictureBoxCollection(storage, listSets, pictureBoxCollectionWidth,
|
||||
pictureBoxCollectionHeight);
|
||||
|
||||
removedLocomotives = new LinkedList<>();
|
||||
|
||||
nameToolsLabel = new JLabel(" Tools");
|
||||
nameToolsLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
|
||||
nameToolsLabel.setHorizontalTextPosition(JLabel.LEFT);
|
||||
nameToolsLabel.setMaximumSize(new Dimension(190, 30));
|
||||
nameToolsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
nameSetsLabel = new JLabel(" Sets");
|
||||
nameSetsLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
|
||||
nameSetsLabel.setHorizontalTextPosition(JLabel.LEFT);
|
||||
nameSetsLabel.setMaximumSize(new Dimension(163, 30));
|
||||
nameSetsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonAddMonorail = new JButton("Add monorail");
|
||||
buttonAddMonorail.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonAddMonorail.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonAddMonorail.setFocusable(false);
|
||||
buttonAddMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonAddMonorail.setBackground(Color.WHITE);
|
||||
buttonAddMonorail.setMaximumSize(new Dimension(155, 36));
|
||||
buttonAddMonorail.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonAddMonorail.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonAddMonorail.addActionListener(e -> {
|
||||
if (listSets.getSelectedIndex() == -1)
|
||||
return;
|
||||
|
||||
var obj = storage.get(listSets.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
|
||||
FrameMonorail frame = new FrameMonorail(this, null);
|
||||
if (frame.getPictureBox().getDialogResult()) {
|
||||
if (obj.addition(frame.getPictureBox().getSelectedLocomotive()) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Object added", "Message",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
pictureBoxCollection.repaint();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Failed to add the object", "Message",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
NumberFormat format = NumberFormat.getInstance();
|
||||
NumberFormatter formatter = new NumberFormatter(format);
|
||||
formatter.setValueClass(Integer.class);
|
||||
formatter.setMinimum(0);
|
||||
formatter.setMaximum(Integer.MAX_VALUE);
|
||||
formatter.setAllowsInvalid(false);
|
||||
formatter.setCommitsOnValidEdit(true);
|
||||
|
||||
textFieldNumber = new JFormattedTextField(formatter);
|
||||
textFieldNumber.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
textFieldNumber.setMaximumSize(new Dimension(155, 25));
|
||||
textFieldNumber.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
textFieldNameSet = new JTextField();
|
||||
textFieldNameSet.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
textFieldNameSet.setMaximumSize(new Dimension(145, 25));
|
||||
textFieldNameSet.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonRemoveMonorail = new JButton("Remove monorail");
|
||||
buttonRemoveMonorail.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonRemoveMonorail.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonRemoveMonorail.setFocusable(false);
|
||||
buttonRemoveMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonRemoveMonorail.setBackground(Color.WHITE);
|
||||
buttonRemoveMonorail.setMaximumSize(new Dimension(155, 36));
|
||||
buttonRemoveMonorail.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonRemoveMonorail.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonRemoveMonorail.addActionListener(e -> {
|
||||
if (listSets.getSelectedIndex() == -1)
|
||||
return;
|
||||
|
||||
var obj = storage.get(listSets.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
|
||||
if (JOptionPane.showConfirmDialog(null, "Delete the object?", "Deletion",
|
||||
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
|
||||
return;
|
||||
}
|
||||
if (textFieldNumber.getText().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||
var removedLocomotive = obj.subtraction(pos);
|
||||
if (removedLocomotive != null) {
|
||||
removedLocomotives.add(removedLocomotive);
|
||||
JOptionPane.showMessageDialog(null, "Object deleted", "Message",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
pictureBoxCollection.repaint();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Failed to delete the object", "Message",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
buttonRefreshCollection = new JButton("Refresh collection");
|
||||
buttonRefreshCollection.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonRefreshCollection.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonRefreshCollection.setFocusable(false);
|
||||
buttonRefreshCollection.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonRefreshCollection.setBackground(Color.WHITE);
|
||||
buttonRefreshCollection.setMaximumSize(new Dimension(155, 36));
|
||||
buttonRefreshCollection.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonRefreshCollection.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonRefreshCollection.addActionListener(e -> {
|
||||
if (listSets.getSelectedIndex() == -1)
|
||||
return;
|
||||
|
||||
var obj = storage.get(listSets.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
|
||||
pictureBoxCollection.repaint();
|
||||
});
|
||||
|
||||
buttonRandomGeneration = new JButton("Show additional frame");
|
||||
buttonRandomGeneration.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonRandomGeneration.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonRandomGeneration.setFocusable(false);
|
||||
buttonRandomGeneration.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonRandomGeneration.setBackground(Color.WHITE);
|
||||
buttonRandomGeneration.setMaximumSize(new Dimension(155, 36));
|
||||
buttonRandomGeneration.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonRandomGeneration.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonRandomGeneration.addActionListener(e -> new FrameRandomGeneration(this));
|
||||
|
||||
buttonShowRemoved = new JButton("Show deleted item");
|
||||
buttonShowRemoved.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonShowRemoved.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonShowRemoved.setFocusable(false);
|
||||
buttonShowRemoved.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonShowRemoved.setBackground(Color.WHITE);
|
||||
buttonShowRemoved.setMaximumSize(new Dimension(155, 36));
|
||||
buttonShowRemoved.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonShowRemoved.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonShowRemoved.addActionListener(e -> {
|
||||
if (removedLocomotives.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(null, "The queue is empty", "Message", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
new FrameMonorail(this, removedLocomotives.remove());
|
||||
});
|
||||
|
||||
buttonAddSet = new JButton("Add set");
|
||||
buttonAddSet.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonAddSet.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonAddSet.setFocusable(false);
|
||||
buttonAddSet.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonAddSet.setBackground(Color.WHITE);
|
||||
buttonAddSet.setMaximumSize(new Dimension(145, 36));
|
||||
buttonAddSet.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonAddSet.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonAddSet.addActionListener(e -> {
|
||||
if (textFieldNameSet.getText() == null || textFieldNameSet.getText().isEmpty()) {
|
||||
JOptionPane.showMessageDialog(null, "Not all data is filled in", "Error",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
storage.addSet(textFieldNameSet.getText());
|
||||
reloadObjects();
|
||||
});
|
||||
|
||||
buttonDelSet = new JButton("Remove set");
|
||||
buttonDelSet.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonDelSet.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonDelSet.setFocusable(false);
|
||||
buttonDelSet.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonDelSet.setBackground(Color.WHITE);
|
||||
buttonDelSet.setMaximumSize(new Dimension(145, 36));
|
||||
buttonDelSet.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonDelSet.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonDelSet.addActionListener(e -> {
|
||||
if (listSets.getSelectedIndex() == -1)
|
||||
return;
|
||||
if (JOptionPane.showConfirmDialog(null, "Delete the set " +
|
||||
listSets.getSelectedValue() + "?", "Deletion", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
|
||||
storage.delSet(listSets.getSelectedValue());
|
||||
reloadObjects();
|
||||
}
|
||||
});
|
||||
|
||||
setsPanel = new JPanel();
|
||||
setsPanel.setMaximumSize(new Dimension(163, 292));
|
||||
setsPanel.setLayout(new BoxLayout(setsPanel, BoxLayout.Y_AXIS));
|
||||
setsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
|
||||
setsPanel.add(nameSetsLabel);
|
||||
setsPanel.add(Box.createVerticalStrut(13));
|
||||
setsPanel.add(textFieldNameSet);
|
||||
setsPanel.add(Box.createVerticalStrut(13));
|
||||
setsPanel.add(buttonAddSet);
|
||||
setsPanel.add(Box.createVerticalStrut(13));
|
||||
setsPanel.add(listSets);
|
||||
setsPanel.add(Box.createVerticalStrut(13));
|
||||
setsPanel.add(buttonDelSet);
|
||||
setsPanel.add(Box.createVerticalStrut(5));
|
||||
|
||||
toolsPanel = new JPanel();
|
||||
toolsPanel.setMaximumSize(new Dimension(190, 620));
|
||||
toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.Y_AXIS));
|
||||
toolsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
|
||||
toolsPanel.add(nameToolsLabel);
|
||||
toolsPanel.add(Box.createVerticalStrut(14));
|
||||
toolsPanel.add(setsPanel);
|
||||
toolsPanel.add(Box.createVerticalStrut(14));
|
||||
toolsPanel.add(buttonAddMonorail);
|
||||
toolsPanel.add(Box.createVerticalStrut(30));
|
||||
toolsPanel.add(textFieldNumber);
|
||||
toolsPanel.add(Box.createVerticalStrut(6));
|
||||
toolsPanel.add(buttonRemoveMonorail);
|
||||
toolsPanel.add(Box.createVerticalStrut(30));
|
||||
toolsPanel.add(buttonRefreshCollection);
|
||||
toolsPanel.add(Box.createVerticalStrut(40));
|
||||
toolsPanel.add(buttonShowRemoved);
|
||||
toolsPanel.add(Box.createVerticalStrut(14));
|
||||
toolsPanel.add(buttonRandomGeneration);
|
||||
|
||||
mainPanel = new JPanel();
|
||||
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
|
||||
mainPanel.add(Box.createHorizontalStrut(10));
|
||||
mainPanel.add(pictureBoxCollection);
|
||||
mainPanel.add(Box.createHorizontalStrut(15));
|
||||
mainPanel.add(toolsPanel);
|
||||
|
||||
paddingTopPanel = new JPanel();
|
||||
paddingTopPanel.setLayout(new BoxLayout(paddingTopPanel, BoxLayout.Y_AXIS));
|
||||
paddingTopPanel.add(Box.createVerticalStrut(15));
|
||||
paddingTopPanel.add(mainPanel);
|
||||
|
||||
add(paddingTopPanel);
|
||||
|
||||
setPreferredSize(new Dimension(990, 664));
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void reloadObjects() {
|
||||
int index = listSets.getSelectedIndex();
|
||||
listModel.clear();
|
||||
for (int i = 0; i < storage.keys().size(); i++) {
|
||||
listModel.addElement(storage.keys().get(i));
|
||||
}
|
||||
if (!listModel.isEmpty() && (index == -1 || index >= listModel.size())) {
|
||||
listSets.setSelectedIndex(0);
|
||||
}
|
||||
else if (!listModel.isEmpty() && index > -1 && index < listModel.size()) {
|
||||
listSets.setSelectedIndex(index);
|
||||
}
|
||||
}
|
||||
}
|
20
src/projectMonorail/FrameRandomGeneration.java
Normal file
20
src/projectMonorail/FrameRandomGeneration.java
Normal file
@ -0,0 +1,20 @@
|
||||
package projectMonorail;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class FrameRandomGeneration extends JDialog {
|
||||
|
||||
private PictureBoxRandomGeneration pictureBox;
|
||||
|
||||
public FrameRandomGeneration(JFrame parent) {
|
||||
super(parent ,"Random generation", true);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
|
||||
pictureBox = new PictureBoxRandomGeneration(400, 400);
|
||||
add(pictureBox);
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package projectMonorail.Generics;
|
||||
import projectMonorail.DrawingObjects.*;
|
||||
import projectMonorail.MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class LocomotivesGenericCollection<T extends DrawingLocomotive, U extends IMoveableObject> {
|
||||
private final int pictureWidth;
|
||||
|
||||
private final int pictureHeight;
|
||||
|
||||
private final int placeSizeWidth = 193;
|
||||
|
||||
private final int placeSizeHeight = 102;
|
||||
|
||||
private final SetGeneric<T> collection;
|
||||
|
||||
public LocomotivesGenericCollection(int picWidth, int picHeight) {
|
||||
int width = picWidth / placeSizeWidth;
|
||||
int height = picHeight / placeSizeHeight;
|
||||
pictureWidth = picWidth;
|
||||
pictureHeight = picHeight;
|
||||
collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
|
||||
public int addition(T obj) {
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return collection.insert(obj);
|
||||
}
|
||||
|
||||
public T subtraction(int pos) {
|
||||
T obj = collection.get(pos);
|
||||
if (obj != null)
|
||||
{
|
||||
return collection.remove(pos);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public U getU(int pos) {
|
||||
if (collection.get(pos) != null)
|
||||
return (U)collection.get(pos).getMoveableObject();
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showLocomotives(Graphics2D g2d) {
|
||||
DrawBackground(g2d);
|
||||
DrawObjects(g2d);
|
||||
}
|
||||
|
||||
private void DrawBackground(Graphics2D g2d) {
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.setStroke(new BasicStroke(3));
|
||||
for (int i = 0; i < pictureWidth / placeSizeWidth; i++) {
|
||||
for (int j = 0; j < pictureHeight / placeSizeHeight + 1; ++j) {
|
||||
g2d.drawLine(i * placeSizeWidth, j * placeSizeHeight, i * placeSizeWidth
|
||||
+ placeSizeWidth / 2, j * placeSizeHeight);
|
||||
}
|
||||
g2d.drawLine( i * placeSizeWidth, 0, i * placeSizeWidth,
|
||||
pictureHeight / placeSizeHeight * placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawObjects(Graphics2D g2d) {
|
||||
int width = pictureWidth / placeSizeWidth;
|
||||
int height = pictureHeight / placeSizeHeight;
|
||||
int diff = 1, currWidth = 0, i = 0;
|
||||
for (T locomotive : collection.GetLocomotives(50)) {
|
||||
currWidth++;
|
||||
if (currWidth > width) {
|
||||
diff++;
|
||||
currWidth = 1;
|
||||
}
|
||||
if (locomotive != null) {
|
||||
locomotive.setPosition(i % width * placeSizeWidth + placeSizeWidth / 40,
|
||||
(height - diff) * placeSizeHeight + placeSizeHeight / 15);
|
||||
locomotive.drawTransport(g2d);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
46
src/projectMonorail/Generics/LocomotivesGenericStorage.java
Normal file
46
src/projectMonorail/Generics/LocomotivesGenericStorage.java
Normal file
@ -0,0 +1,46 @@
|
||||
package projectMonorail.Generics;
|
||||
|
||||
import projectMonorail.DrawingObjects.DrawingLocomotive;
|
||||
import projectMonorail.MovementStrategy.DrawingObjectLocomotive;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class LocomotivesGenericStorage {
|
||||
final HashMap<String, LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> locomotivesStorages;
|
||||
|
||||
public ArrayList<String> keys() {return new ArrayList<>(locomotivesStorages.keySet());}
|
||||
|
||||
private final int pictureWidth;
|
||||
|
||||
private final int pictureHeight;
|
||||
|
||||
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) {
|
||||
locomotivesStorages = new HashMap<>();
|
||||
this.pictureWidth = pictureWidth;
|
||||
this.pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
public void addSet(String name) {
|
||||
if (locomotivesStorages.containsKey(name))
|
||||
return;
|
||||
locomotivesStorages.put(name, new LocomotivesGenericCollection<>(pictureWidth, pictureHeight));
|
||||
}
|
||||
|
||||
public void delSet(String name) {
|
||||
if (locomotivesStorages.containsKey(name))
|
||||
locomotivesStorages.remove(name);
|
||||
}
|
||||
|
||||
public LocomotivesGenericCollection get(String ind) {
|
||||
if (locomotivesStorages.containsKey(ind))
|
||||
return locomotivesStorages.get(ind);
|
||||
return null;
|
||||
}
|
||||
|
||||
public DrawingObjectLocomotive get(String name, int ind) {
|
||||
if (locomotivesStorages.containsKey(name))
|
||||
return locomotivesStorages.get(name).getU(ind);
|
||||
return null;
|
||||
}
|
||||
}
|
72
src/projectMonorail/Generics/SetGeneric.java
Normal file
72
src/projectMonorail/Generics/SetGeneric.java
Normal file
@ -0,0 +1,72 @@
|
||||
package projectMonorail.Generics;
|
||||
|
||||
import projectMonorail.DrawingObjects.DrawingLocomotive;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class SetGeneric<T extends Object> {
|
||||
private final ArrayList<T> places;
|
||||
|
||||
public int count() {
|
||||
return places.size();
|
||||
}
|
||||
|
||||
private final int maxCount;
|
||||
|
||||
public SetGeneric(int count) {
|
||||
maxCount = count;
|
||||
places = new ArrayList<>(count);
|
||||
}
|
||||
|
||||
public int insert(T locomotive) {
|
||||
return insert(locomotive, 0);
|
||||
}
|
||||
|
||||
public int insert(T locomotive, int position) {
|
||||
if (position < 0 || position > count() || count() >= maxCount)
|
||||
return -1;
|
||||
|
||||
places.add(position, locomotive);
|
||||
return position;
|
||||
}
|
||||
|
||||
public T remove(int position) {
|
||||
if (position < 0 || position >= count())
|
||||
return null;
|
||||
return places.remove(position);
|
||||
}
|
||||
|
||||
public T get(int position) {
|
||||
if (position < 0 || position >= count())
|
||||
return null;
|
||||
return places.get(position);
|
||||
}
|
||||
|
||||
public Iterable<T> GetLocomotives(final Integer maxLocomotives) {
|
||||
return () -> new Iterator<>() {
|
||||
private int currentIndex = 0;
|
||||
private int count = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex < count() && (maxLocomotives == null || count < maxLocomotives);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (hasNext()) {
|
||||
count++;
|
||||
return places.get(currentIndex++);
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
8
src/projectMonorail/Main.java
Normal file
8
src/projectMonorail/Main.java
Normal file
@ -0,0 +1,8 @@
|
||||
package projectMonorail;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FrameMonorailCollection frameMonorailCollection = new FrameMonorailCollection();
|
||||
}
|
||||
}
|
||||
|
99
src/projectMonorail/MovementStrategy/AbstractStrategy.java
Normal file
99
src/projectMonorail/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,99 @@
|
||||
package projectMonorail.MovementStrategy;
|
||||
|
||||
import projectMonorail.DirectionType;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
|
||||
private IMoveableObject moveableObject;
|
||||
|
||||
private Status state = Status.NotInit;
|
||||
|
||||
private int fieldWidth;
|
||||
|
||||
protected int getFieldWidth() {
|
||||
return fieldWidth;
|
||||
}
|
||||
|
||||
private int fieldHeight;
|
||||
|
||||
protected int getFieldHeight() {
|
||||
return fieldHeight;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setData(IMoveableObject moveableObject, int width, int height) {
|
||||
if (moveableObject == null) {
|
||||
state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
state = Status.InProgress;
|
||||
this.moveableObject = moveableObject;
|
||||
fieldWidth = width;
|
||||
fieldHeight = height;
|
||||
}
|
||||
|
||||
public void makeStep() {
|
||||
if (state != Status.InProgress) {
|
||||
return;
|
||||
}
|
||||
if (isTargetDestination()) {
|
||||
state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
moveToTarget();
|
||||
}
|
||||
|
||||
protected boolean moveLeft() {
|
||||
return moveTo(DirectionType.Left);
|
||||
}
|
||||
|
||||
protected boolean moveRight() {
|
||||
return moveTo(DirectionType.Right);
|
||||
}
|
||||
|
||||
protected boolean moveUp() {
|
||||
return moveTo(DirectionType.Up);
|
||||
}
|
||||
|
||||
protected boolean moveDown() {
|
||||
return moveTo(DirectionType.Down);
|
||||
}
|
||||
|
||||
protected ObjectParameters getObjectParameters() {
|
||||
if (moveableObject == null) {
|
||||
return null;
|
||||
}
|
||||
return moveableObject.getObjectsPosition();
|
||||
}
|
||||
|
||||
protected Integer getStep() {
|
||||
if (state != Status.InProgress) {
|
||||
return null;
|
||||
}
|
||||
if (moveableObject == null) {
|
||||
return null;
|
||||
}
|
||||
return moveableObject.getStep();
|
||||
}
|
||||
|
||||
protected abstract void moveToTarget();
|
||||
|
||||
protected abstract boolean isTargetDestination();
|
||||
|
||||
private boolean moveTo(DirectionType directionType) {
|
||||
if (state != Status.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject == null) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject.checkCanMove(directionType)) {
|
||||
moveableObject.moveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package projectMonorail.MovementStrategy;
|
||||
|
||||
|
||||
import projectMonorail.DirectionType;
|
||||
import projectMonorail.DrawingObjects.DrawingLocomotive;
|
||||
|
||||
public class DrawingObjectLocomotive implements IMoveableObject {
|
||||
|
||||
private DrawingLocomotive drawingLocomotive = null;
|
||||
|
||||
public DrawingObjectLocomotive(DrawingLocomotive drawingLocomotive) {
|
||||
this.drawingLocomotive = drawingLocomotive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectParameters getObjectsPosition() {
|
||||
if (drawingLocomotive == null || drawingLocomotive.getEntityLocomotive() == null) {
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(drawingLocomotive.getPosX(), drawingLocomotive.getPosY(),
|
||||
drawingLocomotive.getWidth(), drawingLocomotive.getHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStep() {
|
||||
if (drawingLocomotive == null)
|
||||
return 0;
|
||||
return (int) ((drawingLocomotive.getEntityLocomotive() != null) ? drawingLocomotive.getEntityLocomotive().getStep() : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCanMove(DirectionType direction) {
|
||||
if (drawingLocomotive == null)
|
||||
return false;
|
||||
return drawingLocomotive.canMove(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveObject(DirectionType direction) {
|
||||
if (drawingLocomotive != null)
|
||||
drawingLocomotive.moveTransport(direction);
|
||||
}
|
||||
}
|
14
src/projectMonorail/MovementStrategy/IMoveableObject.java
Normal file
14
src/projectMonorail/MovementStrategy/IMoveableObject.java
Normal file
@ -0,0 +1,14 @@
|
||||
package projectMonorail.MovementStrategy;
|
||||
|
||||
import projectMonorail.DirectionType;
|
||||
|
||||
public interface IMoveableObject {
|
||||
|
||||
ObjectParameters getObjectsPosition();
|
||||
|
||||
int getStep();
|
||||
|
||||
boolean checkCanMove(DirectionType direction);
|
||||
|
||||
void moveObject(DirectionType direction);
|
||||
}
|
36
src/projectMonorail/MovementStrategy/MoveToBorder.java
Normal file
36
src/projectMonorail/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,36 @@
|
||||
package projectMonorail.MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy {
|
||||
|
||||
@Override
|
||||
protected boolean isTargetDestination() {
|
||||
var objParams = getObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.rightBorder() <= getFieldWidth() &&
|
||||
objParams.rightBorder() + getStep() >= getFieldWidth() &&
|
||||
objParams.downBorder() <= getFieldHeight() &&
|
||||
objParams.downBorder() + getStep() >= getFieldHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void moveToTarget() {
|
||||
var objParams = getObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth();
|
||||
if (Math.abs(diffX) > getStep()) {
|
||||
if (diffX < 0) {
|
||||
moveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.objectMiddleVertical() - getFieldHeight();
|
||||
if (Math.abs(diffY) > getStep()) {
|
||||
if (diffY < 0) {
|
||||
moveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
40
src/projectMonorail/MovementStrategy/MoveToCenter.java
Normal file
40
src/projectMonorail/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,40 @@
|
||||
package projectMonorail.MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy {
|
||||
|
||||
@Override
|
||||
protected boolean isTargetDestination() {
|
||||
var objParams = getObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.objectMiddleHorizontal() <= getFieldWidth() / 2 &&
|
||||
objParams.objectMiddleHorizontal() + getStep() >= getFieldWidth() / 2 &&
|
||||
objParams.objectMiddleVertical() <= getFieldHeight() / 2 &&
|
||||
objParams.objectMiddleVertical() + getStep() >= getFieldHeight() / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void moveToTarget() {
|
||||
var objParams = getObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth() / 2;
|
||||
if (Math.abs(diffX) > getStep()) {
|
||||
if (diffX > 0) {
|
||||
moveLeft();
|
||||
} else {
|
||||
moveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.objectMiddleVertical() - getFieldHeight() / 2;
|
||||
if (Math.abs(diffY) > getStep()) {
|
||||
if (diffY > 0) {
|
||||
moveUp();
|
||||
} else {
|
||||
moveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
43
src/projectMonorail/MovementStrategy/ObjectParameters.java
Normal file
43
src/projectMonorail/MovementStrategy/ObjectParameters.java
Normal file
@ -0,0 +1,43 @@
|
||||
package projectMonorail.MovementStrategy;
|
||||
|
||||
public class ObjectParameters {
|
||||
|
||||
private final int x;
|
||||
|
||||
private final int y;
|
||||
|
||||
private final int width;
|
||||
|
||||
private final int height;
|
||||
|
||||
public int leftBorder() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int topBorder() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int rightBorder() {
|
||||
return x + width;
|
||||
}
|
||||
|
||||
public int downBorder() {
|
||||
return y + height;
|
||||
}
|
||||
|
||||
public int objectMiddleHorizontal() {
|
||||
return x + width / 2;
|
||||
}
|
||||
|
||||
public int objectMiddleVertical() {
|
||||
return y + height / 2;
|
||||
}
|
||||
|
||||
public ObjectParameters(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
9
src/projectMonorail/MovementStrategy/Status.java
Normal file
9
src/projectMonorail/MovementStrategy/Status.java
Normal file
@ -0,0 +1,9 @@
|
||||
package projectMonorail.MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
|
||||
InProgress,
|
||||
|
||||
Finish
|
||||
}
|
313
src/projectMonorail/PictureBox.java
Normal file
313
src/projectMonorail/PictureBox.java
Normal file
@ -0,0 +1,313 @@
|
||||
package projectMonorail;
|
||||
|
||||
import projectMonorail.DrawingObjects.*;
|
||||
import projectMonorail.MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class PictureBox extends JPanel {
|
||||
private boolean dialogResult = false;
|
||||
|
||||
public boolean getDialogResult() {
|
||||
return dialogResult;
|
||||
}
|
||||
|
||||
private DrawingLocomotive drawingLocomotive;
|
||||
|
||||
private AbstractStrategy abstractStrategy;
|
||||
|
||||
private DrawingLocomotive selectedLocomotive;
|
||||
|
||||
public DrawingLocomotive getSelectedLocomotive() {
|
||||
return selectedLocomotive;
|
||||
}
|
||||
|
||||
private JButton buttonLeft;
|
||||
|
||||
private JButton buttonUp;
|
||||
|
||||
private JButton buttonRight;
|
||||
|
||||
private JButton buttonDown;
|
||||
|
||||
private JButton buttonCreateLocomotive;
|
||||
|
||||
private JButton buttonCreateMonorail;
|
||||
|
||||
private JButton buttonSelectLocomotive;
|
||||
|
||||
private JButton buttonStep;
|
||||
|
||||
private JComboBox comboBoxStrategy;
|
||||
|
||||
private JPanel buttonsPanel;
|
||||
|
||||
private JPanel buttonsMovePanel;
|
||||
|
||||
private JPanel movePaddingPanel;
|
||||
|
||||
private JPanel strategyPaddingPanel;
|
||||
|
||||
private JPanel strategyPanel;
|
||||
|
||||
public PictureBox(DrawingLocomotive locomotive) {
|
||||
if (locomotive != null) {
|
||||
Random random = new Random();
|
||||
drawingLocomotive = locomotive;
|
||||
drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
}
|
||||
|
||||
abstractStrategy = null;
|
||||
selectedLocomotive = null;
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
buttonsPanel = new JPanel();
|
||||
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
|
||||
buttonsPanel.setOpaque(false);
|
||||
|
||||
buttonCreateLocomotive = new JButton("Create locomotive");
|
||||
buttonCreateLocomotive.setFocusable(false);
|
||||
buttonCreateLocomotive.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonCreateLocomotive.setBackground(Color.LIGHT_GRAY);
|
||||
buttonCreateLocomotive.setMaximumSize(new Dimension(140, 42));
|
||||
|
||||
buttonCreateLocomotive.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color chosenColor = JColorChooser.showDialog(this, "Main color", Color.WHITE);
|
||||
if (chosenColor != null) {
|
||||
color = chosenColor;
|
||||
}
|
||||
drawingLocomotive = new DrawingLocomotive(random.nextInt(200, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
color, this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||
drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonCreateMonorail = new JButton("Create monorail");
|
||||
buttonCreateMonorail.setFocusable(false);
|
||||
buttonCreateMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonCreateMonorail.setBackground(Color.LIGHT_GRAY);
|
||||
buttonCreateMonorail.setMaximumSize(new Dimension(140, 42));
|
||||
|
||||
buttonCreateMonorail.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color chosenColor = JColorChooser.showDialog(this, "Main color", Color.WHITE);
|
||||
if (chosenColor != null) {
|
||||
color = chosenColor;
|
||||
}
|
||||
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color chosenAddColor = JColorChooser.showDialog(this, "Additional Color", Color.WHITE);
|
||||
if (chosenAddColor != null) {
|
||||
additionalColor = chosenAddColor;
|
||||
}
|
||||
drawingLocomotive = new DrawingMonorail(random.nextInt(200, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
color, additionalColor, random.nextBoolean(), random.nextBoolean(),
|
||||
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||
drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonSelectLocomotive = new JButton("Select");
|
||||
buttonSelectLocomotive.setFocusable(false);
|
||||
buttonSelectLocomotive.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonSelectLocomotive.setBackground(Color.LIGHT_GRAY);
|
||||
buttonSelectLocomotive.setMaximumSize(new Dimension(140, 42));
|
||||
|
||||
buttonSelectLocomotive.addActionListener(e -> {
|
||||
selectedLocomotive = drawingLocomotive;
|
||||
dialogResult = true;
|
||||
JComponent comp = (JComponent)e.getSource();
|
||||
Window win = SwingUtilities.getWindowAncestor(comp);
|
||||
win.dispose();
|
||||
});
|
||||
|
||||
ActionListener buttonMoveListener = e -> {
|
||||
if (drawingLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String buttonName = ((JButton) e.getSource()).getName();
|
||||
|
||||
switch (buttonName) {
|
||||
case ("buttonUp"):
|
||||
drawingLocomotive.moveTransport(DirectionType.Up);
|
||||
break;
|
||||
case ("buttonDown"):
|
||||
drawingLocomotive.moveTransport(DirectionType.Down);
|
||||
break;
|
||||
case ("buttonLeft"):
|
||||
drawingLocomotive.moveTransport(DirectionType.Left);
|
||||
break;
|
||||
case ("buttonRight"):
|
||||
drawingLocomotive.moveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
repaint();
|
||||
};
|
||||
|
||||
buttonLeft = new JButton();
|
||||
buttonLeft.setName("buttonLeft");
|
||||
buttonLeft.setFocusable(false);
|
||||
buttonLeft.setPreferredSize(new Dimension(30, 30));
|
||||
buttonLeft.setIcon(new ImageIcon("Resources/arrowLeft.png"));
|
||||
buttonLeft.addActionListener(buttonMoveListener);
|
||||
|
||||
buttonRight = new JButton();
|
||||
buttonRight.setName("buttonRight");
|
||||
buttonRight.setFocusable(false);
|
||||
buttonRight.setPreferredSize(new Dimension(30, 30));
|
||||
buttonRight.setIcon(new ImageIcon("Resources/arrowRight.png"));
|
||||
buttonRight.addActionListener(buttonMoveListener);
|
||||
|
||||
buttonDown = new JButton();
|
||||
buttonDown.setName("buttonDown");
|
||||
buttonDown.setFocusable(false);
|
||||
buttonDown.setPreferredSize(new Dimension(30, 30));
|
||||
buttonDown.setIcon(new ImageIcon("Resources/arrowDown.png"));
|
||||
buttonDown.addActionListener(buttonMoveListener);
|
||||
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setName("buttonUp");
|
||||
buttonUp.setFocusable(false);
|
||||
buttonUp.setPreferredSize(new Dimension(30, 30));
|
||||
buttonUp.setIcon(new ImageIcon("Resources/arrowUp.png"));
|
||||
buttonUp.addActionListener(buttonMoveListener);
|
||||
|
||||
buttonsMovePanel = new JPanel();
|
||||
buttonsMovePanel.setLayout(new GridBagLayout());
|
||||
buttonsMovePanel.setOpaque(false);
|
||||
|
||||
GridBagConstraints constrains = new GridBagConstraints();
|
||||
constrains.insets = new Insets(5, 5, 5, 5);
|
||||
constrains.gridx = 0;
|
||||
constrains.gridy = 0;
|
||||
|
||||
buttonsMovePanel.add(Box.createHorizontalStrut(30), constrains);
|
||||
|
||||
constrains.gridx = 1;
|
||||
constrains.gridy = 0;
|
||||
|
||||
buttonsMovePanel.add(buttonUp, constrains);
|
||||
|
||||
constrains.gridx = 2;
|
||||
constrains.gridy = 0;
|
||||
|
||||
buttonsMovePanel.add(Box.createHorizontalStrut(30), constrains);
|
||||
|
||||
constrains.gridx = 0;
|
||||
constrains.gridy = 1;
|
||||
|
||||
buttonsMovePanel.add(buttonLeft, constrains);
|
||||
|
||||
constrains.gridx = 1;
|
||||
constrains.gridy = 1;
|
||||
|
||||
buttonsMovePanel.add(buttonDown, constrains);
|
||||
|
||||
constrains.gridx = 2;
|
||||
constrains.gridy = 1;
|
||||
|
||||
buttonsMovePanel.add(buttonRight, constrains);
|
||||
|
||||
movePaddingPanel = new JPanel();
|
||||
movePaddingPanel.setLayout(new BoxLayout(movePaddingPanel, BoxLayout.Y_AXIS));
|
||||
movePaddingPanel.setOpaque(false);
|
||||
|
||||
movePaddingPanel.add(buttonsMovePanel);
|
||||
movePaddingPanel.add(Box.createVerticalStrut(22));
|
||||
|
||||
buttonsPanel.add(Box.createHorizontalStrut(12));
|
||||
buttonsPanel.add(buttonCreateMonorail);
|
||||
buttonsPanel.add(Box.createHorizontalStrut(16));
|
||||
buttonsPanel.add(buttonCreateLocomotive);
|
||||
buttonsPanel.add(Box.createHorizontalStrut(16));
|
||||
buttonsPanel.add(buttonSelectLocomotive);
|
||||
buttonsPanel.add(Box.createHorizontalStrut(400));
|
||||
buttonsPanel.add(movePaddingPanel);
|
||||
|
||||
add(buttonsPanel, BorderLayout.SOUTH);
|
||||
|
||||
strategyPaddingPanel = new JPanel();
|
||||
strategyPaddingPanel.setLayout(new BoxLayout(strategyPaddingPanel, BoxLayout.X_AXIS));
|
||||
strategyPaddingPanel.setOpaque(false);
|
||||
|
||||
String[] items = {
|
||||
"Form center",
|
||||
"Form border"
|
||||
};
|
||||
comboBoxStrategy = new JComboBox(items);
|
||||
comboBoxStrategy.setMaximumSize(new Dimension(150, 23));
|
||||
|
||||
buttonStep = new JButton("Step");
|
||||
buttonStep.setFocusable(false);
|
||||
buttonStep.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonStep.setBackground(Color.LIGHT_GRAY);
|
||||
buttonStep.setMaximumSize(new Dimension(75, 28));
|
||||
|
||||
buttonStep.addActionListener(e -> {
|
||||
if (drawingLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled()) {
|
||||
abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) {
|
||||
case 0 -> new MoveToCenter();
|
||||
case 1 -> new MoveToBorder();
|
||||
default -> null;
|
||||
};
|
||||
if (abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
abstractStrategy.setData(new DrawingObjectLocomotive(drawingLocomotive), this.getWidth(), this.getHeight());
|
||||
}
|
||||
if (abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
abstractStrategy.makeStep();
|
||||
repaint();
|
||||
if (abstractStrategy.getStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
abstractStrategy = null;
|
||||
}
|
||||
});
|
||||
|
||||
strategyPanel = new JPanel();
|
||||
strategyPanel.setLayout(new BoxLayout(strategyPanel, BoxLayout.Y_AXIS));
|
||||
strategyPanel.setOpaque(false);
|
||||
|
||||
strategyPanel.add(Box.createVerticalStrut(10));
|
||||
strategyPanel.add(comboBoxStrategy);
|
||||
strategyPanel.add(Box.createVerticalStrut(15));
|
||||
strategyPanel.add(buttonStep);
|
||||
|
||||
strategyPaddingPanel.add(Box.createHorizontalStrut(735));
|
||||
strategyPaddingPanel.add(strategyPanel);
|
||||
|
||||
add(strategyPaddingPanel, BorderLayout.NORTH);
|
||||
|
||||
setPreferredSize(new Dimension(900, 660));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
if (drawingLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
drawingLocomotive.drawTransport(g2d);
|
||||
}
|
||||
}
|
32
src/projectMonorail/PictureBoxCollection.java
Normal file
32
src/projectMonorail/PictureBoxCollection.java
Normal file
@ -0,0 +1,32 @@
|
||||
package projectMonorail;
|
||||
|
||||
import projectMonorail.DrawingObjects.*;
|
||||
import projectMonorail.Generics.*;
|
||||
import projectMonorail.MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class PictureBoxCollection extends JPanel {
|
||||
private final LocomotivesGenericStorage storage;
|
||||
|
||||
private JList<String> listSets;
|
||||
|
||||
public PictureBoxCollection(LocomotivesGenericStorage storage, JList<String> listSets, int width, int height) {
|
||||
this.storage = storage;
|
||||
this.listSets = listSets;
|
||||
setMaximumSize(new Dimension(width, height));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
if (listSets.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = storage.get(listSets.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
obj.showLocomotives(g2d);
|
||||
}
|
||||
}
|
62
src/projectMonorail/PictureBoxRandomGeneration.java
Normal file
62
src/projectMonorail/PictureBoxRandomGeneration.java
Normal file
@ -0,0 +1,62 @@
|
||||
package projectMonorail;
|
||||
|
||||
import projectMonorail.DrawingObjects.*;
|
||||
import projectMonorail.Entities.*;
|
||||
import projectMonorail.Extras.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class PictureBoxRandomGeneration extends JPanel {
|
||||
private RandomGeneric<EntityLocomotive, IDrawingWheels> randomGeneric;
|
||||
|
||||
private DrawingLocomotive drawingLocomotive;
|
||||
|
||||
private JButton buttonRandomCreation;
|
||||
|
||||
public PictureBoxRandomGeneration(int width, int height) {
|
||||
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||
|
||||
buttonRandomCreation = new JButton("Create monorail");
|
||||
buttonRandomCreation.setFocusable(false);
|
||||
buttonRandomCreation.setMaximumSize(new Dimension(155, 36));
|
||||
buttonRandomCreation.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||
buttonRandomCreation.setBackground(Color.WHITE);
|
||||
buttonRandomCreation.setHorizontalTextPosition(JButton.CENTER);
|
||||
buttonRandomCreation.setVerticalTextPosition(JButton.CENTER);
|
||||
buttonRandomCreation.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
buttonRandomCreation.addActionListener(e -> {
|
||||
drawingLocomotive = randomGeneric.getDrawingLocomotive();
|
||||
repaint();
|
||||
});
|
||||
|
||||
add(Box.createVerticalStrut(height - 80));
|
||||
add(buttonRandomCreation);
|
||||
setPreferredSize(new Dimension(width, height));
|
||||
|
||||
randomGeneric = new RandomGeneric<>(7, 3, width, height);
|
||||
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.RED));
|
||||
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.GREEN));
|
||||
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.BLUE));
|
||||
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.PINK));
|
||||
randomGeneric.addItem(new EntityMonorail(200, 2000, Color.WHITE, Color.BLUE,true, false));
|
||||
randomGeneric.addItem(new EntityMonorail(200, 2000, Color.ORANGE, Color.LIGHT_GRAY,false, true));
|
||||
randomGeneric.addItem(new EntityMonorail(200, 2000, Color.CYAN, Color.MAGENTA,true, true));
|
||||
randomGeneric.addItem(new DrawingNormalWheels());
|
||||
randomGeneric.addItem(new DrawingVintageWheels());
|
||||
randomGeneric.addItem(new DrawingSquarePatternWheels());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
if (drawingLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
drawingLocomotive.setPosition(getWidth() * 27 / 100, getHeight() * 3 / 10);
|
||||
drawingLocomotive.drawTransport(g2d);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user