ПИбд-23 Салин Олег Алексеевич Лабораторная работа №2 (усложненная) #3
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" default="true">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
62
src/MonorailHard/DrawningObjects/DrawningLocomotive.java
Normal file
62
src/MonorailHard/DrawningObjects/DrawningLocomotive.java
Normal file
@ -0,0 +1,62 @@
|
||||
package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.Entities.EntityLocomotive;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningLocomotive extends DrawningMonorail{
|
||||
public DrawningLocomotive(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
|
||||
int width, int height, boolean secondCabine, boolean magniteRail, Color additionalColor){
|
||||
super(speed, weight, bodyColor, wheelColor, tireColor, width, height);
|
||||
if(EntityMonorail() != null){
|
||||
EntityMonorail = new EntityLocomotive(speed, weight, bodyColor, wheelColor, tireColor, wheelNumb, secondCabine,
|
||||
magniteRail, additionalColor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawMonorail(Graphics2D g2d){
|
||||
if (!(EntityMonorail instanceof EntityLocomotive))
|
||||
{
|
||||
return;
|
||||
}
|
||||
super.DrawMonorail(g2d);
|
||||
int dif = _monorailWidth / 10;
|
||||
_monorailWidth -= dif;
|
||||
EntityLocomotive _locomotive = (EntityLocomotive) EntityMonorail;
|
||||
//вторая кабина
|
||||
if (_locomotive.SecondCabine()) {
|
||||
int[] pointsSecondCabineX = {_startPosX + _monorailWidth / 20 * 19,
|
||||
_startPosX + _monorailWidth + dif,
|
||||
_startPosX + _monorailWidth + dif,
|
||||
_startPosX + _monorailWidth / 20 * 19};
|
||||
int[] pointsSecondCabineY = {_startPosY + _monorailHeight / 10,
|
||||
_startPosY + _monorailHeight / 5 * 2,
|
||||
_startPosY + _monorailHeight / 10 * 7,
|
||||
_startPosY + _monorailHeight / 10 * 7};
|
||||
g2d.setColor(((EntityLocomotive) EntityMonorail).AdditionalColor());
|
||||
g2d.fillPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length);
|
||||
Rectangle Rect = new Rectangle();
|
||||
Rect.x = _startPosX + _monorailWidth / 20 * 19;
|
||||
Rect.y = _startPosY + _monorailHeight / 25 * 4 + _monorailHeight / 50 * 3;
|
||||
Rect.width = _monorailWidth / 120 * 6;
|
||||
Rect.height = _monorailHeight / 50 * 7;
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(Rect.x, Rect.y, Rect.width, Rect.height);
|
||||
g2d.setColor(Color.BLUE);
|
||||
g2d.drawRect(Rect.x, Rect.y, Rect.width, Rect.height);
|
||||
}
|
||||
|
||||
_monorailWidth+=dif;
|
||||
|
||||
//магнитная линия
|
||||
if (_locomotive.MagniteRail())
|
||||
{
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawLine(_startPosX, _startPosY + _monorailHeight, _startPosX + _monorailWidth, _startPosY + _monorailHeight);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,48 +1,76 @@
|
||||
package MonorailHard;
|
||||
package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.DirectionType;
|
||||
import MonorailHard.Entities.EntityMonorail;
|
||||
import MonorailHard.MovementStrategy.IMoveableObject;
|
||||
import com.sun.source.tree.ImportTree;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.html.parser.Entity;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningMonorail {
|
||||
|
||||
JPanel MonorailPanel;
|
||||
private EntityMonorail EntityMonorail;
|
||||
protected EntityMonorail EntityMonorail;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX = 0;
|
||||
private int _startPosY = 0;
|
||||
private int _monorailWidth = 133;
|
||||
private int _monorailHeight = 50;
|
||||
private DrawningWheels DrawningWheels;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected int _monorailWidth = 133;
|
||||
protected int _monorailHeight = 50;
|
||||
protected IDraw DrawningWheels;
|
||||
|
||||
protected int wheelSz;
|
||||
|
||||
|
||||
public EntityMonorail EntityMonorail(){
|
||||
return EntityMonorail;
|
||||
}
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
|
||||
int width, int height, boolean secondCabine, boolean magniteRail, JPanel monorailPanel){
|
||||
public DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor,
|
||||
int width, int height){
|
||||
if(width <= _monorailWidth || height <= _monorailHeight)
|
||||
return false;
|
||||
return;
|
||||
_startPosY=0;
|
||||
_startPosX = 0;
|
||||
monorailPanel.setSize(width, height);
|
||||
MonorailPanel = monorailPanel;
|
||||
monorailPanel.paint(MonorailPanel.getGraphics());
|
||||
wheelSz = _monorailHeight - _monorailHeight * 7 / 10;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityMonorail = new EntityMonorail();
|
||||
EntityMonorail.Init(speed, weight, bodyColor, wheelColor, tireColor, wheelNumb, secondCabine, magniteRail);
|
||||
EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor);
|
||||
int dif = _monorailWidth / 10;
|
||||
DrawningWheels = new DrawningWheels();
|
||||
DrawningWheels.Init(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel);
|
||||
DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
Random rand = new Random();
|
||||
int variant = rand.nextInt(0, 3);
|
||||
if(variant == 0)
|
||||
DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
else if(variant == 1)
|
||||
DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
else
|
||||
DrawningWheels = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6));
|
||||
return true;
|
||||
}
|
||||
|
||||
protected DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor,
|
||||
int width, int height, int monorailWidth, int monorailHeight){
|
||||
if(width <= _monorailWidth || height <= _monorailHeight)
|
||||
return;
|
||||
_startPosY=0;
|
||||
_startPosX = 0;
|
||||
wheelSz = _monorailHeight - _monorailHeight * 7 / 10;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_monorailWidth = monorailWidth;
|
||||
_monorailHeight = monorailHeight;
|
||||
EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor);
|
||||
int dif = _monorailWidth / 10;
|
||||
Random rand = new Random();
|
||||
int variant = rand.nextInt(0, 3);
|
||||
if(variant == 0)
|
||||
DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
else if(variant == 1)
|
||||
DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
else
|
||||
DrawningWheels = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
|
||||
DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6));
|
||||
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y){
|
||||
@ -50,49 +78,68 @@ public class DrawningMonorail {
|
||||
return;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
if(x + _monorailWidth <= _pictureWidth|| y + _monorailHeight <= _pictureHeight){
|
||||
if(x + _monorailWidth >= _pictureWidth|| y + _monorailHeight >= _pictureHeight || x < 0 || y < 0){
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction){
|
||||
public int GetPosX(){return _startPosX;}
|
||||
public int GetPosY(){return _startPosY;}
|
||||
public int GetWidth(){return _monorailWidth;}
|
||||
public int GetHeight(){return _monorailHeight;}
|
||||
public boolean CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityMonorail == null)
|
||||
return false;
|
||||
boolean can = false;
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
can = _startPosX - EntityMonorail.Step() >= 0;
|
||||
break;
|
||||
case Right:
|
||||
can = _startPosX + EntityMonorail.Step() + _monorailWidth< _pictureWidth;
|
||||
break;
|
||||
case Down:
|
||||
can = _startPosY + EntityMonorail.Step() + _monorailHeight < _pictureHeight;
|
||||
break;
|
||||
case Up:
|
||||
can = _startPosY - EntityMonorail.Step() >= 0;
|
||||
break;
|
||||
};
|
||||
return can;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void MoveTransport(DirectionType direction){
|
||||
if (!CanMove(direction) || EntityMonorail == null)
|
||||
return;
|
||||
MonorailPanel.paint(MonorailPanel.getGraphics());
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
if (_startPosX - EntityMonorail.Step() >= 0)
|
||||
_startPosX -= (int)EntityMonorail.Step();
|
||||
else
|
||||
_startPosX = 0;
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityMonorail.Step() >= 0)
|
||||
_startPosY -= (int)EntityMonorail.Step();
|
||||
else
|
||||
_startPosY = 0;
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + EntityMonorail.Step() + _monorailWidth < _pictureWidth)
|
||||
if (_startPosX + EntityMonorail.Step() + _monorailWidth <= _pictureWidth)
|
||||
_startPosX += (int)EntityMonorail.Step();
|
||||
else
|
||||
_startPosX = _pictureWidth - _monorailWidth;
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + EntityMonorail.Step() + _monorailHeight < _pictureHeight)
|
||||
if (_startPosY + EntityMonorail.Step() + _monorailHeight <= _pictureHeight)
|
||||
_startPosY += (int)EntityMonorail.Step();
|
||||
else
|
||||
_startPosY = _pictureHeight - _monorailHeight;
|
||||
break;
|
||||
}
|
||||
DrawningWheels.CurX = _startPosX;
|
||||
DrawningWheels.CurY = _startPosY;
|
||||
DrawningWheels.ChangeX(_startPosX);
|
||||
DrawningWheels.ChangeY(_startPosY);
|
||||
}
|
||||
|
||||
public void DrawMonorail(){
|
||||
Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics();
|
||||
public void DrawMonorail(Graphics2D g2d){
|
||||
if (EntityMonorail == null)
|
||||
return;
|
||||
int dif = _monorailWidth / 10;
|
||||
@ -141,25 +188,6 @@ public class DrawningMonorail {
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(xPointsArrDoor, yPointsArrDoor, xPointsArrDoor.length);
|
||||
|
||||
|
||||
//передняя часть тележки
|
||||
int[] xPointsArrFrontCart = { _startPosX + _monorailWidth / 10 * 4, _startPosX + _monorailWidth / 10 * 2,
|
||||
_startPosX, _startPosX + _monorailWidth / 10 * 4,
|
||||
_startPosX + _monorailWidth / 10 * 4};
|
||||
int[] yPointsArrFrontCart = { _startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7,
|
||||
_startPosY + _monorailHeight / 10 * 9, _startPosY + _monorailHeight / 10 * 9,
|
||||
_startPosY + _monorailHeight / 10 * 7};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length);
|
||||
|
||||
//задняя часть тележки
|
||||
int[] xPointsArrBackCart = {_startPosX + _monorailWidth / 10 * 6, _startPosX + _monorailWidth / 10 * 9,
|
||||
_startPosX + _monorailWidth, _startPosX + _monorailWidth / 10 * 6};
|
||||
int[] yPointsArrBackCart = { _startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7,
|
||||
_startPosY + _monorailHeight / 10 * 9, _startPosY + _monorailHeight / 10 * 9};
|
||||
|
||||
g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length);
|
||||
|
||||
//левое окно
|
||||
Rectangle leftRect = new Rectangle();
|
||||
leftRect.x = _startPosX + _monorailWidth / 10 * 2;
|
||||
@ -193,43 +221,8 @@ public class DrawningMonorail {
|
||||
g2d.fillRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height);
|
||||
g2d.setColor(Color.BLUE);
|
||||
g2d.drawRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height);
|
||||
|
||||
//вторая кабина
|
||||
if (EntityMonorail.SecondCabine())
|
||||
{
|
||||
int[] pointsSecondCabineX = { _startPosX + _monorailWidth / 20 * 19,
|
||||
_startPosX + _monorailWidth + dif,
|
||||
_startPosX + _monorailWidth + dif,
|
||||
_startPosX + _monorailWidth / 20 * 19};
|
||||
int[] pointsSecondCabineY = { _startPosY + _monorailHeight / 10,
|
||||
_startPosY + _monorailHeight / 5 * 2,
|
||||
_startPosY + _monorailHeight / 10 * 7,
|
||||
_startPosY + _monorailHeight / 10 * 7};
|
||||
g2d.setColor(EntityMonorail.BodyColor());
|
||||
g2d.fillPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length);
|
||||
Rectangle Rect = new Rectangle();
|
||||
Rect.x = _startPosX + _monorailWidth / 20 * 19;
|
||||
Rect.y = _startPosY + _monorailHeight / 25 * 4 + _monorailHeight / 50 * 3;
|
||||
Rect.width = _monorailWidth / 120 * 6;
|
||||
Rect.height = _monorailHeight / 50 * 7;
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(Rect.x, Rect.y, Rect.width, Rect.height);
|
||||
g2d.setColor(Color.BLUE);
|
||||
g2d.drawRect(Rect.x, Rect.y, Rect.width, Rect.height);
|
||||
}
|
||||
DrawningWheels.DrawWheels();
|
||||
|
||||
//колеса
|
||||
DrawningWheels.DrawWheels(g2d);
|
||||
_monorailWidth += dif;
|
||||
|
||||
//магнитная линия
|
||||
if (EntityMonorail.MagniteRail())
|
||||
{
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawLine(_startPosX, _startPosY + _monorailHeight, _startPosX + _monorailWidth, _startPosY + _monorailHeight);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
84
src/MonorailHard/DrawningObjects/DrawningWheels.java
Normal file
84
src/MonorailHard/DrawningObjects/DrawningWheels.java
Normal file
@ -0,0 +1,84 @@
|
||||
package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.NumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheels implements IDraw{
|
||||
private int WheelSz;
|
||||
private NumberType WheelsNumb;
|
||||
private Color WheelColor, TireColor;
|
||||
private int Width, Height;
|
||||
protected int CurX, CurY;
|
||||
public int WheelSz(){
|
||||
return WheelSz;
|
||||
}
|
||||
|
||||
public DrawningWheels(int width, int height, int curX, int curY, Color wheelColor, Color tireColor){
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
WheelColor = wheelColor;
|
||||
TireColor = tireColor;
|
||||
WheelSz = Height - Height * 7 / 10;
|
||||
}
|
||||
public void ChangeX(int x){
|
||||
CurX = x;
|
||||
}
|
||||
public void ChangeY(int y){
|
||||
CurY = y;
|
||||
}
|
||||
public void ChangeWheelsNumb(int x){
|
||||
if(x <= 2)
|
||||
WheelsNumb = NumberType.Two;
|
||||
if(x == 3)
|
||||
WheelsNumb = NumberType.Three;
|
||||
if(x >= 4)
|
||||
WheelsNumb = NumberType.Four;
|
||||
}
|
||||
|
||||
public NumberType WheelsNumb(){
|
||||
return WheelsNumb;
|
||||
}
|
||||
|
||||
public void DrawWheel(Graphics2D g2d, int x, int y){
|
||||
g2d.setColor(WheelColor);
|
||||
g2d.fillOval( x, y , WheelSz, WheelSz);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawOval(x, y, WheelSz, WheelSz);
|
||||
}
|
||||
|
||||
public void DrawWheels(Graphics2D g2d){
|
||||
//передняя часть тележки
|
||||
int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2,
|
||||
CurX, CurX + Width / 10 * 4,
|
||||
CurX + Width / 10 * 4};
|
||||
int[] yPointsArrFrontCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7,
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9,
|
||||
CurY + Height / 10 * 7};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length);
|
||||
|
||||
//задняя часть тележки
|
||||
int[] xPointsArrBackCart = {CurX + Width / 10 * 6, CurX + Width / 10 * 9,
|
||||
CurX + Width, CurX + Width / 10 * 6};
|
||||
int[] yPointsArrBackCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7,
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9};
|
||||
|
||||
g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length);
|
||||
|
||||
DrawWheel(g2d, CurX + Width / 10, CurY + Height / 10 * 7);
|
||||
DrawWheel(g2d, CurX + Width / 10 * 8, CurY + Height / 10 * 7);
|
||||
//3 колеса
|
||||
if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four)
|
||||
DrawWheel(g2d,CurX + Width / 10 * 6, CurY + Height / 10 * 7);
|
||||
|
||||
//4 колеса
|
||||
if (WheelsNumb == NumberType.Four)
|
||||
DrawWheel(g2d, CurX + Width / 10 * 3, CurY + Height / 10 * 7);
|
||||
}
|
||||
|
||||
|
||||
}
|
89
src/MonorailHard/DrawningObjects/DrawningWheelsCart.java
Normal file
89
src/MonorailHard/DrawningObjects/DrawningWheelsCart.java
Normal file
@ -0,0 +1,89 @@
|
||||
package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.NumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheelsCart implements IDraw{
|
||||
private int WheelSz;
|
||||
private NumberType WheelsNumb;
|
||||
private Color WheelColor, TireColor;
|
||||
private int Width, Height;
|
||||
public int CurX, CurY;
|
||||
public int WheelSz(){
|
||||
return WheelSz;
|
||||
}
|
||||
|
||||
public DrawningWheelsCart(int width, int height, int curX, int curY, Color wheelColor, Color tireColor){
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
WheelColor = wheelColor;
|
||||
TireColor = tireColor;
|
||||
WheelSz = Height - Height * 7 / 10;
|
||||
}
|
||||
|
||||
public void ChangeWheelsNumb(int x){
|
||||
if(x <= 2)
|
||||
WheelsNumb = NumberType.Two;
|
||||
if(x == 3)
|
||||
WheelsNumb = NumberType.Three;
|
||||
if(x >= 4)
|
||||
WheelsNumb = NumberType.Four;
|
||||
}
|
||||
|
||||
public NumberType WheelsNumb(){
|
||||
return WheelsNumb;
|
||||
}
|
||||
|
||||
public void ChangeX(int x){
|
||||
CurX = x;
|
||||
}
|
||||
public void ChangeY(int y){
|
||||
CurY = y;
|
||||
}
|
||||
public void DrawWheel(Graphics2D g2d, int x, int y){
|
||||
g2d.setColor(WheelColor);
|
||||
g2d.fillOval( x, y , WheelSz, WheelSz);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawOval(x, y, WheelSz, WheelSz);
|
||||
}
|
||||
|
||||
public void DrawWheels(Graphics2D g2d){
|
||||
//передняя часть тележки
|
||||
int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2,
|
||||
CurX, CurX + Width / 10 * 4,
|
||||
CurX + Width / 10 * 4};
|
||||
int[] yPointsArrFrontCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7,
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9,
|
||||
CurY + Height / 10 * 7};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawLine(CurX + Width / 10 * 2, CurY + Height / 10 * 7,CurX + Width / 10 * 4, CurY + Height / 10 * 9);
|
||||
g2d.drawLine(CurX + Width / 10 * 4, CurY + Height / 10 * 7,CurX + Width / 10 * 2, CurY + Height / 10 * 9);
|
||||
|
||||
//задняя часть тележки
|
||||
int[] xPointsArrBackCart = {CurX + Width / 10 * 6, CurX + Width / 10 * 9,
|
||||
CurX + Width, CurX + Width / 10 * 6};
|
||||
int[] yPointsArrBackCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7,
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawLine(CurX + Width / 10 * 6, CurY + Height / 10 * 7,CurX + Width / 10 * 9, CurY + Height / 10 * 9);
|
||||
g2d.drawLine(CurX + Width / 10 * 9, CurY + Height / 10 * 7,CurX + Width / 10 * 6, CurY + Height / 10 * 9);
|
||||
|
||||
DrawWheel(g2d, CurX + Width / 10, CurY + Height / 10 * 7);
|
||||
DrawWheel(g2d, CurX + Width / 10 * 8, CurY + Height / 10 * 7);
|
||||
//3 колеса
|
||||
if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four)
|
||||
DrawWheel(g2d, CurX + Width / 10 * 6, CurY + Height / 10 * 7);
|
||||
|
||||
//4 колеса
|
||||
if (WheelsNumb == NumberType.Four)
|
||||
DrawWheel(g2d, CurX + Width / 10 * 3, CurY + Height / 10 * 7);
|
||||
}
|
||||
}
|
85
src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java
Normal file
85
src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java
Normal file
@ -0,0 +1,85 @@
|
||||
package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.NumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheelsOrn implements IDraw{
|
||||
private int WheelSz;
|
||||
private NumberType WheelsNumb;
|
||||
private Color WheelColor, TireColor;
|
||||
private int Width, Height;
|
||||
public int CurX, CurY;
|
||||
public int WheelSz(){
|
||||
return WheelSz;
|
||||
}
|
||||
|
||||
public DrawningWheelsOrn(int width, int height, int curX, int curY, Color wheelColor, Color tireColor){
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
WheelColor = wheelColor;
|
||||
TireColor = tireColor;
|
||||
WheelSz = Height - Height * 7 / 10;
|
||||
}
|
||||
|
||||
public void ChangeWheelsNumb(int x){
|
||||
if(x <= 2)
|
||||
WheelsNumb = NumberType.Two;
|
||||
if(x == 3)
|
||||
WheelsNumb = NumberType.Three;
|
||||
if(x >= 4)
|
||||
WheelsNumb = NumberType.Four;
|
||||
}
|
||||
|
||||
public NumberType WheelsNumb(){
|
||||
return WheelsNumb;
|
||||
}
|
||||
|
||||
public void ChangeX(int x){
|
||||
CurX = x;
|
||||
}
|
||||
public void ChangeY(int y){
|
||||
CurY = y;
|
||||
}
|
||||
public void DrawWheel(Graphics2D g2d, int x, int y){
|
||||
g2d.setColor(WheelColor);
|
||||
g2d.fillOval( x, y , WheelSz, WheelSz);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawOval(x, y, WheelSz, WheelSz);
|
||||
g2d.drawLine(x, y + WheelSz / 2, x + WheelSz, y + WheelSz / 2);
|
||||
g2d.drawLine(x + WheelSz / 2, y, x + WheelSz / 2, y + WheelSz);
|
||||
}
|
||||
|
||||
public void DrawWheels(Graphics2D g2d){
|
||||
//передняя часть тележки
|
||||
int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2,
|
||||
CurX, CurX + Width / 10 * 4,
|
||||
CurX + Width / 10 * 4};
|
||||
int[] yPointsArrFrontCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7,
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9,
|
||||
CurY + Height / 10 * 7};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length);
|
||||
|
||||
//задняя часть тележки
|
||||
int[] xPointsArrBackCart = {CurX + Width / 10 * 6, CurX + Width / 10 * 9,
|
||||
CurX + Width, CurX + Width / 10 * 6};
|
||||
int[] yPointsArrBackCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7,
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9};
|
||||
|
||||
g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length);
|
||||
|
||||
DrawWheel(g2d, CurX + Width / 10, CurY + Height / 10 * 7);
|
||||
DrawWheel(g2d,CurX + Width / 10 * 8, CurY + Height / 10 * 7);
|
||||
//3 колеса
|
||||
if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four)
|
||||
DrawWheel(g2d,CurX + Width / 10 * 6, CurY + Height / 10 * 7);
|
||||
|
||||
//4 колеса
|
||||
if (WheelsNumb == NumberType.Four)
|
||||
DrawWheel(g2d,CurX + Width / 10 * 3, CurY + Height / 10 * 7);
|
||||
}
|
||||
}
|
14
src/MonorailHard/DrawningObjects/IDraw.java
Normal file
14
src/MonorailHard/DrawningObjects/IDraw.java
Normal file
@ -0,0 +1,14 @@
|
||||
package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.NumberType;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDraw {
|
||||
public void ChangeWheelsNumb(int x);
|
||||
public NumberType WheelsNumb();
|
||||
public void DrawWheels(Graphics2D g2d);
|
||||
public void DrawWheel(Graphics2D g2d, int x, int y);
|
||||
public void ChangeX(int x);
|
||||
public void ChangeY(int y);
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
package MonorailHard;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheels {
|
||||
private int WheelSz;
|
||||
JPanel MonorailPanel;
|
||||
private NumberType WheelsNumb;
|
||||
private Color WheelColor, TireColor;
|
||||
private int Width, Height;
|
||||
public int CurX, CurY;
|
||||
public int WheelSz(){
|
||||
return WheelSz;
|
||||
}
|
||||
|
||||
boolean Init(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
WheelColor = wheelColor;
|
||||
TireColor = tireColor;
|
||||
WheelSz = Height - Height * 7 / 10;
|
||||
MonorailPanel = monorailPanel;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ChangeWheelsNumb(int x){
|
||||
if(x <= 2)
|
||||
WheelsNumb = NumberType.Two;
|
||||
if(x == 3)
|
||||
WheelsNumb = NumberType.Three;
|
||||
if(x >= 4)
|
||||
WheelsNumb = NumberType.Four;
|
||||
}
|
||||
|
||||
public NumberType WheelsNumb(){
|
||||
return WheelsNumb;
|
||||
}
|
||||
|
||||
public void DrawWheels(){
|
||||
Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics();
|
||||
g2d.setColor(WheelColor);
|
||||
g2d.fillOval( CurX + Width / 10, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawOval(CurX + Width / 10, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
g2d.setColor(WheelColor);
|
||||
g2d.fillOval(CurX + Width / 10 * 8, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawOval(CurX + Width / 10 * 8, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
|
||||
//3 колеса
|
||||
if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four)
|
||||
{
|
||||
g2d.setColor(WheelColor);
|
||||
g2d.fillOval(CurX + Width / 10 * 6, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawOval(CurX + Width / 10 * 6, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
}
|
||||
|
||||
//4 колеса
|
||||
if (WheelsNumb == NumberType.Four)
|
||||
{
|
||||
g2d.setColor(WheelColor);
|
||||
g2d.fillOval(CurX + Width / 10 * 3, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.drawOval(CurX + Width / 10 * 3, CurY + Height / 10 * 7, WheelSz, WheelSz);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
20
src/MonorailHard/Entities/EntityLocomotive.java
Normal file
20
src/MonorailHard/Entities/EntityLocomotive.java
Normal file
@ -0,0 +1,20 @@
|
||||
package MonorailHard.Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLocomotive extends EntityMonorail{
|
||||
private Color AdditionalColor;
|
||||
private boolean SecondCabine;
|
||||
private boolean MagniteRail;
|
||||
public Color AdditionalColor(){return AdditionalColor;};
|
||||
public boolean SecondCabine(){return SecondCabine;};
|
||||
public boolean MagniteRail(){return MagniteRail;};
|
||||
public EntityLocomotive(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
|
||||
boolean secondCabine, boolean magniteRail, Color additionalColor) {
|
||||
super(speed, weight, bodyColor, wheelColor, tireColor);
|
||||
AdditionalColor = additionalColor;
|
||||
SecondCabine = secondCabine;
|
||||
MagniteRail = magniteRail;
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +1,15 @@
|
||||
package MonorailHard;
|
||||
package MonorailHard.Entities;
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityMonorail {
|
||||
private int Speed, WheelNumb;
|
||||
private int Speed;
|
||||
private double Weight, Step;
|
||||
private Color BodyColor, WheelColor, TireColor;
|
||||
private boolean MagniteRail;
|
||||
private boolean SecondCabine;
|
||||
|
||||
public int Speed(){
|
||||
return Speed;
|
||||
}
|
||||
|
||||
public int WheelNumb(){
|
||||
return WheelNumb;
|
||||
}
|
||||
public double Weight(){
|
||||
return Weight;
|
||||
}
|
||||
@ -30,18 +25,12 @@ public class EntityMonorail {
|
||||
public Color TireColor(){
|
||||
return TireColor;
|
||||
}
|
||||
public boolean SecondCabine(){return SecondCabine;}
|
||||
public boolean MagniteRail(){return MagniteRail;}
|
||||
public void Init(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
|
||||
boolean secondCabine, boolean magniteRail){
|
||||
public EntityMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor){
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
Step = (double)Speed * 100 / Weight;
|
||||
BodyColor = bodyColor;
|
||||
WheelColor = wheelColor;
|
||||
TireColor = tireColor;
|
||||
WheelNumb = wheelNumb;
|
||||
SecondCabine = secondCabine;
|
||||
MagniteRail = magniteRail;
|
||||
}
|
||||
}
|
196
src/MonorailHard/FormMonorail.java
Normal file
196
src/MonorailHard/FormMonorail.java
Normal file
@ -0,0 +1,196 @@
|
||||
package MonorailHard;
|
||||
|
||||
import MonorailHard.DrawningObjects.DrawningLocomotive;
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.AbstractDocument;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormMonorail{
|
||||
private DrawningMonorail DrawningMonorail;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
Canvas canv;
|
||||
static int pictureBoxWidth = 882;
|
||||
static int pictureBoxHeight = 453;
|
||||
|
||||
public void Draw(){
|
||||
canv.repaint();
|
||||
}
|
||||
|
||||
public FormMonorail(){
|
||||
JFrame MonorailFrame =new JFrame ();
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
JButton buttonCreateLocomotive = new JButton("Создать локомотив");
|
||||
JButton buttonStep = new JButton("Шаг");
|
||||
JComboBox comboBoxStrategy = new JComboBox(
|
||||
new String[]{
|
||||
"Довести до центра",
|
||||
"Довести до края",
|
||||
});
|
||||
JButton UpButton = new JButton();
|
||||
UpButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\UpButton.png"));
|
||||
JButton DownButton = new JButton();
|
||||
DownButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\DownButton.png"));
|
||||
JButton LeftButton = new JButton();
|
||||
LeftButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\LeftButton.png"));
|
||||
JButton RightButton = new JButton();
|
||||
RightButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\RightButton.png"));
|
||||
buttonStep.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
if (DrawningMonorail == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled())
|
||||
{
|
||||
switch (comboBoxStrategy.getSelectedIndex())
|
||||
{
|
||||
case 0:
|
||||
_abstractStrategy = new MoveToCenter();
|
||||
break;
|
||||
case 1:
|
||||
_abstractStrategy = new MoveToBorder();
|
||||
break;
|
||||
default:
|
||||
_abstractStrategy = null;
|
||||
break;
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawningObjectMonorail(DrawningMonorail), pictureBoxWidth,
|
||||
pictureBoxHeight);
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreate.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
pictureBoxWidth, pictureBoxHeight);
|
||||
canv.DrawningMonorail = DrawningMonorail;
|
||||
comboBoxStrategy.enable(true);
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreateLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
Random random = new Random();
|
||||
DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
random.nextInt(1, 6),
|
||||
pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean(),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)));
|
||||
canv.DrawningMonorail = DrawningMonorail;
|
||||
comboBoxStrategy.enable(true);
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
RightButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null) {
|
||||
return;
|
||||
}
|
||||
DrawningMonorail.MoveTransport(DirectionType.Right);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
LeftButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null)
|
||||
return;
|
||||
DrawningMonorail.MoveTransport(DirectionType.Left);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
UpButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null)
|
||||
return;
|
||||
DrawningMonorail.MoveTransport(DirectionType.Up);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
DownButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null)
|
||||
return;
|
||||
DrawningMonorail.MoveTransport(DirectionType.Down);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
MonorailFrame.setSize (900, 500);
|
||||
MonorailFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
||||
MonorailFrame.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setSize(pictureBoxWidth, pictureBoxHeight);
|
||||
buttonCreate.setBounds(198, 401, 180, 40);
|
||||
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
|
||||
RightButton.setBounds(840,411,30,30);
|
||||
LeftButton.setBounds(768,411,30,30);
|
||||
UpButton.setBounds(804,375,30,30);
|
||||
DownButton.setBounds(804,411,30,30);
|
||||
comboBoxStrategy.setBounds(719,12,151,28);
|
||||
buttonStep.setBounds(768, 46, 94, 29);
|
||||
MonorailFrame.add(canv);
|
||||
MonorailFrame.add(buttonCreate);
|
||||
MonorailFrame.add(buttonCreateLocomotive);
|
||||
MonorailFrame.add(UpButton);
|
||||
MonorailFrame.add(DownButton);
|
||||
MonorailFrame.add(LeftButton);
|
||||
MonorailFrame.add(RightButton);
|
||||
MonorailFrame.add(comboBoxStrategy);
|
||||
MonorailFrame.add(buttonStep);
|
||||
MonorailFrame.setVisible(true);
|
||||
}
|
||||
}
|
||||
class Canvas extends JComponent{
|
||||
public DrawningMonorail DrawningMonorail;
|
||||
public Canvas(){
|
||||
}
|
||||
public void paintComponent (Graphics g){
|
||||
if (DrawningMonorail == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
DrawningMonorail.DrawMonorail(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
package MonorailHard;
|
||||
import MonorailHard.DrawningObjects.DrawningLocomotive;
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
@ -11,86 +15,6 @@ import javax.swing.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
JFrame MonorailFrame = new JFrame();
|
||||
JPanel MonorailPanel = new JPanel();
|
||||
MonorailFrame.setLayout(new BorderLayout());
|
||||
MonorailFrame.setSize(900, 500);
|
||||
MonorailFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
MonorailFrame.setLayout(new BorderLayout(1,1));
|
||||
DrawningMonorail DrawningMonorail = new DrawningMonorail();
|
||||
MonorailPanel.setLayout(null);
|
||||
BufferedImage RightIcon = ImageIO.read(new File("RightButton.png"));
|
||||
BufferedImage LeftIcon = ImageIO.read(new File("LeftButton.png"));
|
||||
BufferedImage UpIcon = ImageIO.read(new File("UpButton.png"));
|
||||
BufferedImage DownIcon = ImageIO.read(new File("DownButton.png"));
|
||||
|
||||
JButton RightButton = new JButton(new ImageIcon(RightIcon));
|
||||
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
|
||||
JButton UpButton = new JButton(new ImageIcon(UpIcon));
|
||||
JButton DownButton = new JButton(new ImageIcon(DownIcon));
|
||||
JButton CreateButton = new JButton();
|
||||
CreateButton.setText("Создать");
|
||||
CreateButton.setBounds(12, 401, 90, 40);
|
||||
RightButton.setBounds(840,411,30,30);
|
||||
LeftButton.setBounds(768,411,30,30);
|
||||
UpButton.setBounds(804,375,30,30);
|
||||
DownButton.setBounds(804,411,30,30);
|
||||
MonorailPanel.add(CreateButton);
|
||||
MonorailPanel.add(RightButton);
|
||||
MonorailPanel.add(LeftButton);
|
||||
MonorailPanel.add(UpButton);
|
||||
MonorailPanel.add(DownButton);
|
||||
MonorailFrame.add(MonorailPanel, BorderLayout.CENTER);
|
||||
Random random = new Random();
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
DrawningMonorail.Init(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
random.nextInt(2, 5),
|
||||
MonorailPanel.getWidth(), MonorailPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), MonorailPanel);
|
||||
DrawningMonorail.DrawMonorail();
|
||||
}
|
||||
});
|
||||
RightButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null)
|
||||
return;
|
||||
DrawningMonorail.MoveTransport(DirectionType.Right);
|
||||
DrawningMonorail.DrawMonorail();
|
||||
}
|
||||
});
|
||||
LeftButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null)
|
||||
return;
|
||||
DrawningMonorail.MoveTransport(DirectionType.Left);
|
||||
DrawningMonorail.DrawMonorail();
|
||||
}
|
||||
});
|
||||
UpButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null)
|
||||
return;
|
||||
DrawningMonorail.MoveTransport(DirectionType.Up);
|
||||
DrawningMonorail.DrawMonorail();
|
||||
}
|
||||
});
|
||||
DownButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningMonorail.EntityMonorail() == null)
|
||||
return;
|
||||
DrawningMonorail.MoveTransport(DirectionType.Down);
|
||||
DrawningMonorail.DrawMonorail();
|
||||
}
|
||||
});
|
||||
|
||||
MonorailFrame.setVisible(true);
|
||||
FormMonorail form = new FormMonorail();
|
||||
}
|
||||
}
|
||||
|
75
src/MonorailHard/MovementStrategy/AbstractStrategy.java
Normal file
75
src/MonorailHard/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,75 @@
|
||||
package MonorailHard.MovementStrategy;
|
||||
|
||||
import MonorailHard.DirectionType;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _moveableObject;
|
||||
private Status _state = Status.NotInit;
|
||||
private int FieldWidth;
|
||||
protected int FieldWidth(){return FieldWidth;}
|
||||
private int FieldHeight;
|
||||
protected int FieldHeight(){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;
|
||||
_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 _moveableObject.GetObjectParameters();
|
||||
else return null;
|
||||
}
|
||||
|
||||
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.CheckCanMove(directionType))
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package MonorailHard.MovementStrategy;
|
||||
|
||||
import MonorailHard.DirectionType;
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
|
||||
public class DrawningObjectMonorail implements IMoveableObject{
|
||||
private final DrawningMonorail _drawningMonorail;
|
||||
public DrawningObjectMonorail(DrawningMonorail drawningMonorail){
|
||||
_drawningMonorail = drawningMonorail;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectParameters(){
|
||||
if(_drawningMonorail == null || _drawningMonorail.EntityMonorail() == null)
|
||||
return null;
|
||||
return new ObjectParameters(_drawningMonorail.GetPosX(), _drawningMonorail.GetPosY(),
|
||||
_drawningMonorail.GetWidth(), _drawningMonorail.GetHeight());
|
||||
}
|
||||
|
||||
public int GetStep(){
|
||||
if(_drawningMonorail.EntityMonorail() == null)
|
||||
return 0;
|
||||
return (int)_drawningMonorail.EntityMonorail().Step();
|
||||
}
|
||||
|
||||
public boolean CheckCanMove(DirectionType direction){
|
||||
if(_drawningMonorail == null)
|
||||
return false;
|
||||
return _drawningMonorail.CanMove(direction);
|
||||
}
|
||||
|
||||
public void MoveObject(DirectionType direction){
|
||||
if(_drawningMonorail == null)
|
||||
return;
|
||||
_drawningMonorail.MoveTransport(direction);
|
||||
}
|
||||
}
|
10
src/MonorailHard/MovementStrategy/IMoveableObject.java
Normal file
10
src/MonorailHard/MovementStrategy/IMoveableObject.java
Normal file
@ -0,0 +1,10 @@
|
||||
package MonorailHard.MovementStrategy;
|
||||
|
||||
import MonorailHard.DirectionType;
|
||||
|
||||
public interface IMoveableObject {
|
||||
public ObjectParameters GetObjectParameters();
|
||||
public int GetStep();
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
36
src/MonorailHard/MovementStrategy/MoveToBorder.java
Normal file
36
src/MonorailHard/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,36 @@
|
||||
package MonorailHard.MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy {
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
int a = FieldWidth();
|
||||
int b = FieldHeight();
|
||||
int q = GetStep();
|
||||
return objParams.RightBorder <= FieldWidth() && objParams.RightBorder + GetStep() >= FieldWidth() &&
|
||||
objParams.DownBorder <= FieldHeight() && objParams.DownBorder + GetStep() >= FieldHeight();
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder - FieldWidth();
|
||||
if (Math.abs(diffX) >= GetStep()) {
|
||||
if (diffX < 0) {
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder - FieldHeight();
|
||||
if (Math.abs(diffY) >= GetStep()) {
|
||||
if (diffY < 0) {
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
53
src/MonorailHard/MovementStrategy/MoveToCenter.java
Normal file
53
src/MonorailHard/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,53 @@
|
||||
package MonorailHard.MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination(){
|
||||
var objParams = GetObjectParameters();
|
||||
if(objParams == null)
|
||||
return false;
|
||||
return ((objParams.ObjectMiddleHorizontal <= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth() / 2)
|
||||
||(objParams.ObjectMiddleHorizontal >= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth() / 2)) &&
|
||||
|
||||
((objParams.ObjectMiddleVertical <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 2) ||
|
||||
(objParams.ObjectMiddleVertical >= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight() / 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth() / 2;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight() / 2;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
src/MonorailHard/MovementStrategy/ObjectParameters.java
Normal file
29
src/MonorailHard/MovementStrategy/ObjectParameters.java
Normal file
@ -0,0 +1,29 @@
|
||||
package MonorailHard.MovementStrategy;
|
||||
|
||||
public class ObjectParameters {
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
private final int _width;
|
||||
private final int _height;
|
||||
public int LeftBorder;
|
||||
public int TopBorder;
|
||||
public int RightBorder;
|
||||
public int DownBorder;
|
||||
public int ObjectMiddleHorizontal;
|
||||
public int ObjectMiddleVertical;
|
||||
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
LeftBorder = _x;
|
||||
TopBorder = _y;
|
||||
RightBorder = _x + width;
|
||||
DownBorder = _y + height;
|
||||
ObjectMiddleHorizontal = _x + _width / 2;
|
||||
ObjectMiddleVertical = _y + _height / 2;
|
||||
}
|
||||
|
||||
}
|
7
src/MonorailHard/MovementStrategy/Status.java
Normal file
7
src/MonorailHard/MovementStrategy/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package MonorailHard.MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user