This commit is contained in:
frog24 2023-12-28 20:56:39 +04:00
parent f51605506d
commit 9b465c83f0
17 changed files with 611 additions and 140 deletions

2
.idea/misc.xml generated
View File

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

View File

@ -1,2 +1,91 @@
public class AbstractStrategy {
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.GetObjectPosition();
}
protected Integer GetStep() {
if (state != Status.InProgress) {
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;
}
}

View File

@ -1,6 +1,6 @@
import java.awt.*;
public class DrawingDecks {
public class DrawingDecks implements IDrawingDecks{
private DeckType deckType;
private int NumberDecks;
public void SetNumberDecks(int value){

View File

@ -0,0 +1,44 @@
import java.awt.*;
public class DrawingHexagoneDecks implements IDrawingDecks{
private DeckType deckType;
private int NumberDecks;
public void SetNumberDecks(int value){
NumberDecks = value;
switch (value){
case 2:
deckType = DeckType.TwoDecks;
break;
case 3:
deckType = DeckType.ThreeDecks;
break;
default:
deckType = DeckType.OneDeck;
}
};
public void DrawDeck(int x, int y, int width, int height, Graphics g, Color MainColor){
int[] xPoints = {x, x+5, x+width-5, x+width, x+width, x, x};
int[] yPoints = {y+5, y, y, y+5, y+height, y+height, y+5};
g.setColor(MainColor);
g.fillPolygon(xPoints, yPoints, xPoints.length);
g.setColor(Color.black);
g.drawPolygon(xPoints, yPoints, xPoints.length);
}
public void DrawingDecks(int _startPosX, int _startPosY, Color MainColor, Graphics g){
switch (deckType){
case OneDeck:
DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor);
break;
case TwoDecks:
DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor);
DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor);
break;
case ThreeDecks:
DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor);
DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor);
DrawDeck(_startPosX + 50, _startPosY + 14, 40, 8, g, MainColor);
break;
}
}
}

View File

@ -0,0 +1,43 @@
public class DrawingObjectShip implements IMoveableObject{
private DrawingShip _drawingShip = null;
public DrawingObjectShip(DrawingShip drawingShip)
{
_drawingShip = drawingShip;
}
@Override
public ObjectParameters GetObjectPosition()
{
if (_drawingShip == null || _drawingShip.entityShip == null) {
return null;
}
return new ObjectParameters(_drawingShip.GetPosX(), _drawingShip.GetPosY(),
_drawingShip.GetWidth(), _drawingShip.GetHeight());
}
@Override
public int GetStep()
{
if (_drawingShip != null && _drawingShip.entityShip!=null){
return (int)(_drawingShip.entityShip.GetStep());
}
return 0;
}
@Override
public boolean CheckCanMove(DirectionType direction)
{
if (_drawingShip != null){
return _drawingShip.CanMove(direction);
}
return false;
}
@Override
public void MoveObject(DirectionType direction)
{
if (_drawingShip != null) {
_drawingShip.MoveTransport(direction);
}
}
}

View File

@ -0,0 +1,42 @@
import java.awt.*;
public class DrawingRoundDecks implements IDrawingDecks{
private DeckType deckType;
private int NumberDecks;
public void SetNumberDecks(int value){
NumberDecks = value;
switch (value){
case 2:
deckType = DeckType.TwoDecks;
break;
case 3:
deckType = DeckType.ThreeDecks;
break;
default:
deckType = DeckType.OneDeck;
}
};
public void DrawDeck(int x, int y, int width, int height, Graphics g, Color MainColor){
g.setColor(MainColor);
g.fillOval(x, y, width, height);
g.setColor(Color.black);
g.drawOval(x, y, width, height);
}
public void DrawingDecks(int _startPosX, int _startPosY, Color MainColor, Graphics g){
switch (deckType){
case OneDeck:
DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor);
break;
case TwoDecks:
DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor);
DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor);
break;
case ThreeDecks:
DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor);
DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor);
DrawDeck(_startPosX + 50, _startPosY + 14, 40, 8, g, MainColor);
break;
}
}
}

131
scr/DrawingShip.java Normal file
View File

