Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
69dfd359c2 | |||
1ce9de4971 | |||
b48714c9cb | |||
744b41ca05 | |||
2996acbea2 | |||
d00844dfac | |||
fbf7485bd1 | |||
b9b074c809 | |||
6c478bb1a6 |
@ -3,6 +3,7 @@
|
|||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
BIN
Liner_Advanced/res/icons8-arrow-down-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-down-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 387 B |
BIN
Liner_Advanced/res/icons8-arrow-left-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-left-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 332 B |
BIN
Liner_Advanced/res/icons8-arrow-right-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-right-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 294 B |
BIN
Liner_Advanced/res/icons8-arrow-up-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-up-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 372 B |
@ -1,5 +1,12 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Hello, World!");
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
new projectliner.FormShipCollection().setVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
public class CircularDeckDrawing implements IAdditionalElements {
|
||||||
|
private DeckEnum deckEnum;
|
||||||
|
|
||||||
|
public CircularDeckDrawing() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumericalValue(int value) {
|
||||||
|
for(DeckEnum deck : DeckEnum.values()) {
|
||||||
|
if (deck.getDeckNumber() == value) {
|
||||||
|
this.deckEnum = deck;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Invalid deck number: " + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumericalValue() {
|
||||||
|
return this.deckEnum.getDeckNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawAdditionalElement(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
switch (this.deckEnum) {
|
||||||
|
case UPPER:
|
||||||
|
this.drawUpperDeck(x, y, borderColor, fillColor, g);
|
||||||
|
case MIDDLE:
|
||||||
|
this.drawMiddleDeck(x, y, borderColor, fillColor, g);
|
||||||
|
case LOWER:
|
||||||
|
this.drawLowerDeck(x, y, borderColor, fillColor, g);
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawLowerDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillOval(x + 30, y + 15, 100, 20);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawOval(x + 30, y + 15, 100, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawMiddleDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillOval(x + 70, y + 10, 50, 20);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawOval(x + 70, y + 10, 50, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawUpperDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillOval(x + 85, y, 25, 20);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawOval(x + 85, y, 25, 20);
|
||||||
|
}
|
||||||
|
}
|
17
Liner_Advanced/src/projectliner/Drawings/DeckEnum.java
Normal file
17
Liner_Advanced/src/projectliner/Drawings/DeckEnum.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
public enum DeckEnum {
|
||||||
|
LOWER(1),
|
||||||
|
MIDDLE(2),
|
||||||
|
UPPER(3);
|
||||||
|
|
||||||
|
private final int deckNumber;
|
||||||
|
|
||||||
|
private DeckEnum(int deckNumber) {
|
||||||
|
this.deckNumber = deckNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDeckNumber() {
|
||||||
|
return this.deckNumber;
|
||||||
|
}
|
||||||
|
}
|
13
Liner_Advanced/src/projectliner/Drawings/DirectionType.java
Normal file
13
Liner_Advanced/src/projectliner/Drawings/DirectionType.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
public enum DirectionType {
|
||||||
|
NONE,
|
||||||
|
UP,
|
||||||
|
DOWN,
|
||||||
|
LEFT,
|
||||||
|
RIGHT;
|
||||||
|
|
||||||
|
private DirectionType() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
165
Liner_Advanced/src/projectliner/Drawings/DrawingBaseLiner.java
Normal file
165
Liner_Advanced/src/projectliner/Drawings/DrawingBaseLiner.java
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Point;
|
||||||
|
import projectliner.Entities.BaseLinerEntity;
|
||||||
|
|
||||||
|
public class DrawingBaseLiner {
|
||||||
|
public BaseLinerEntity baseLiner;
|
||||||
|
protected Integer pictureWidth;
|
||||||
|
protected Integer pictureHeight;
|
||||||
|
protected Integer startPosX;
|
||||||
|
protected Integer startPosY;
|
||||||
|
private int drawingLinerWidth;
|
||||||
|
private int drawingLinerHeight;
|
||||||
|
private IAdditionalElements deck;
|
||||||
|
|
||||||
|
private DrawingBaseLiner() {
|
||||||
|
this.drawingLinerWidth = 140;
|
||||||
|
this.drawingLinerHeight = 40;
|
||||||
|
this.pictureWidth = null;
|
||||||
|
this.pictureHeight = null;
|
||||||
|
this.startPosX = null;
|
||||||
|
this.startPosY = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingBaseLiner(int speed, double weight, Color primaryColor) {
|
||||||
|
this();
|
||||||
|
this.baseLiner = new BaseLinerEntity(speed, weight, primaryColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingBaseLiner(int drawingBaseLinerWidth, int drawingBaseLinerHeight) {
|
||||||
|
this.drawingLinerHeight = drawingBaseLinerHeight;
|
||||||
|
this.drawingLinerWidth = drawingBaseLinerWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingBaseLiner(BaseLinerEntity liner, IAdditionalElements deck, int deckNum) {
|
||||||
|
this();
|
||||||
|
this.baseLiner = liner;
|
||||||
|
this.deck = deck;
|
||||||
|
this.deck.setNumericalValue(deckNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setPictureSize(int width, int height) {
|
||||||
|
if (this.drawingLinerWidth <= width && this.drawingLinerHeight <= height) {
|
||||||
|
this.pictureWidth = width;
|
||||||
|
this.pictureHeight = height;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPosition(int x, int y) {
|
||||||
|
if (this.pictureHeight != null && this.pictureWidth != null) {
|
||||||
|
if (x < 0) {
|
||||||
|
x = 0;
|
||||||
|
} else if (x > this.pictureWidth - this.drawingLinerWidth) {
|
||||||
|
x = this.pictureWidth - this.drawingLinerWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y < 0) {
|
||||||
|
y = 0;
|
||||||
|
} else if (y > this.pictureHeight - this.drawingLinerHeight) {
|
||||||
|
y = this.pictureHeight - this.drawingLinerHeight;
|
||||||
|
}
|
||||||
|
this.startPosX = x;
|
||||||
|
this.startPosY = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean moveTransport(DirectionType direction) {
|
||||||
|
if (this.baseLiner != null && this.startPosX != null && this.startPosY != null && this.pictureWidth != null && this.pictureHeight != null) {
|
||||||
|
switch (direction) {
|
||||||
|
case LEFT:
|
||||||
|
if ((double)this.startPosX - this.baseLiner.getStep() > (double)0.0F) {
|
||||||
|
this.startPosX = this.startPosX - (int)this.baseLiner.getStep();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case UP:
|
||||||
|
if ((double)this.startPosY - this.baseLiner.getStep() > (double)0.0F) {
|
||||||
|
this.startPosY = this.startPosY - (int)this.baseLiner.getStep();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
if ((double)this.startPosX + this.baseLiner.getStep() < (double)(this.pictureWidth - this.drawingLinerWidth)) {
|
||||||
|
this.startPosX = this.startPosX + (int)this.baseLiner.getStep();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DOWN:
|
||||||
|
if ((double)this.startPosY + this.baseLiner.getStep() < (double)(this.pictureHeight - this.drawingLinerHeight)) {
|
||||||
|
this.startPosY = this.startPosY + (int)this.baseLiner.getStep();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawTransport(Graphics g) {
|
||||||
|
if (this.baseLiner != null && this.startPosX != null && this.startPosY != null) {
|
||||||
|
int x = this.startPosX;
|
||||||
|
int y = this.startPosY;
|
||||||
|
Graphics2D g2d = (Graphics2D)g;
|
||||||
|
Color deckColor = Color.WHITE;
|
||||||
|
Color borderColor = Color.BLACK;
|
||||||
|
this.drawLinerBase(g);
|
||||||
|
if (this.deck == null) {
|
||||||
|
this.deck = new SquareDeckDrawing();
|
||||||
|
this.deck.setNumericalValue(1);
|
||||||
|
}
|
||||||
|
this.deck.drawAdditionalElement(x, y - 20, borderColor, deckColor, g2d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawLinerBase(Graphics g) {
|
||||||
|
int x = this.startPosX;
|
||||||
|
int y = this.startPosY;
|
||||||
|
Graphics2D g2d = (Graphics2D)g;
|
||||||
|
Color bodyColor = this.baseLiner.getPrimaryColor();
|
||||||
|
Color borderColor = Color.BLACK;
|
||||||
|
Point[] hullPoints = new Point[]{new Point(x + 20, y + 40),
|
||||||
|
new Point(x + 120, y + 40), new Point(x + 140, y + 10),
|
||||||
|
new Point(x, y + 10)};
|
||||||
|
|
||||||
|
g2d.setColor(bodyColor);
|
||||||
|
g2d.fillPolygon(new int[]{hullPoints[0].x, hullPoints[1].x,
|
||||||
|
hullPoints[2].x, hullPoints[3].x},
|
||||||
|
new int[]{hullPoints[0].y, hullPoints[1].y,
|
||||||
|
hullPoints[2].y, hullPoints[3].y}, 4);
|
||||||
|
g2d.setColor(borderColor);
|
||||||
|
|
||||||
|
g2d.drawPolygon(new int[]{hullPoints[0].x, hullPoints[1].x,
|
||||||
|
hullPoints[2].x, hullPoints[3].x},
|
||||||
|
new int[]{hullPoints[0].y, hullPoints[1].y,
|
||||||
|
hullPoints[2].y, hullPoints[3].y}, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseLinerEntity getBaseLiner() {
|
||||||
|
return this.baseLiner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPosX() {
|
||||||
|
return this.startPosX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPosY() {
|
||||||
|
return this.startPosY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return this.drawingLinerWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return this.drawingLinerHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
77
Liner_Advanced/src/projectliner/Drawings/DrawingLiner.java
Normal file
77
Liner_Advanced/src/projectliner/Drawings/DrawingLiner.java
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
import projectliner.Entities.BaseLinerEntity;
|
||||||
|
import projectliner.Entities.LinerEntity;
|
||||||
|
import projectliner.Entities.LinerEntityType;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class DrawingLiner extends DrawingBaseLiner {
|
||||||
|
private IAdditionalElements deck;
|
||||||
|
|
||||||
|
public DrawingLiner(int speed, double weight, Color primaryColor,
|
||||||
|
Color secondaryColor, LinerEntityType type, int capacity,
|
||||||
|
int maxPassengers, boolean hasExtraDeck, boolean hasPool, int deckNum, String form) {
|
||||||
|
super(140, 60);
|
||||||
|
this.baseLiner = new LinerEntity(speed, weight, primaryColor,
|
||||||
|
secondaryColor, type, capacity, maxPassengers, hasExtraDeck, hasPool);
|
||||||
|
setDeckForm(form);
|
||||||
|
this.deck.setNumericalValue(deckNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingLiner(BaseLinerEntity liner, IAdditionalElements deck, int deckNum) {
|
||||||
|
super(140, 60);
|
||||||
|
this.baseLiner = liner;
|
||||||
|
this.deck = deck;
|
||||||
|
this.deck.setNumericalValue(deckNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BufferedImage drawTransport() {
|
||||||
|
BufferedImage image =
|
||||||
|
new BufferedImage(this.pictureWidth, this.pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||||
|
Graphics g = image.getGraphics();
|
||||||
|
drawTransport(g);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setDeckForm(String form) {
|
||||||
|
switch (form) {
|
||||||
|
case "Triangular Deck" -> this.deck = new TriangularDeckDrawing();
|
||||||
|
case "Circular Deck" -> this.deck = new CircularDeckDrawing();
|
||||||
|
case "Square Deck" -> this.deck = new SquareDeckDrawing();
|
||||||
|
default -> throw new IllegalArgumentException("Invalid deck form: " + form);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawTransport(Graphics g) {
|
||||||
|
if (this.baseLiner != null && this.startPosX != null && this.startPosY != null) {
|
||||||
|
int x = this.startPosX;
|
||||||
|
int y = this.startPosY;
|
||||||
|
Graphics2D g2d = (Graphics2D)g;
|
||||||
|
|
||||||
|
if (this.baseLiner instanceof LinerEntity linerEntity) {
|
||||||
|
this.startPosY = this.startPosY + 20;
|
||||||
|
super.drawLinerBase(g);
|
||||||
|
this.startPosY = this.startPosY - 20;
|
||||||
|
Color deckColor = linerEntity.getSecondaryColor();
|
||||||
|
Color borderColor = Color.BLACK;
|
||||||
|
this.deck.drawAdditionalElement(x, y, borderColor, deckColor, g);
|
||||||
|
if (linerEntity.hasPool()) {
|
||||||
|
g2d.setColor(Color.CYAN);
|
||||||
|
g2d.fillOval(x + 35, y + 15, 30, 10);
|
||||||
|
g2d.setColor(Color.BLACK);
|
||||||
|
g2d.drawOval(x + 35, y + 15, 30, 10);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
super.drawTransport(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
public interface IAdditionalElements {
|
||||||
|
void setNumericalValue(int var1);
|
||||||
|
|
||||||
|
int getNumericalValue();
|
||||||
|
|
||||||
|
void drawAdditionalElement(int var1, int var2, Color var3, Color var4, Graphics var5);
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
public class SquareDeckDrawing implements IAdditionalElements {
|
||||||
|
private DeckEnum deckEnum;
|
||||||
|
|
||||||
|
public SquareDeckDrawing() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumericalValue(int value) {
|
||||||
|
for(DeckEnum deck : DeckEnum.values()) {
|
||||||
|
if (deck.getDeckNumber() == value) {
|
||||||
|
this.deckEnum = deck;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Invalid deck number: " + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumericalValue() {
|
||||||
|
return this.deckEnum.getDeckNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawAdditionalElement(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
switch (this.deckEnum) {
|
||||||
|
case UPPER:
|
||||||
|
this.drawUpperDeck(x, y, borderColor, fillColor, g);
|
||||||
|
case MIDDLE:
|
||||||
|
this.drawMiddleDeck(x, y, borderColor, fillColor, g);
|
||||||
|
case LOWER:
|
||||||
|
this.drawLowerDeck(x, y, borderColor, fillColor, g);
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawLowerDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillRect(x + 30, y + 20, 100, 10);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawRect(x + 30, y + 20, 100, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawMiddleDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillRect(x + 70, y + 10, 50, 10);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawRect(x + 70, y + 10, 50, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawUpperDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillRect(x + 85, y, 25, 10);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawRect(x + 85, y, 25, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
|||||||
|
package projectliner.Drawings;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
public class TriangularDeckDrawing implements IAdditionalElements {
|
||||||
|
private DeckEnum deckEnum;
|
||||||
|
|
||||||
|
public TriangularDeckDrawing() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumericalValue(int value) {
|
||||||
|
for(DeckEnum deck : DeckEnum.values()) {
|
||||||
|
if (deck.getDeckNumber() == value) {
|
||||||
|
this.deckEnum = deck;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Invalid deck number: " + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumericalValue() {
|
||||||
|
return this.deckEnum.getDeckNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawAdditionalElement(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
switch (this.deckEnum) {
|
||||||
|
case UPPER:
|
||||||
|
this.drawUpperDeck(x, y, borderColor, fillColor, g);
|
||||||
|
case MIDDLE:
|
||||||
|
this.drawMiddleDeck(x, y, borderColor, fillColor, g);
|
||||||
|
case LOWER:
|
||||||
|
this.drawLowerDeck(x, y, borderColor, fillColor, g);
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawLowerDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
int[] xPoints = new int[]{x + 130, x + 130, x};
|
||||||
|
int[] yPoints = new int[]{y + 15, y + 30, y + 25};
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillPolygon(xPoints, yPoints, 3);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawPolygon(xPoints, yPoints, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawMiddleDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
int[] xPoints = new int[]{x + 120, x + 120, x + 60};
|
||||||
|
int[] yPoints = new int[]{y + 10, y + 20, y + 15};
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillPolygon(xPoints, yPoints, 3);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawPolygon(xPoints, yPoints, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawUpperDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
|
||||||
|
int[] xPoints = new int[]{x + 110, x + 110, x + 80};
|
||||||
|
int[] yPoints = new int[]{y + 5, y + 15, y + 10};
|
||||||
|
g.setColor(fillColor);
|
||||||
|
g.fillPolygon(xPoints, yPoints, 3);
|
||||||
|
g.setColor(borderColor);
|
||||||
|
g.drawPolygon(xPoints, yPoints, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
|||||||
|
package projectliner.Entities;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
|
public class BaseLinerEntity {
|
||||||
|
private int speed;
|
||||||
|
private double weight;
|
||||||
|
private Color primaryColor;
|
||||||
|
|
||||||
|
public BaseLinerEntity(int speed, double weight, Color primaryColor) {
|
||||||
|
this.speed = speed;
|
||||||
|
this.weight = weight;
|
||||||
|
this.primaryColor = primaryColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSpeed() {
|
||||||
|
return this.speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getWeight() {
|
||||||
|
return this.weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getPrimaryColor() {
|
||||||
|
return this.primaryColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getStep() {
|
||||||
|
return (double)this.speed / (this.weight / (double)100.0F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
53
Liner_Advanced/src/projectliner/Entities/LinerEntity.java
Normal file
53
Liner_Advanced/src/projectliner/Entities/LinerEntity.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package projectliner.Entities;
|
||||||
|
|
||||||
|
import projectliner.Drawings.CircularDeckDrawing;
|
||||||
|
import projectliner.Drawings.IAdditionalElements;
|
||||||
|
import projectliner.Drawings.SquareDeckDrawing;
|
||||||
|
import projectliner.Drawings.TriangularDeckDrawing;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
|
public class LinerEntity extends BaseLinerEntity {
|
||||||
|
private Color secondaryColor;
|
||||||
|
private LinerEntityType type;
|
||||||
|
private int capacity;
|
||||||
|
private int maxPassengers;
|
||||||
|
private boolean hasExtraDeck;
|
||||||
|
private boolean hasPool;
|
||||||
|
|
||||||
|
public LinerEntity(int speed, double weight, Color primaryColor, Color secondaryColor,
|
||||||
|
LinerEntityType type, int capacity, int maxPassengers,
|
||||||
|
boolean hasExtraDeck, boolean hasPool) {
|
||||||
|
super(speed, weight, primaryColor);
|
||||||
|
this.secondaryColor = secondaryColor;
|
||||||
|
this.type = type;
|
||||||
|
this.capacity = capacity;
|
||||||
|
this.maxPassengers = maxPassengers;
|
||||||
|
this.hasExtraDeck = hasExtraDeck;
|
||||||
|
this.hasPool = hasPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getSecondaryColor() {
|
||||||
|
return this.secondaryColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LinerEntityType getType() {
|
||||||
|
return this.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCapacity() {
|
||||||
|
return this.capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxPassengers() {
|
||||||
|
return this.maxPassengers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasExtraDeck() {
|
||||||
|
return this.hasExtraDeck;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPool() {
|
||||||
|
return this.hasPool;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package projectliner.Entities;
|
||||||
|
|
||||||
|
public enum LinerEntityType {
|
||||||
|
PASSENGER,
|
||||||
|
CARGO,
|
||||||
|
MILITARY,
|
||||||
|
MIXED
|
||||||
|
}
|
195
Liner_Advanced/src/projectliner/FormLiner.java
Normal file
195
Liner_Advanced/src/projectliner/FormLiner.java
Normal file
@ -0,0 +1,195 @@
|
|||||||
|
package projectliner;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Random;
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JComboBox;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLayeredPane;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.Timer;
|
||||||
|
import projectliner.Drawings.DirectionType;
|
||||||
|
import projectliner.Drawings.DrawingBaseLiner;
|
||||||
|
import projectliner.Drawings.DrawingLiner;
|
||||||
|
import projectliner.Entities.LinerEntityType;
|
||||||
|
import projectliner.MovementStrategy.AbstractStrategy;
|
||||||
|
import projectliner.MovementStrategy.MoveToBorder;
|
||||||
|
import projectliner.MovementStrategy.MoveToCenter;
|
||||||
|
import projectliner.MovementStrategy.MoveableLiner;
|
||||||
|
import projectliner.MovementStrategy.StrategyStatus;
|
||||||
|
|
||||||
|
public class FormLiner extends JFrame {
|
||||||
|
private DrawingBaseLiner drawingLiner;
|
||||||
|
private JPanel pictureBoxLiner;
|
||||||
|
private Timer moveTimer;
|
||||||
|
private DirectionType currentDirection;
|
||||||
|
private JComboBox<String> comboBoxStrategy;
|
||||||
|
private AbstractStrategy strategy;
|
||||||
|
|
||||||
|
public FormLiner() {
|
||||||
|
this.initializeComponent();
|
||||||
|
this.strategy = null;
|
||||||
|
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeComponent() {
|
||||||
|
JLayeredPane layeredPane = new JLayeredPane();
|
||||||
|
layeredPane.setPreferredSize(new Dimension(900, 500));
|
||||||
|
this.add(layeredPane, "Center");
|
||||||
|
this.pictureBoxLiner = new JPanel() {
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
if (FormLiner.this.drawingLiner != null) {
|
||||||
|
FormLiner.this.drawingLiner.drawTransport(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.pictureBoxLiner.setBounds(0, 0, 900, 500);
|
||||||
|
layeredPane.add(this.pictureBoxLiner, JLayeredPane.DEFAULT_LAYER);
|
||||||
|
|
||||||
|
String[] options = new String[]{"", "To Center", "To Border"};
|
||||||
|
this.comboBoxStrategy = new JComboBox(options);
|
||||||
|
this.comboBoxStrategy.setBounds(800, 10, 100, 30);
|
||||||
|
this.comboBoxStrategy.setEnabled(false);
|
||||||
|
this.comboBoxStrategy.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
String selected = (String)FormLiner.this.comboBoxStrategy.getSelectedItem();
|
||||||
|
System.out.println(selected);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
layeredPane.add(this.comboBoxStrategy, JLayeredPane.PALETTE_LAYER);
|
||||||
|
JButton buttonStrategyStep = new JButton("Step");
|
||||||
|
buttonStrategyStep.setBounds(830, 50, 70, 30);
|
||||||
|
buttonStrategyStep.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
FormLiner.this.buttonStrategyStep_Click(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
layeredPane.add(buttonStrategyStep, JLayeredPane.PALETTE_LAYER);
|
||||||
|
|
||||||
|
URL upIconURL = this.getClass().getClassLoader().getResource("icons8-arrow-up-60.png");
|
||||||
|
URL downIconURL = this.getClass().getClassLoader().getResource("icons8-arrow-down-60.png");
|
||||||
|
URL leftIconURL = this.getClass().getClassLoader().getResource("icons8-arrow-left-60.png");
|
||||||
|
URL rightIconURL = this.getClass().getClassLoader().getResource("icons8-arrow-right-60.png");
|
||||||
|
ImageIcon upIcon = new ImageIcon(upIconURL);
|
||||||
|
ImageIcon downIcon = new ImageIcon(downIconURL);
|
||||||
|
ImageIcon leftIcon = new ImageIcon(leftIconURL);
|
||||||
|
ImageIcon rightIcon = new ImageIcon(rightIconURL);
|
||||||
|
JButton buttonMoveUp = new JButton(upIcon);
|
||||||
|
buttonMoveUp.setBounds(660, 440, 60, 60);
|
||||||
|
buttonMoveUp.setName("buttonMoveUp");
|
||||||
|
this.addMoveButtonListeners(buttonMoveUp, DirectionType.UP);
|
||||||
|
layeredPane.add(buttonMoveUp, JLayeredPane.PALETTE_LAYER);
|
||||||
|
JButton buttonMoveDown = new JButton(downIcon);
|
||||||
|
buttonMoveDown.setBounds(720, 440, 60, 60);
|
||||||
|
buttonMoveDown.setName("buttonMoveDown");
|
||||||
|
this.addMoveButtonListeners(buttonMoveDown, DirectionType.DOWN);
|
||||||
|
layeredPane.add(buttonMoveDown, JLayeredPane.PALETTE_LAYER);
|
||||||
|
JButton buttonMoveLeft = new JButton(leftIcon);
|
||||||
|
buttonMoveLeft.setBounds(780, 440, 60, 60);
|
||||||
|
buttonMoveLeft.setName("buttonMoveLeft");
|
||||||
|
this.addMoveButtonListeners(buttonMoveLeft, DirectionType.LEFT);
|
||||||
|
layeredPane.add(buttonMoveLeft, JLayeredPane.PALETTE_LAYER);
|
||||||
|
JButton buttonMoveRight = new JButton(rightIcon);
|
||||||
|
buttonMoveRight.setBounds(840, 440, 60, 60);
|
||||||
|
buttonMoveRight.setName("buttonMoveRight");
|
||||||
|
this.addMoveButtonListeners(buttonMoveRight, DirectionType.RIGHT);
|
||||||
|
layeredPane.add(buttonMoveRight, JLayeredPane.PALETTE_LAYER);
|
||||||
|
this.moveTimer = new Timer(100, new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
FormLiner.this.moveTransport(FormLiner.this.currentDirection);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.pack();
|
||||||
|
this.setDefaultCloseOperation(3);
|
||||||
|
this.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addMoveButtonListeners(JButton button, final DirectionType direction) {
|
||||||
|
button.addMouseListener(new MouseAdapter() {
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
FormLiner.this.currentDirection = direction;
|
||||||
|
FormLiner.this.moveTimer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
FormLiner.this.moveTimer.stop();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void draw() {
|
||||||
|
if (this.drawingLiner != null) {
|
||||||
|
BufferedImage bitmap = new BufferedImage(this.pictureBoxLiner.getWidth(),
|
||||||
|
this.pictureBoxLiner.getHeight(), 2);
|
||||||
|
Graphics graphics = bitmap.getGraphics();
|
||||||
|
graphics.setColor(this.pictureBoxLiner.getBackground());
|
||||||
|
graphics.fillRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
|
||||||
|
this.drawingLiner.drawTransport(graphics);
|
||||||
|
this.pictureBoxLiner.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveTransport(DirectionType direction) {
|
||||||
|
if (this.drawingLiner != null) {
|
||||||
|
boolean var10000;
|
||||||
|
switch (direction) {
|
||||||
|
case UP -> var10000 = this.drawingLiner.moveTransport(DirectionType.UP);
|
||||||
|
case DOWN -> var10000 = this.drawingLiner.moveTransport(DirectionType.DOWN);
|
||||||
|
case LEFT -> var10000 = this.drawingLiner.moveTransport(DirectionType.LEFT);
|
||||||
|
case RIGHT -> var10000 = this.drawingLiner.moveTransport(DirectionType.RIGHT);
|
||||||
|
case NONE -> var10000 = false;
|
||||||
|
default -> throw new MatchException((String)null, (Throwable)null);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean result = var10000;
|
||||||
|
if (result) {
|
||||||
|
this.draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buttonStrategyStep_Click(ActionEvent e) {
|
||||||
|
if (this.drawingLiner != null) {
|
||||||
|
if (this.comboBoxStrategy.isEnabled() &&
|
||||||
|
this.comboBoxStrategy.getSelectedIndex() == 1) {
|
||||||
|
this.strategy = new MoveToCenter();
|
||||||
|
} else if (this.comboBoxStrategy.isEnabled() &&
|
||||||
|
this.comboBoxStrategy.getSelectedIndex() == 2) {
|
||||||
|
this.strategy = new MoveToBorder();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.strategy.setData(new MoveableLiner(this.drawingLiner), 900, 500);
|
||||||
|
this.comboBoxStrategy.setSelectedIndex(0);
|
||||||
|
this.comboBoxStrategy.setEnabled(false);
|
||||||
|
this.strategy.makeStep();
|
||||||
|
this.draw();
|
||||||
|
if (this.strategy.getStatus() == StrategyStatus.FINISHED) {
|
||||||
|
this.comboBoxStrategy.setEnabled(true);
|
||||||
|
this.strategy = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLiner(DrawingBaseLiner liner) {
|
||||||
|
this.drawingLiner = liner;
|
||||||
|
drawingLiner.setPosition(0, 0);
|
||||||
|
drawingLiner.setPictureSize(pictureBoxLiner.getWidth(), pictureBoxLiner.getHeight());
|
||||||
|
comboBoxStrategy.setEnabled(true);
|
||||||
|
strategy = null;
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
}
|
131
Liner_Advanced/src/projectliner/FormLinerGenerator.java
Normal file
131
Liner_Advanced/src/projectliner/FormLinerGenerator.java
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
package projectliner;
|
||||||
|
|
||||||
|
import projectliner.Drawings.*;
|
||||||
|
import projectliner.Entities.BaseLinerEntity;
|
||||||
|
import projectliner.Entities.LinerEntity;
|
||||||
|
import projectliner.Entities.LinerEntityType;
|
||||||
|
import projectliner.GenericObjectsCollection.GenericLinerModelsCollection;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.LineBorder;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class FormLinerGenerator extends JFrame {
|
||||||
|
private GenericLinerModelsCollection<BaseLinerEntity, IAdditionalElements> collection;
|
||||||
|
private JTextArea textAreaShips;
|
||||||
|
private JTextArea textAreaForms;
|
||||||
|
private JLabel pictureBox;
|
||||||
|
private FormShipCollection parent;
|
||||||
|
private DrawingLiner drawing;
|
||||||
|
|
||||||
|
public FormLinerGenerator(FormShipCollection parent) {
|
||||||
|
setTitle("Liner Generator");
|
||||||
|
setSize(800, 600);
|
||||||
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
this.parent = parent;
|
||||||
|
|
||||||
|
collection = new GenericLinerModelsCollection<>();
|
||||||
|
|
||||||
|
textAreaShips = new JTextArea(10, 30);
|
||||||
|
textAreaForms = new JTextArea(10, 30);
|
||||||
|
|
||||||
|
pictureBox = new JLabel();
|
||||||
|
pictureBox.setPreferredSize(new Dimension(800, 400));
|
||||||
|
pictureBox.setBorder(new LineBorder(Color.BLACK));
|
||||||
|
|
||||||
|
JPanel panelLists = new JPanel(new GridBagLayout());
|
||||||
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
|
gbc.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc.insets = new Insets(5, 5, 5, 5);
|
||||||
|
|
||||||
|
JLabel labelShips = new JLabel("Entities");
|
||||||
|
gbc.gridx = 0;
|
||||||
|
gbc.gridy = 0;
|
||||||
|
panelLists.add(labelShips, gbc);
|
||||||
|
|
||||||
|
JLabel labelForms = new JLabel("Additional Elements");
|
||||||
|
gbc.gridx = 1;
|
||||||
|
gbc.gridy = 0;
|
||||||
|
panelLists.add(labelForms, gbc);
|
||||||
|
|
||||||
|
gbc.gridx = 0;
|
||||||
|
gbc.gridy = 1;
|
||||||
|
gbc.weightx = 0.5;
|
||||||
|
gbc.weighty = 1.0;
|
||||||
|
panelLists.add(new JScrollPane(textAreaShips), gbc);
|
||||||
|
|
||||||
|
gbc.gridx = 1;
|
||||||
|
gbc.gridy = 1;
|
||||||
|
panelLists.add(new JScrollPane(textAreaForms), gbc);
|
||||||
|
|
||||||
|
JButton buttonGenerate = new JButton("Generate Drawing");
|
||||||
|
buttonGenerate.addActionListener(e -> generateDrawing());
|
||||||
|
buttonGenerate.setPreferredSize(new Dimension(150, 50));
|
||||||
|
|
||||||
|
JButton buttonSend = new JButton("Send to Collection");
|
||||||
|
buttonSend.addActionListener(e -> sendToCollection());
|
||||||
|
buttonSend.setPreferredSize(new Dimension(150, 50));
|
||||||
|
|
||||||
|
add(panelLists, BorderLayout.NORTH);
|
||||||
|
add(buttonGenerate, BorderLayout.SOUTH);
|
||||||
|
add(buttonSend, BorderLayout.WEST);
|
||||||
|
add(pictureBox, BorderLayout.CENTER);
|
||||||
|
pack();
|
||||||
|
generateRandomObjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateRandomObjects() {
|
||||||
|
Random rnd = new Random();
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
int linerType = rnd.nextInt(2);
|
||||||
|
|
||||||
|
switch (linerType) {
|
||||||
|
case 0 -> generateRandomLiner();
|
||||||
|
case 1 -> generateRandomShip();
|
||||||
|
}
|
||||||
|
IAdditionalElements form;
|
||||||
|
switch (rnd.nextInt(3)) {
|
||||||
|
case 0 -> form = new TriangularDeckDrawing();
|
||||||
|
case 1 -> form = new CircularDeckDrawing();
|
||||||
|
default -> form = new SquareDeckDrawing();
|
||||||
|
}
|
||||||
|
collection.addForm(form);
|
||||||
|
}
|
||||||
|
textAreaShips.setText(collection.getShipInfo());
|
||||||
|
textAreaForms.setText(collection.getFormInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateRandomShip() {
|
||||||
|
Random rnd = new Random();
|
||||||
|
BaseLinerEntity ship = new LinerEntity(rnd.nextInt(200) + 100, rnd.nextInt(2000) + 1000,
|
||||||
|
new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)),
|
||||||
|
new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)),
|
||||||
|
LinerEntityType.values()[rnd.nextInt(LinerEntityType.values().length)],
|
||||||
|
rnd.nextInt(9000) + 1000, rnd.nextInt(90) + 10,
|
||||||
|
rnd.nextBoolean(), rnd.nextBoolean());
|
||||||
|
collection.addShip(ship);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateRandomLiner() {
|
||||||
|
Random rnd = new Random();
|
||||||
|
BaseLinerEntity liner = new BaseLinerEntity(rnd.nextInt(100), rnd.nextDouble(),
|
||||||
|
new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)));
|
||||||
|
collection.addShip(liner);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateDrawing() {
|
||||||
|
this.drawing = collection.createRandomShip();
|
||||||
|
drawing.setPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
|
||||||
|
drawing.setPosition((pictureBox.getWidth() - drawing.getWidth()) / 2,
|
||||||
|
(pictureBox.getHeight() - drawing.getHeight()) / 2);
|
||||||
|
pictureBox.setIcon(new ImageIcon(drawing.drawTransport()));
|
||||||
|
pictureBox.revalidate();
|
||||||
|
pictureBox.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendToCollection() {
|
||||||
|
parent.addGeneratedShip(this.drawing);
|
||||||
|
}
|
||||||
|
}
|
467
Liner_Advanced/src/projectliner/FormShipCollection.java
Normal file
467
Liner_Advanced/src/projectliner/FormShipCollection.java
Normal file
@ -0,0 +1,467 @@
|
|||||||
|
package projectliner;
|
||||||
|
|
||||||
|
import projectliner.GenericObjectsCollection.*;
|
||||||
|
import projectliner.Drawings.*;
|
||||||
|
import projectliner.Entities.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
import javax.swing.text.MaskFormatter;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class FormShipCollection extends JFrame {
|
||||||
|
private AbstractCompany company = null;
|
||||||
|
private JComboBox<String> comboBoxCompanySelector;
|
||||||
|
private JButton buttonAddLiner;
|
||||||
|
private JButton buttonAddShip;
|
||||||
|
private JButton buttonRemoveShip;
|
||||||
|
private JButton buttonSubmitForTesting;
|
||||||
|
private JButton buttonRefresh;
|
||||||
|
private JButton buttonCreateRandomShip;
|
||||||
|
private JTextField textFieldPosition;
|
||||||
|
private JLabel pictureBox;
|
||||||
|
private final CollectionStorage<DrawingBaseLiner> collectionStorage;
|
||||||
|
private JPanel collectionStorages;
|
||||||
|
private JPanel controlPanel;
|
||||||
|
private JButton buttonAddCollection;
|
||||||
|
private JButton buttonRemoveCollection;
|
||||||
|
private JButton buttonCreateCompany;
|
||||||
|
private JTextField textBoxCollectionName;
|
||||||
|
private JRadioButton radioButtonList;
|
||||||
|
private JRadioButton radioButtonArray;
|
||||||
|
private JList<String> listBoxCollections;
|
||||||
|
private JButton buttonShowRemovedObjects;
|
||||||
|
|
||||||
|
public FormShipCollection() {
|
||||||
|
setTitle("FormShipCollection");
|
||||||
|
setSize(1300, 800);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
collectionStorage = new CollectionStorage<>();
|
||||||
|
|
||||||
|
comboBoxCompanySelector = new JComboBox<>(new String[]{"", "Landing Stage"});
|
||||||
|
comboBoxCompanySelector.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
comboBoxCompanySelector.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
controlPanel.setEnabled(false);
|
||||||
|
for (Component component : controlPanel.getComponents()) {
|
||||||
|
component.setEnabled(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonAddLiner = new JButton("Add Liner");
|
||||||
|
buttonAddLiner.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonAddLiner.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
createObject("DrawingBaseLiner");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonAddShip = new JButton("Add Ship");
|
||||||
|
buttonAddShip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonAddShip.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
createObject("DrawingLiner");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonCreateRandomShip = new JButton("Create Random Ship");
|
||||||
|
buttonCreateRandomShip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonCreateRandomShip.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
shipConstructor();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonRemoveShip = new JButton("Remove Ship");
|
||||||
|
buttonRemoveShip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonRemoveShip.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
removeShip();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonSubmitForTesting = new JButton("Submit for Testing");
|
||||||
|
buttonSubmitForTesting.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonSubmitForTesting.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
submitForTesting();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonRefresh = new JButton("Refresh");
|
||||||
|
buttonRefresh.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonRefresh.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonAddCollection = new JButton("Add Collection");
|
||||||
|
buttonAddCollection.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonAddCollection.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
addCollection();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonRemoveCollection = new JButton("Remove Collection");
|
||||||
|
buttonRemoveCollection.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonRemoveCollection.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
removeCollection();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonCreateCompany = new JButton("Create Company");
|
||||||
|
buttonCreateCompany.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonCreateCompany.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
createCompany();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonShowRemovedObjects = new JButton("Show Removed Objects");
|
||||||
|
buttonShowRemovedObjects.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
|
||||||
|
buttonShowRemovedObjects.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
sendToFormFromRemovedCollection();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
JLabel labelCollectionName = new JLabel("Collection Name:");
|
||||||
|
JLabel labelPosition = new JLabel("Position:");
|
||||||
|
textBoxCollectionName = new JTextField(10);
|
||||||
|
textBoxCollectionName.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
|
||||||
|
radioButtonList = new JRadioButton("List");
|
||||||
|
radioButtonArray = new JRadioButton("Array");
|
||||||
|
ButtonGroup radioGroup = new ButtonGroup();
|
||||||
|
radioGroup.add(radioButtonList);
|
||||||
|
radioGroup.add(radioButtonArray);
|
||||||
|
|
||||||
|
listBoxCollections = new JList<>();
|
||||||
|
listBoxCollections.setMaximumSize(new Dimension(Integer.MAX_VALUE, 150));
|
||||||
|
listBoxCollections.setListData(new String[]{});
|
||||||
|
collectionStorages = new JPanel();
|
||||||
|
collectionStorages.setLayout(new BoxLayout(collectionStorages, BoxLayout.Y_AXIS));
|
||||||
|
collectionStorages.setMaximumSize(new Dimension(200, 500));
|
||||||
|
collectionStorages.setBorder(new EmptyBorder(10, 10, 10, 10));
|
||||||
|
|
||||||
|
labelCollectionName.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
textBoxCollectionName.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
radioButtonList.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
radioButtonArray.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonAddCollection.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
listBoxCollections.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonRemoveCollection.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
comboBoxCompanySelector.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonCreateCompany.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
collectionStorages.add(labelCollectionName);
|
||||||
|
collectionStorages.add(Box.createVerticalStrut(10));
|
||||||
|
collectionStorages.add(textBoxCollectionName);
|
||||||
|
collectionStorages.add(radioButtonList);
|
||||||
|
collectionStorages.add(radioButtonArray);
|
||||||
|
collectionStorages.add(Box.createVerticalStrut(10));
|
||||||
|
collectionStorages.add(buttonAddCollection);
|
||||||
|
collectionStorages.add(Box.createVerticalStrut(10));
|
||||||
|
collectionStorages.add(listBoxCollections);
|
||||||
|
collectionStorages.add(Box.createVerticalStrut(10));
|
||||||
|
collectionStorages.add(buttonRemoveCollection);
|
||||||
|
collectionStorages.add(Box.createVerticalStrut(30));
|
||||||
|
collectionStorages.add(comboBoxCompanySelector);
|
||||||
|
collectionStorages.add(Box.createVerticalStrut(10));
|
||||||
|
collectionStorages.add(buttonCreateCompany);
|
||||||
|
collectionStorages.setEnabled(false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
MaskFormatter maskFormatter = new MaskFormatter("##");
|
||||||
|
maskFormatter.setPlaceholderCharacter('_');
|
||||||
|
textFieldPosition = new JFormattedTextField(maskFormatter);
|
||||||
|
textFieldPosition.setMaximumSize(new Dimension(50,
|
||||||
|
textFieldPosition.getPreferredSize().height));
|
||||||
|
} catch (ParseException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
pictureBox = new JLabel();
|
||||||
|
pictureBox.setPreferredSize(new Dimension(600, 400));
|
||||||
|
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||||
|
|
||||||
|
buttonAddLiner.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonAddShip.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonCreateRandomShip.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
labelPosition.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonRemoveShip.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
textFieldPosition.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonSubmitForTesting.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonShowRemovedObjects.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
buttonRefresh.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
controlPanel = new JPanel();
|
||||||
|
controlPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
||||||
|
controlPanel.setMaximumSize(new Dimension(200, 500));
|
||||||
|
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
|
||||||
|
controlPanel.add(buttonAddLiner);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(10));
|
||||||
|
controlPanel.add(buttonAddShip);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(10));
|
||||||
|
controlPanel.add(buttonCreateRandomShip);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(10));
|
||||||
|
controlPanel.add(buttonRemoveShip);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(10));
|
||||||
|
controlPanel.add(labelPosition);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(5));
|
||||||
|
controlPanel.add(textFieldPosition);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(10));
|
||||||
|
controlPanel.add(buttonSubmitForTesting);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(10));
|
||||||
|
controlPanel.add(buttonShowRemovedObjects);
|
||||||
|
controlPanel.add(Box.createVerticalStrut(10));
|
||||||
|
controlPanel.add(buttonRefresh);
|
||||||
|
|
||||||
|
JPanel rightPanel = new JPanel();
|
||||||
|
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
|
||||||
|
rightPanel.add(collectionStorages);
|
||||||
|
rightPanel.add(controlPanel);
|
||||||
|
|
||||||
|
add(rightPanel, BorderLayout.EAST);
|
||||||
|
add(pictureBox, BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createCompany() {
|
||||||
|
if (listBoxCollections.getSelectedIndex() < 0 || listBoxCollections.getSelectedValue() == null) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Collection is not selected");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
IGenericObjectsCollection<DrawingBaseLiner> collection =
|
||||||
|
collectionStorage.getCollection(listBoxCollections.getSelectedValue());
|
||||||
|
if (collection == null) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Collection is not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (comboBoxCompanySelector.getSelectedItem().toString()) {
|
||||||
|
case "Landing Stage":
|
||||||
|
company = new LandingStage(pictureBox.getWidth(), pictureBox.getHeight(), collection);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
controlPanel.setEnabled(true);
|
||||||
|
for (Component component : controlPanel.getComponents()) {
|
||||||
|
component.setEnabled(true);
|
||||||
|
}
|
||||||
|
refreshListBoxItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendToFormFromRemovedCollection() {
|
||||||
|
IGenericObjectsCollection<DrawingBaseLiner> removedCollection
|
||||||
|
= collectionStorage.getCollection("Removed");
|
||||||
|
if (removedCollection == null || removedCollection.getCount() == 0) {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"No objects in the Removed collection");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String input = JOptionPane.showInputDialog(this,
|
||||||
|
"There are " + removedCollection.getCount() +
|
||||||
|
" objects in the Removed collection.\n" +
|
||||||
|
"Enter the number of the object to send to the form:",
|
||||||
|
"Select Object",
|
||||||
|
JOptionPane.QUESTION_MESSAGE);
|
||||||
|
|
||||||
|
if (input == null || input.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int index;
|
||||||
|
try {
|
||||||
|
index = Integer.parseInt(input);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"Invalid number entered", "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index < 0 || index >= removedCollection.getCount()) {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"Invalid index", "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawingBaseLiner liner = removedCollection.get(index);
|
||||||
|
if (liner != null) {
|
||||||
|
FormLiner form = new FormLiner();
|
||||||
|
form.setLiner(liner);
|
||||||
|
form.setVisible(true);
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"No object found at the specified index", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeCollection() {
|
||||||
|
if (listBoxCollections.getSelectedIndex() < 0 ||
|
||||||
|
listBoxCollections.getSelectedValue() == null) {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"Collection is not selected", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String collectionName = listBoxCollections.getSelectedValue();
|
||||||
|
if (JOptionPane.showConfirmDialog(this,
|
||||||
|
"Are you sure you want to remove the collection '" + collectionName + "'?",
|
||||||
|
"Confirm Removal", JOptionPane.YES_NO_OPTION,
|
||||||
|
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
|
||||||
|
collectionStorage.delCollection(collectionName);
|
||||||
|
refreshListBoxItems();
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"Collection removed successfully", "Success",
|
||||||
|
JOptionPane.OK_OPTION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addCollection() {
|
||||||
|
if (textBoxCollectionName.getText().isEmpty() ||
|
||||||
|
(!radioButtonList.isSelected() && !radioButtonArray.isSelected())) {
|
||||||
|
JOptionPane.showMessageDialog(this,
|
||||||
|
"Please fill all fields", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CollectionType collectionType = CollectionType.NONE;
|
||||||
|
if (radioButtonArray.isSelected()) {
|
||||||
|
collectionType = CollectionType.ARRAY;
|
||||||
|
} else if (radioButtonList.isSelected()) {
|
||||||
|
collectionType = CollectionType.LIST;
|
||||||
|
}
|
||||||
|
collectionStorage.addCollection(textBoxCollectionName.getText(), collectionType);
|
||||||
|
refreshListBoxItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshListBoxItems() {
|
||||||
|
DefaultListModel<String> listModel = new DefaultListModel<>();
|
||||||
|
for (String colName : collectionStorage.getKeys()) {
|
||||||
|
if (colName != null && !colName.isEmpty()) {
|
||||||
|
listModel.addElement(colName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
listBoxCollections.setModel(listModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createObject(String type) {
|
||||||
|
if (company == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Random random = new Random();
|
||||||
|
DrawingBaseLiner drawingLiner;
|
||||||
|
switch (type) {
|
||||||
|
case "DrawingBaseLiner":
|
||||||
|
drawingLiner = new DrawingBaseLiner(random.nextInt(200) + 100,
|
||||||
|
random.nextInt(2000) + 1000, getColor(random));
|
||||||
|
break;
|
||||||
|
case "DrawingLiner":
|
||||||
|
drawingLiner = new DrawingLiner(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
|
||||||
|
getColor(random), getColor(random),
|
||||||
|
LinerEntityType.CARGO, random.nextInt(9000) + 1000, random.nextInt(90) + 10,
|
||||||
|
random.nextBoolean(), random.nextBoolean(), 3, "Square Deck");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (AbstractCompany.add(company, drawingLiner)) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Object was added");
|
||||||
|
pictureBox.setIcon(new ImageIcon(company.show()));
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Couldn't add object");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color getColor(Random random) {
|
||||||
|
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
||||||
|
JColorChooser colorChooser = new JColorChooser(color);
|
||||||
|
JDialog dialog = JColorChooser.createDialog(this, "Choose Color", true, colorChooser, null, null);
|
||||||
|
dialog.setVisible(true);
|
||||||
|
return colorChooser.getColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeShip() {
|
||||||
|
if (textFieldPosition.getText().isEmpty() || company == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (collectionStorage.getCollection("Removed") == null) {
|
||||||
|
collectionStorage.addCollection("Removed", CollectionType.LINKED_LIST);
|
||||||
|
}
|
||||||
|
int pos = Integer.parseInt(textFieldPosition.getText());
|
||||||
|
if (JOptionPane.showConfirmDialog(this,
|
||||||
|
"Remove object?", "Removing...",
|
||||||
|
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
|
||||||
|
IGenericObjectsCollection<DrawingBaseLiner> removedCollection
|
||||||
|
= collectionStorage.getCollection("Removed");
|
||||||
|
DrawingBaseLiner removedObject = collectionStorage.getObject(listBoxCollections.getSelectedValue(), pos);
|
||||||
|
if (removedObject != null) {
|
||||||
|
removedCollection.insert(removedObject);
|
||||||
|
}
|
||||||
|
if (AbstractCompany.remove(company, pos)) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Object was removed");
|
||||||
|
pictureBox.setIcon(new ImageIcon(company.show()));
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Couldn't remove object");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void submitForTesting() {
|
||||||
|
if (company == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
DrawingBaseLiner liner = null;
|
||||||
|
int counter = 100;
|
||||||
|
while (liner == null && counter > 0) {
|
||||||
|
liner = company.getRandomObject();
|
||||||
|
counter--;
|
||||||
|
}
|
||||||
|
if (liner == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FormLiner form = new FormLiner();
|
||||||
|
form.setLiner(liner);
|
||||||
|
form.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refresh() {
|
||||||
|
if (company == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBox.setIcon(new ImageIcon(company.show()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void shipConstructor() {
|
||||||
|
FormLinerGenerator form = new FormLinerGenerator(FormShipCollection.this);
|
||||||
|
form.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addGeneratedShip(DrawingLiner liner) {
|
||||||
|
AbstractCompany.add(company, liner);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,58 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
import projectliner.Drawings.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public abstract class AbstractCompany {
|
||||||
|
protected final int placeSizeWidth = 210;
|
||||||
|
protected final int placeSizeHeight = 80;
|
||||||
|
protected final int pictureWidth;
|
||||||
|
protected final int pictureHeight;
|
||||||
|
protected IGenericObjectsCollection<DrawingBaseLiner> collection = null;
|
||||||
|
|
||||||
|
private int getMaxCount() {
|
||||||
|
return pictureWidth * pictureHeight / (placeSizeWidth * placeSizeHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractCompany(int picWidth, int picHeight, IGenericObjectsCollection<DrawingBaseLiner> collection) {
|
||||||
|
this.pictureWidth = picWidth;
|
||||||
|
this.pictureHeight = picHeight;
|
||||||
|
this.collection = collection;
|
||||||
|
this.collection.setMaxCount(getMaxCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean add(AbstractCompany company, DrawingBaseLiner liner) {
|
||||||
|
return company.collection != null && company.collection.insert(liner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean remove(AbstractCompany company, int position) {
|
||||||
|
return company.collection != null && company.collection.remove(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingBaseLiner getRandomObject() {
|
||||||
|
Random rnd = new Random();
|
||||||
|
return collection != null ? collection.get(rnd.nextInt(getMaxCount())) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BufferedImage show() {
|
||||||
|
BufferedImage bitmap = new BufferedImage(pictureWidth, pictureHeight, 2);
|
||||||
|
Graphics graphics = bitmap.getGraphics();
|
||||||
|
drawBackground(graphics);
|
||||||
|
setObjectsPosition();
|
||||||
|
if (collection != null) {
|
||||||
|
for (int i = 0; i < collection.getCount(); ++i) {
|
||||||
|
DrawingBaseLiner obj = collection.get(i);
|
||||||
|
if (obj != null) {
|
||||||
|
obj.drawTransport(graphics);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void drawBackground(Graphics g);
|
||||||
|
protected abstract void setObjectsPosition();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CollectionStorage<T> {
|
||||||
|
private final Map<String, IGenericObjectsCollection<T>> storages;
|
||||||
|
|
||||||
|
public CollectionStorage() {
|
||||||
|
this.storages = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getKeys() {
|
||||||
|
return new ArrayList<>(storages.keySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCollection(String name, CollectionType collectionType) {
|
||||||
|
if (name == null || name.isEmpty() || storages.containsKey(name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (collectionType) {
|
||||||
|
case ARRAY:
|
||||||
|
storages.put(name, new GenericObjectsCollection<>());
|
||||||
|
break;
|
||||||
|
case LIST:
|
||||||
|
storages.put(name, new ListGenericObjects<>());
|
||||||
|
break;
|
||||||
|
case LINKED_LIST:
|
||||||
|
storages.put(name, new LinkedListGenericObjects<>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delCollection(String name) {
|
||||||
|
if (name == null || name.isEmpty() || !storages.containsKey(name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
storages.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IGenericObjectsCollection<T> getCollection(String name) {
|
||||||
|
return storages.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getObject(String name, int position) {
|
||||||
|
IGenericObjectsCollection<T> collection = storages.get(name);
|
||||||
|
if (collection == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return collection.get(position);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
public enum CollectionType {
|
||||||
|
NONE,
|
||||||
|
ARRAY,
|
||||||
|
LIST,
|
||||||
|
LINKED_LIST
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
import projectliner.Drawings.*;
|
||||||
|
import projectliner.Entities.BaseLinerEntity;
|
||||||
|
import projectliner.Entities.LinerEntity;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class GenericLinerModelsCollection <T extends BaseLinerEntity,
|
||||||
|
E extends IAdditionalElements> {
|
||||||
|
private List<T> collectionShips;
|
||||||
|
private List<E> collectionForms;
|
||||||
|
|
||||||
|
public GenericLinerModelsCollection() {
|
||||||
|
collectionShips = new ArrayList<>();
|
||||||
|
collectionForms = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addShip(T ship) {
|
||||||
|
collectionShips.add(ship);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addForm(E form) {
|
||||||
|
collectionForms.add(form);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingLiner createRandomShip() {
|
||||||
|
Random rnd = new Random();
|
||||||
|
int shipIndex = rnd.nextInt(collectionShips.size());
|
||||||
|
int formIndex = rnd.nextInt(collectionForms.size());
|
||||||
|
|
||||||
|
T selectedShip = collectionShips.get(shipIndex);
|
||||||
|
E selectedForm = collectionForms.get(formIndex);
|
||||||
|
|
||||||
|
return new DrawingLiner(selectedShip, selectedForm, rnd.nextInt(2) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getShipInfo() {
|
||||||
|
StringBuilder shipInfo = new StringBuilder();
|
||||||
|
for (T ship : collectionShips) {
|
||||||
|
if (ship instanceof LinerEntity liner) {
|
||||||
|
shipInfo.append("Ship: ").append("\n");
|
||||||
|
shipInfo.append("Speed: ").append(liner.getSpeed()).append(",\n");
|
||||||
|
shipInfo.append("Weight: ").append(liner.getWeight()).append(",\n");
|
||||||
|
shipInfo.append("Primary color: ").append(liner.getPrimaryColor()).append(",\n");
|
||||||
|
shipInfo.append("Secondary color: ").append(liner.getSecondaryColor()).append(",\n");
|
||||||
|
shipInfo.append("Type: ").append(liner.getType()).append(",\n");
|
||||||
|
shipInfo.append("Capacity: ").append(liner.getCapacity()).append(",\n");
|
||||||
|
shipInfo.append("Max passengers: ").append(liner.getMaxPassengers()).append(",\n");
|
||||||
|
shipInfo.append("Has extra deck: ").append(liner.hasExtraDeck()).append(",\n");
|
||||||
|
shipInfo.append("Has pool: ").append(liner.hasPool()).append(",\n");
|
||||||
|
} else {
|
||||||
|
shipInfo.append("Liner: ").append("\n");
|
||||||
|
shipInfo.append("Speed: ").append(ship.getSpeed()).append(",\n");
|
||||||
|
shipInfo.append("Weight: ").append(ship.getWeight()).append(",\n");
|
||||||
|
shipInfo.append("Primary color: ").append(ship.getPrimaryColor()).append(",\n");
|
||||||
|
}
|
||||||
|
shipInfo.append("\n\n");
|
||||||
|
}
|
||||||
|
return shipInfo.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFormInfo() {
|
||||||
|
StringBuilder formInfo = new StringBuilder();
|
||||||
|
for (E deck : collectionForms) {
|
||||||
|
if (deck instanceof TriangularDeckDrawing) {
|
||||||
|
formInfo.append("Triangular deck").append("\n");
|
||||||
|
} else if (deck instanceof CircularDeckDrawing) {
|
||||||
|
formInfo.append("Circular deck").append("\n");
|
||||||
|
} else if (deck instanceof SquareDeckDrawing) {
|
||||||
|
formInfo.append("Square deck").append("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return formInfo.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,86 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class GenericObjectsCollection<T> implements IGenericObjectsCollection<T> {
|
||||||
|
private T[] collection;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public GenericObjectsCollection() {
|
||||||
|
collection = (T[]) new Object[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCount() {
|
||||||
|
return collection.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMaxCount(int maxCount) {
|
||||||
|
if (maxCount > 0) {
|
||||||
|
if (collection.length > 0) {
|
||||||
|
collection = Arrays.copyOf(collection, maxCount);
|
||||||
|
} else {
|
||||||
|
collection = (T[]) new Object[maxCount];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T get(int position) {
|
||||||
|
if (position < 0 || position >= collection.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return collection[position];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insert(T obj) {
|
||||||
|
for (int i = 0; i < collection.length; ++i) {
|
||||||
|
if (collection[i] == null) {
|
||||||
|
collection[i] = obj;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insert(T obj, int position) {
|
||||||
|
if (position < 0 || position >= collection.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (collection[position] == null) {
|
||||||
|
collection[position] = obj;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
int originalPosition = position;
|
||||||
|
while (position < collection.length) {
|
||||||
|
if (collection[position] == null) {
|
||||||
|
collection[position] = obj;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
position = originalPosition;
|
||||||
|
while (position >= 0) {
|
||||||
|
if (collection[position] == null) {
|
||||||
|
collection[position] = obj;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
position--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(int position) {
|
||||||
|
if (position < 0 || position >= collection.length || collection[position] == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
collection[position] = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
public interface IGenericObjectsCollection<T> {
|
||||||
|
int getCount();
|
||||||
|
void setMaxCount(int maxCount);
|
||||||
|
boolean insert(T obj);
|
||||||
|
boolean insert(T obj, int position);
|
||||||
|
boolean remove(int position);
|
||||||
|
T get(int position);
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
import projectliner.Drawings.*;
|
||||||
|
import projectliner.GenericObjectsCollection.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.Point;
|
||||||
|
|
||||||
|
public class LandingStage extends AbstractCompany {
|
||||||
|
public LandingStage(int picWidth, int picHeight,
|
||||||
|
IGenericObjectsCollection<DrawingBaseLiner> collection) {
|
||||||
|
super(picWidth, picHeight, collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void drawBackground(Graphics g) {
|
||||||
|
int spacing = 20;
|
||||||
|
|
||||||
|
int horizontalCount = pictureWidth / placeSizeWidth;
|
||||||
|
int verticalCount = pictureHeight / placeSizeHeight;
|
||||||
|
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setStroke(new BasicStroke(3));
|
||||||
|
g2d.setColor(Color.BLACK);
|
||||||
|
|
||||||
|
for (int i = 0; i < horizontalCount; i++) {
|
||||||
|
for (int j = 0; j < verticalCount; j++) {
|
||||||
|
int x = i * placeSizeWidth;
|
||||||
|
int y = j * placeSizeHeight;
|
||||||
|
Point[] parkingPlace = {
|
||||||
|
new Point(x + placeSizeWidth - spacing, y),
|
||||||
|
new Point(x, y),
|
||||||
|
new Point(x, y + placeSizeHeight),
|
||||||
|
new Point(x + placeSizeWidth - spacing, y + placeSizeHeight),
|
||||||
|
new Point(x, y + placeSizeHeight),
|
||||||
|
new Point(x, y),
|
||||||
|
};
|
||||||
|
g2d.drawPolygon(new int[]{parkingPlace[0].x, parkingPlace[1].x,
|
||||||
|
parkingPlace[2].x,parkingPlace [3].x,
|
||||||
|
parkingPlace[4].x, parkingPlace[5].x},
|
||||||
|
new int[]{parkingPlace [0].y, parkingPlace [1].y,
|
||||||
|
parkingPlace[2].y, parkingPlace[3].y,
|
||||||
|
parkingPlace[4].y, parkingPlace[5].y}, 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setObjectsPosition() {
|
||||||
|
if (collection == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int spacing = 10;
|
||||||
|
|
||||||
|
int horizontalCount = pictureWidth / placeSizeWidth;
|
||||||
|
int verticalCount = pictureHeight / placeSizeHeight;
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < horizontalCount; i++) {
|
||||||
|
for (int j = verticalCount - 1; j >= 0; j--) {
|
||||||
|
if (index >= collection.getCount()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawingBaseLiner obj = collection.get(index);
|
||||||
|
if (obj != null) {
|
||||||
|
obj.setPictureSize(pictureWidth, pictureHeight);
|
||||||
|
int x = i * placeSizeWidth;
|
||||||
|
int y = j * placeSizeHeight;
|
||||||
|
obj.setPosition(x + spacing, y + spacing);
|
||||||
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class LinkedListGenericObjects<T> implements IGenericObjectsCollection<T> {
|
||||||
|
private final LinkedList<T> collection;
|
||||||
|
private int maxCount;
|
||||||
|
|
||||||
|
public LinkedListGenericObjects() {
|
||||||
|
this.collection = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCount() {
|
||||||
|
return collection.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMaxCount(int maxCount) {
|
||||||
|
if (maxCount > 0) {
|
||||||
|
this.maxCount = maxCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insert(T obj) {
|
||||||
|
collection.add(obj);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insert(T obj, int position) {
|
||||||
|
if (position < 0 || position >= maxCount) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position >= collection.size()) {
|
||||||
|
collection.addAll(new LinkedList<>(collection.subList(collection.size(), position)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collection.get(position) == null) {
|
||||||
|
collection.set(position, obj);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(int position) {
|
||||||
|
if (position < 0 || position >= collection.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
collection.remove(position);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T get(int position) {
|
||||||
|
if (position < 0 || position >= collection.size()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return collection.get(position);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package projectliner.GenericObjectsCollection;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ListGenericObjects<T> implements IGenericObjectsCollection<T> {
|
||||||
|
private final List<T> collection;
|
||||||
|
private int maxCount;
|
||||||
|
|
||||||
|
public ListGenericObjects() {
|
||||||
|
this.collection = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCount() {
|
||||||
|
return collection.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMaxCount(int maxCount) {
|
||||||
|
if (maxCount > 0) {
|
||||||
|
this.maxCount = maxCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T get(int position) {
|
||||||
|
if (position < 0 || position >= collection.size()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return collection.get(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insert(T obj) {
|
||||||
|
if (collection.size() < maxCount) {
|
||||||
|
collection.add(obj);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insert(T obj, int position) {
|
||||||
|
if (position < 0 || position >= maxCount) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position >= collection.size()) {
|
||||||
|
collection.addAll(Collections.nCopies(position - collection.size() + 1, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collection.get(position) == null) {
|
||||||
|
collection.set(position, obj);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(int position) {
|
||||||
|
if (position < 0 || position >= collection.size() || collection.get(position) == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
collection.remove(position);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
public abstract class AbstractStrategy {
|
||||||
|
private IMoveableObject moveableObject;
|
||||||
|
private StrategyStatus state;
|
||||||
|
protected int fieldWidth;
|
||||||
|
protected int fieldHeight;
|
||||||
|
|
||||||
|
public AbstractStrategy() {
|
||||||
|
this.state = StrategyStatus.NOT_INIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StrategyStatus getStatus() {
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(IMoveableObject moveableObject, int width, int height) {
|
||||||
|
if (moveableObject == null) {
|
||||||
|
this.state = StrategyStatus.NOT_INIT;
|
||||||
|
} else {
|
||||||
|
this.state = StrategyStatus.IN_PROGRESS;
|
||||||
|
this.moveableObject = moveableObject;
|
||||||
|
this.fieldWidth = width;
|
||||||
|
this.fieldHeight = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void makeStep() {
|
||||||
|
if (this.state == StrategyStatus.IN_PROGRESS) {
|
||||||
|
if (this.isTargetDestination()) {
|
||||||
|
this.state = StrategyStatus.FINISHED;
|
||||||
|
} else {
|
||||||
|
this.moveToTarget();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean moveLeft() {
|
||||||
|
return this.moveTo(MovementDirection.LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean moveRight() {
|
||||||
|
return this.moveTo(MovementDirection.RIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean moveUp() {
|
||||||
|
return this.moveTo(MovementDirection.UP);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean moveDown() {
|
||||||
|
return this.moveTo(MovementDirection.DOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ObjectParameters getObjectParameters() {
|
||||||
|
return this.moveableObject != null ? this.moveableObject.getObjectPosition() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Integer getStep() {
|
||||||
|
if (this.state != StrategyStatus.IN_PROGRESS) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return this.moveableObject != null ? this.moveableObject.getStep() : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void moveToTarget();
|
||||||
|
|
||||||
|
protected abstract boolean isTargetDestination();
|
||||||
|
|
||||||
|
private boolean moveTo(MovementDirection movementDirection) {
|
||||||
|
if (this.state != StrategyStatus.IN_PROGRESS) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return this.moveableObject != null && this.moveableObject.tryMoveObject(movementDirection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
public interface IMoveableObject {
|
||||||
|
ObjectParameters getObjectPosition();
|
||||||
|
|
||||||
|
int getStep();
|
||||||
|
|
||||||
|
boolean tryMoveObject(MovementDirection var1);
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
public class MoveToBorder extends AbstractStrategy {
|
||||||
|
public MoveToBorder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void moveToTarget() {
|
||||||
|
ObjectParameters objParams = this.getObjectParameters();
|
||||||
|
if (objParams != null) {
|
||||||
|
if (objParams.getRightBorder() + this.getStep() < this.fieldWidth && objParams.getDownBorder() + this.getStep() < this.fieldHeight) {
|
||||||
|
this.moveRight();
|
||||||
|
this.moveDown();
|
||||||
|
} else if (objParams.getRightBorder() + this.getStep() < this.fieldWidth) {
|
||||||
|
this.moveRight();
|
||||||
|
} else if (objParams.getDownBorder() + this.getStep() < this.fieldHeight) {
|
||||||
|
this.moveDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isTargetDestination() {
|
||||||
|
ObjectParameters objParams = this.getObjectParameters();
|
||||||
|
if (objParams == null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return objParams.getRightBorder() + this.getStep() >= this.fieldWidth && objParams.getDownBorder() + this.getStep() >= this.fieldHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
public class MoveToCenter extends AbstractStrategy {
|
||||||
|
public MoveToCenter() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void moveToTarget() {
|
||||||
|
ObjectParameters objParams = this.getObjectParameters();
|
||||||
|
if (objParams != null) {
|
||||||
|
if (objParams.getObjectMiddleHorizontal() < this.fieldWidth / 2) {
|
||||||
|
if (objParams.getObjectMiddleVertical() < this.fieldHeight / 2) {
|
||||||
|
this.moveRight();
|
||||||
|
this.moveDown();
|
||||||
|
} else {
|
||||||
|
this.moveRight();
|
||||||
|
this.moveUp();
|
||||||
|
}
|
||||||
|
} else if (objParams.getObjectMiddleVertical() < this.fieldHeight / 2) {
|
||||||
|
this.moveLeft();
|
||||||
|
this.moveDown();
|
||||||
|
} else {
|
||||||
|
this.moveLeft();
|
||||||
|
this.moveUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isTargetDestination() {
|
||||||
|
ObjectParameters objParams = this.getObjectParameters();
|
||||||
|
if (objParams == null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return objParams.getObjectMiddleHorizontal() - this.getStep() <= this.fieldWidth / 2 && objParams.getObjectMiddleHorizontal() + this.getStep() >= this.fieldWidth / 2 && objParams.getObjectMiddleVertical() - this.getStep() <= this.fieldHeight / 2 && objParams.getObjectMiddleVertical() + this.getStep() >= this.fieldHeight / 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
import projectliner.Drawings.DirectionType;
|
||||||
|
import projectliner.Drawings.DrawingBaseLiner;
|
||||||
|
|
||||||
|
public class MoveableLiner implements IMoveableObject {
|
||||||
|
private final DrawingBaseLiner liner;
|
||||||
|
|
||||||
|
public MoveableLiner(DrawingBaseLiner liner) {
|
||||||
|
this.liner = liner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectParameters getObjectPosition() {
|
||||||
|
return this.liner != null && this.liner.getBaseLiner() != null && this.liner.getPosX() != null && this.liner.getPosY() != null ? new ObjectParameters(this.liner.getPosX(), this.liner.getPosY(), this.liner.getWidth(), this.liner.getHeight()) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStep() {
|
||||||
|
return (int)(this.liner != null && this.liner.getBaseLiner() != null ? this.liner.getBaseLiner().getStep() : (double)0.0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean tryMoveObject(MovementDirection direction) {
|
||||||
|
return this.liner != null && this.liner.getBaseLiner() != null ? this.liner.moveTransport(getDirectionType(direction)) : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DirectionType getDirectionType(MovementDirection direction) {
|
||||||
|
switch (direction) {
|
||||||
|
case LEFT -> {
|
||||||
|
return DirectionType.LEFT;
|
||||||
|
}
|
||||||
|
case RIGHT -> {
|
||||||
|
return DirectionType.RIGHT;
|
||||||
|
}
|
||||||
|
case UP -> {
|
||||||
|
return DirectionType.UP;
|
||||||
|
}
|
||||||
|
case DOWN -> {
|
||||||
|
return DirectionType.DOWN;
|
||||||
|
}
|
||||||
|
default -> {
|
||||||
|
return DirectionType.NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
public enum MovementDirection {
|
||||||
|
UP(1),
|
||||||
|
DOWN(2),
|
||||||
|
LEFT(3),
|
||||||
|
RIGHT(4);
|
||||||
|
|
||||||
|
private final int value;
|
||||||
|
|
||||||
|
private MovementDirection(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
public class ObjectParameters {
|
||||||
|
private final int x;
|
||||||
|
private final int y;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
|
||||||
|
public ObjectParameters(int x, int y, int width, int height) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRightBorder() {
|
||||||
|
return this.x + this.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLeftBorder() {
|
||||||
|
return this.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTopBorder() {
|
||||||
|
return this.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDownBorder() {
|
||||||
|
return this.y + this.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getObjectMiddleHorizontal() {
|
||||||
|
return this.x + this.width / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getObjectMiddleVertical() {
|
||||||
|
return this.y + this.height / 2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package projectliner.MovementStrategy;
|
||||||
|
|
||||||
|
public enum StrategyStatus {
|
||||||
|
NOT_INIT,
|
||||||
|
IN_PROGRESS,
|
||||||
|
FINISHED;
|
||||||
|
|
||||||
|
private StrategyStatus() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user