@ -0,0 +1,131 @@
import java.awt.*;
import java.util.Random;
public class DrawingShip {
public EntityShip entityShip;
public EntityShip GetEntityWarmlyShip(){
return entityShip;
}
private void SetEntityWarmlyShip(EntityShip entityShip){
this.entityShip = entityShip;
}
private IDrawingDecks drawingDecks;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private int _shipWidth = 100;
private int _shipHeight = 70;
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _shipWidth;
}
public int GetHeight() {
return _shipHeight;
}
public DrawingShip(int speed, double weight, Color mainColor, int width, int height, int numberDecks){
if (width < _shipWidth || height <_shipHeight){
return;
}
_pictureWidth = width;
_pictureHeight = height;
entityShip = new EntityShip(speed, weight, mainColor);
Random rnd = new Random();
drawingDecks = switch (rnd.nextInt(3)){
case 0 -> new DrawingDecks();
case 1 -> new DrawingRoundDecks();
case 2 -> new DrawingHexagoneDecks();
default -> new DrawingDecks();
};
drawingDecks.SetNumberDecks(numberDecks);
}
protected DrawingShip(int speed, double weight, Color mainColor, int width, int height, int numberDecks, int shipWidth, int shipHeight){
if (width < _shipWidth || height <_shipHeight){
}
_pictureWidth = width;
_pictureHeight = height;
_shipWidth = shipWidth;
_shipHeight = shipHeight;
entityShip = new EntityShip(speed, weight, mainColor);
Random rnd = new Random();
drawingDecks = switch (rnd.nextInt(3)){
case 0 -> new DrawingDecks();
case 1 -> new DrawingRoundDecks();
case 2 -> new DrawingHexagoneDecks();
default -> new DrawingDecks();
};
drawingDecks.SetNumberDecks(numberDecks);
}
public void SetPosition(int x, int y){
if (x < 0 || x + _shipWidth > _pictureWidth){
x = 20;
}
if (y < 0 || y + _shipHeight > _pictureHeight){
y = 20;
}
_startPosX = x;
_startPosY = y;
}
public boolean CanMove(DirectionType direction)
{
if (entityShip == null){
return false;
}
return switch (direction){
case Left -> _startPosX - entityShip.GetStep() > 0;
case Up -> _startPosY - entityShip.GetStep() > 0;
case Right -> _startPosX + entityShip.GetStep() + _shipWidth <= _pictureWidth;
case Down -> _startPosY + entityShip.GetStep() + _shipHeight <= _pictureHeight;
};
}
public void MoveTransport(DirectionType direction){
if (!CanMove(direction) || entityShip == null) {
return;
}
switch (direction)
{
case Left:
_startPosX -= (int)entityShip.GetStep();
break;
case Up:
_startPosY -= (int)entityShip.GetStep();
break;
case Right:
_startPosX += (int)entityShip.GetStep();
break;
case Down:
_startPosY += (int)entityShip.GetStep();
break;
}
}
public void DrawTransport(Graphics g){
if (entityShip == null || drawingDecks == null){
System.out.println("?????????");
return;
}
//палуба
drawingDecks.DrawingDecks(_startPosX, _startPosY, entityShip.GetMainColor(), g);
//корпус
int[] xPoints = {_startPosX, _startPosX + 100, _startPosX + 90, _startPosX + 20, _startPosX};
int[] yPoints = {_startPosY + 40, _startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40};
int nPoints = 5;
g.setColor(entityShip.GetMainColor());
g.fillPolygon(xPoints, yPoints, nPoints);
g.setColor(Color.black);
g.drawPolygon(xPoints, yPoints, nPoints);
//якорь
g.drawLine(_startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55);
g.drawLine(_startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50);
g.drawLine(_startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55);
}
}

View File

@ -1,83 +1,18 @@
import java.awt.*;
public class DrawingWarmlyShip {
private EntityWarmlyShip entityWarmlyShip;
public EntityWarmlyShip GetEntityWarmlyShip(){
return entityWarmlyShip;
}
private void SetEntityWarmlyShip(EntityWarmlyShip entityWarmlyShip){
this.entityWarmlyShip = entityWarmlyShip;
}
private DrawingDecks drawingDecks;
public DrawingDecks GetDrawingDecks(){
return drawingDecks;
}
private void SetDrawingDecks( DrawingDecks drawingDecks){
this.drawingDecks = drawingDecks;
}
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _shipWidth = 100;
private final int _shipHeight = 70;
public boolean Init(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment, int width, int height, int numberDecks){
if (width < _shipWidth || height <_shipHeight){
return false;
}
_pictureWidth = width;
_pictureHeight = height;
entityWarmlyShip = new EntityWarmlyShip();
entityWarmlyShip.Init(speed, weight, mainColor, optionalColor, pipes, fuelCompartment);
drawingDecks = new DrawingDecks();
drawingDecks.SetNumberDecks(numberDecks);
return true;
}
public void SetPosition(int x, int y){
if (x < 0 || x + _shipWidth > _pictureWidth){
x = 20;
}
if (y < 0 || y + _shipHeight > _pictureHeight){
y = 20;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction){
if (entityWarmlyShip == null){
return;
}
switch (direction){
case Left:
System.out.println(_startPosX + " " + entityWarmlyShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityWarmlyShip.GetStep() + _shipWidth) + " " + _pictureWidth);
if (_startPosX - entityWarmlyShip.GetStep() > 0){
_startPosX -= (int)entityWarmlyShip.GetStep();
}
break;
case Up:
System.out.println(_startPosY + " " + entityWarmlyShip.GetStep() + " " + (_startPosY + entityWarmlyShip.GetStep()) + " " + _pictureHeight);
if (_startPosY - entityWarmlyShip.GetStep() > 0){
_startPosY -= (int)entityWarmlyShip.GetStep();
}
break;
case Right:
System.out.println(_startPosX + " " + entityWarmlyShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityWarmlyShip.GetStep() + _shipWidth) + " " + _pictureWidth);
if (_startPosX + entityWarmlyShip.GetStep() + _shipWidth < _pictureWidth){
_startPosX += (int)entityWarmlyShip.GetStep();
}
break;
case Down:
System.out.println(_startPosY + " " + entityWarmlyShip.GetStep() + " " + (_startPosY + entityWarmlyShip.GetStep()) + " " + _pictureHeight);
if (_startPosY + entityWarmlyShip.GetStep() + _shipHeight < _pictureHeight){
_startPosY += (int)entityWarmlyShip.GetStep();
}
break;
public class DrawingWarmlyShip extends DrawingShip{
public DrawingWarmlyShip(int speed, double weight, Color mainColor, Color optionalColor,
boolean pipes, boolean fuelCompartment, int width, int height, int numberDecks){
super(speed, weight, mainColor, width, height, numberDecks, 100, 60);
if (entityShip != null){
entityShip = new EntityWarmlyShip(speed, weight, mainColor, optionalColor, pipes, fuelCompartment);
}
}
@Override
public void DrawTransport(Graphics g){
if (entityWarmlyShip == null || drawingDecks == null){
System.out.println("?????????");
if (!(entityShip instanceof EntityWarmlyShip entityWarmlyShip)) {
return;
}
if (entityWarmlyShip.GetPipes()){
g.setColor(entityWarmlyShip.GetOptionalColor());
g.fillRect(_startPosX + 70, _startPosY, 10, 30);
@ -93,20 +28,7 @@ public class DrawingWarmlyShip {
g.setColor(Color.black);
g.drawRect(_startPosX + 10, _startPosY + 30, 10, 10);
}
//палуба
drawingDecks.DrawingDecks(_startPosX, _startPosY, entityWarmlyShip.GetMainColor(), g);
//корпус
int[] xPoints = {_startPosX, _startPosX + 100, _startPosX + 90, _startPosX + 20, _startPosX};
int[] yPoints = {_startPosY + 40, _startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40};
int nPoints = 5;
g.setColor(entityWarmlyShip.GetMainColor());
g.fillPolygon(xPoints, yPoints, nPoints);
g.setColor(Color.black);
g.drawPolygon(xPoints, yPoints, nPoints);
//якорь
g.drawLine(_startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55);
g.drawLine(_startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50);
g.drawLine(_startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55);
super.DrawTransport(g);
}
}

35
scr/EntityShip.java Normal file
View File

@ -0,0 +1,35 @@
import java.awt.*;
public class EntityShip {
private int Speed;
public int GetSpeed(){
return Speed;
}
private void SetSpeed(int speed){
Speed = speed;
}
private double Weight;
public double GetWeight(){
return Weight;
}
private void SetWeight(int weight){
Weight = weight;
}
private Color MainColor;
public Color GetMainColor(){
return MainColor;
}
private void SetMainColor(Color mainColor){
MainColor = mainColor;
}
private double Step;
public double GetStep() {
return (double)Speed * 100 / Weight;
}
public EntityShip(int speed, double weight, Color mainColor)
{
Speed = speed;
Weight = weight;
MainColor = mainColor;
}
}

View File

@ -1,26 +1,5 @@
import java.awt.*;
public class EntityWarmlyShip {
private int Speed;
public int GetSpeed(){
return Speed;
}
private void SetSpeed(int speed){
Speed = speed;
}
private double Weight;
public double GetWeight(){
return Weight;
}
private void SetWeight(int weight){
Weight = weight;
}
private Color MainColor;
public Color GetMainColor(){
return MainColor;
}
private void SetMainColor(Color mainColor){
MainColor = mainColor;
}
public class EntityWarmlyShip extends EntityShip{
private Color OptionalColor;
public Color GetOptionalColor(){
return OptionalColor;
@ -42,15 +21,9 @@ public class EntityWarmlyShip {
private void SetFuelCompartment(boolean fuelCompartment){
FuelCompartment = fuelCompartment;
}
private double Step;
public double GetStep() {
return (double)Speed * 100 / Weight;
}
public void Init(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment)
public EntityWarmlyShip(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment)
{
Speed = speed;
Weight = weight;
MainColor = mainColor;
super(speed, weight, mainColor);
OptionalColor = optionalColor;
Pipes = pipes;
FuelCompartment = fuelCompartment;

6
scr/IDrawingDecks.java Normal file
View File

@ -0,0 +1,6 @@
import java.awt.*;
public interface IDrawingDecks {
public void SetNumberDecks(int value);
public void DrawingDecks(int _startPosX, int _startPosY, Color MainColor, Graphics g);
}

9
scr/IMoveableObject.java Normal file
View File

@ -0,0 +1,9 @@
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
boolean CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}

31
scr/MoveToBorder.java Normal file
View File

@ -0,0 +1,31 @@
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.RightBorder() - GetFieldWidth();
if (Math.abs(diffX) > GetStep())
{
MoveRight();
}
var diffY = objParams.DownBorder() - GetFieldHeight();
if (Math.abs(diffY) > GetStep())
{
MoveDown();
}
}
}

39
scr/MoveToCenter.java Normal file
View File

@ -0,0 +1,39 @@
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean isTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null ) {
return false;
}
return
Math.abs(objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2) <= GetStep()
&& Math.abs(objParams.ObjectMiddleVertical() - GetFieldHeight() / 2) <= GetStep();
}
@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();
}
}
}
}

40
scr/ObjectParameters.java Normal file
View File

@ -0,0 +1,40 @@
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) {
_x = x;
_y = y;
_width = width;
_height = height;
}
}

5
scr/Status.java Normal file
View File

@ -0,0 +1,5 @@
public enum Status {
NotInit,
InProgress,
Finish
}

View File

@ -6,16 +6,20 @@ import java.awt.event.ActionListener;
import java.util.*;
public class WarmlyShipForm{
private DrawingWarmlyShip _drawingWarmlyShip;
private DrawingShip _drawingShip;
private AbstractStrategy _abstractStrategy;
Canvas canv;
public void Draw(){
canv.repaint();
}
public WarmlyShipForm(){
JFrame frame = new JFrame("Warmly Ship");
JButton buttonCreate = new JButton("Создать");
buttonCreate.setFocusPainted(false);
buttonCreate.setContentAreaFilled(false);
JButton buttonCreateShip = new JButton("Создать");
buttonCreateShip.setFocusPainted(false);
buttonCreateShip.setContentAreaFilled(false);
JButton buttonCreateWarmlyShip = new JButton("Создать Продвинутый");
buttonCreateWarmlyShip.setFocusPainted(false);
buttonCreateWarmlyShip.setContentAreaFilled(false);
JButton buttonUp = new JButton();
//buttonUp.setBorderPainted(false); //граница кнопки
buttonUp.setFocusPainted(false); //контур вокруг текста
@ -37,20 +41,43 @@ public class WarmlyShipForm{
buttonRight.setContentAreaFilled(false);
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon(((new ImageIcon("images/ArrowRight.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
buttonCreate.addActionListener(
String[] items = {
"Form center",
"Form border"
};
JComboBox comboBoxStrategy = new JComboBox(items);
JButton buttonStep = new JButton("Шаг");
buttonStep.setFocusPainted(false);
buttonStep.setContentAreaFilled(false);
buttonCreateShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingWarmlyShip = new DrawingWarmlyShip();
_drawingWarmlyShip.Init(
_drawingShip = new DrawingShip(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
900, 460, random.nextInt(3) + 1);
_drawingShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
canv._drawingShip = _drawingShip;
Draw();
}
}
);
buttonCreateWarmlyShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingShip = new DrawingWarmlyShip(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(), 900, 460, random.nextInt(3) + 1);
_drawingWarmlyShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
canv._drawingWarmlyShip = _drawingWarmlyShip;
_drawingShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
canv._drawingShip = _drawingShip;
Draw();
}
}
@ -58,26 +85,55 @@ public class WarmlyShipForm{
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingWarmlyShip == null){
if (_drawingShip == null){
return;
}
switch ((((JButton)(e.getSource())).getName())){
case "up":
_drawingWarmlyShip.MoveTransport(DirectionType.Up);
_drawingShip.MoveTransport(DirectionType.Up);
break;
case "down":
_drawingWarmlyShip.MoveTransport(DirectionType.Down);
_drawingShip.MoveTransport(DirectionType.Down);
break;
case "left":
_drawingWarmlyShip.MoveTransport(DirectionType.Left);
_drawingShip.MoveTransport(DirectionType.Left);
break;
case "right":
_drawingWarmlyShip.MoveTransport(DirectionType.Right);
_drawingShip.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
};
buttonStep.addActionListener(e -> {
if (_drawingShip == 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 DrawingObjectShip(_drawingShip), this.canv.getWidth(), this.canv.getHeight()-50);
}
if (_abstractStrategy == null)
{
return;
}
comboBoxStrategy.setEnabled(false);
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
});
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
@ -87,30 +143,36 @@ public class WarmlyShipForm{
frame.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 900, 500);
buttonCreate.setBounds(2, 420, 100, 40);
buttonCreateShip.setBounds(2, 420, 100, 40);
buttonCreateWarmlyShip.setBounds(102, 420, 100, 40);
buttonUp.setBounds(800, 380, 40, 40);
buttonDown.setBounds(800, 420, 40, 40);
buttonLeft.setBounds(760, 420, 40, 40);
buttonRight.setBounds(840, 420, 40, 40);
comboBoxStrategy.setBounds(800,10,100,50);
buttonStep.setBounds(800,80,100,40);
frame.add(canv);
frame.add(buttonCreate);
frame.add(buttonCreateShip);
frame.add(buttonCreateWarmlyShip);
frame.add(buttonUp);
frame.add(buttonDown);
frame.add(buttonLeft);
frame.add(buttonRight);
frame.add(comboBoxStrategy);
frame.add(buttonStep);
frame.setVisible(true);
}
class Canvas extends JComponent{
public DrawingWarmlyShip _drawingWarmlyShip;
public DrawingShip _drawingShip;
public Canvas(){}
public void paintComponent(Graphics g){
if (_drawingWarmlyShip == null){
if (_drawingShip == null){
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
_drawingWarmlyShip.DrawTransport(g2d);
_drawingShip.DrawTransport(g2d);
super.repaint();
}
}