Compare commits

...

14 Commits
main ... Lab4

Author SHA1 Message Date
gg12 darfren
96937e1972 All done 2023-11-10 13:40:30 +04:00
gg12 darfren
7018134d52 All done 2.0 2023-11-10 13:29:14 +04:00
gg12 darfren
f3419a40e6 All done 2023-10-26 17:07:11 +04:00
gg12 darfren
7a9ec33ba1 Базовая готова 2023-10-26 15:07:34 +04:00
gg12 darfren
1c466317b8 Скоро будет готово 2023-10-26 14:50:40 +04:00
gg12 darfren
bc5671ff30 One More Commit 2023-10-25 23:53:45 +04:00
gg12 darfren
d8f83dcb7d Пофиксил формочку 2023-10-25 23:51:31 +04:00
gg12 darfren
321ec13a27 Точно Done 2023-10-17 12:32:59 +04:00
gg12 darfren
77f0c3372d Done 2023-10-16 20:27:30 +04:00
gg12 darfren
9109bc14a0 Мелкие правки 2023-10-16 16:45:33 +04:00
gg12 darfren
0a03f8b6d0 Переделал базовую на Java 2023-10-16 16:08:28 +04:00
gg12 darfren
c7b13068a1 Test commit 2023-10-16 09:54:49 +04:00
gg12 darfren
523fbaee82 Исправил ошибку 2023-10-12 20:51:36 +04:00
gg12 darfren
8b768049a0 Done 2023-10-04 11:37:04 +04:00
29 changed files with 1760 additions and 22 deletions

47
.gitignore vendored
View File

@ -1,26 +1,29 @@
# ---> Java
# Compiled class file
*.class
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
# Log file
*.log
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
# BlueJ files
*.ctxt
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

5
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" default="true">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,9 @@
package MonorailHard;
public enum DirectionType {
Up,
Down,
Left,
Right
}

View 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);
}
}
}

View File

@ -0,0 +1,233 @@
package MonorailHard.DrawningObjects;
import MonorailHard.DirectionType;
import MonorailHard.Entities.EntityMonorail;
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
import MonorailHard.MovementStrategy.IMoveableObject;
import com.sun.source.tree.ImportTree;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningMonorail {
protected EntityMonorail EntityMonorail;
public int _pictureWidth;
public int _pictureHeight;
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 DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor,
int width, int height){
if(width <= _monorailWidth || height <= _monorailHeight)
return;
_startPosY=0;
_startPosX = 0;
wheelSz = _monorailHeight - _monorailHeight * 7 / 10;
_pictureWidth = width;
_pictureHeight = height;
EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor);
int dif = _monorailWidth / 10;
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));
}
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){
if(EntityMonorail == null)
return;
_startPosX = x;
_startPosY = y;
DrawningWheels.ChangeX(_startPosX);
DrawningWheels.ChangeY(_startPosY);
if(x + _monorailWidth >= _pictureWidth|| y + _monorailHeight >= _pictureHeight || x < 0 || y < 0){
_startPosX = 0;
_startPosY = 0;
}
}
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 IMoveableObject GetMoveableObject(){
return new DrawningObjectMonorail(this);
}
public void MoveTransport(DirectionType direction){
if (!CanMove(direction) || EntityMonorail == null)
return;
switch (direction)
{
case Left:
if (_startPosX - EntityMonorail.Step() >= 0)
_startPosX -= (int)EntityMonorail.Step();
break;
case Up:
if (_startPosY - EntityMonorail.Step() >= 0)
_startPosY -= (int)EntityMonorail.Step();
break;
case Right:
if (_startPosX + EntityMonorail.Step() + _monorailWidth <= _pictureWidth)
_startPosX += (int)EntityMonorail.Step();
break;
case Down:
if (_startPosY + EntityMonorail.Step() + _monorailHeight <= _pictureHeight)
_startPosY += (int)EntityMonorail.Step();
break;
}
DrawningWheels.ChangeX(_startPosX);
DrawningWheels.ChangeY(_startPosY);
}
public void DrawMonorail(Graphics2D g2d){
if (EntityMonorail == null)
return;
int dif = _monorailWidth / 10;
_monorailWidth -= dif;
if (_monorailWidth - _monorailWidth / 20 * 17 < wheelSz)
wheelSz = _monorailWidth - _monorailWidth / 20 * 17;
g2d.setColor(Color.BLACK);
//нижняя часть локомотива
int[] xPointsArrLow = { _startPosX + _monorailWidth / 10 * 4, _startPosX + _monorailWidth / 10,
_startPosX + _monorailWidth / 10, _startPosX + _monorailWidth / 20 * 19,
_startPosX + _monorailWidth / 20 * 19, _startPosX + _monorailWidth / 10 * 5,
_startPosX + _monorailWidth / 10 * 4 };
int[] yPointsArrLow = {_startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5 * 2,
_startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7,
_startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5 * 2,
_startPosY + _monorailHeight / 5 * 2};
g2d.setColor(EntityMonorail.BodyColor());
g2d.fillPolygon(xPointsArrLow, yPointsArrLow, xPointsArrLow.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrLow, yPointsArrLow, xPointsArrLow.length);
//крыша локомотива
int[] xPointsArrRoof = {_startPosX + _monorailWidth / 10, _startPosX + _monorailWidth / 10 * 2,
_startPosX + _monorailWidth /20 * 19, _startPosX + _monorailWidth /20 * 19,
_startPosX + _monorailWidth / 10};
int[] yPointsArrRoof = { _startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 10,
_startPosY + _monorailHeight / 10, _startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5 * 2};
g2d.setColor(EntityMonorail.BodyColor());
g2d.fillPolygon(xPointsArrRoof, yPointsArrRoof, xPointsArrRoof.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrRoof, yPointsArrRoof, xPointsArrRoof.length);
//дверь локомотива
int[] xPointsArrDoor = { _startPosX + _monorailWidth / 10 * 4, _startPosX + _monorailWidth / 10 * 4,
_startPosX + _monorailWidth / 10 * 5, _startPosX + _monorailWidth / 10 * 5,
_startPosX + _monorailWidth / 10 * 4 };
int[] yPointsArrDoor = { _startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5,
_startPosY + _monorailHeight / 5, _startPosY + _monorailHeight / 5 * 3,
_startPosY + _monorailHeight / 5 * 3 };
g2d.setColor(EntityMonorail.TireColor());
g2d.fillPolygon(xPointsArrDoor, yPointsArrDoor, xPointsArrDoor.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrDoor, yPointsArrDoor, xPointsArrDoor.length);
//левое окно
Rectangle leftRect = new Rectangle();
leftRect.x = _startPosX + _monorailWidth / 10 * 2;
leftRect.y = _startPosY + _monorailHeight / 25 * 4;
leftRect.width = _monorailWidth / 120 * 8;
leftRect.height = _monorailHeight/ 50 * 10;
g2d.setColor(Color.WHITE);
g2d.fillRect(leftRect.x, leftRect.y, leftRect.width, leftRect.height);
g2d.setColor(Color.BLUE);
g2d.drawRect(leftRect.x, leftRect.y, leftRect.width, leftRect.height);
//среднее окно
Rectangle midRect = new Rectangle();
midRect.x= _startPosX + _monorailWidth / 10 * 3;
midRect.y = _startPosY + _monorailHeight / 25 * 4;
midRect.width = _monorailWidth / 120 * 8;
midRect.height = _monorailHeight / 50 * 10;
g2d.setColor(Color.WHITE);
g2d.fillRect(midRect.x, midRect.y, midRect.width, midRect.height);
g2d.setColor(Color.BLUE);
g2d.drawRect(midRect.x, midRect.y, midRect.width, midRect.height);
//правое окно
Rectangle rightRect = new Rectangle();
rightRect.x = _startPosX + _monorailWidth / 20 * 17;
rightRect.y= _startPosY + _monorailHeight / 25 * 4;
rightRect.width = _monorailWidth / 120 * 8;
rightRect.height = _monorailHeight / 50 * 10;
g2d.setColor(Color.WHITE);
g2d.fillRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height);
g2d.setColor(Color.BLUE);
g2d.drawRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height);
//колеса
DrawningWheels.DrawWheels(g2d);
_monorailWidth += dif;
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}

View 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;
}
}

View File

@ -0,0 +1,36 @@
package MonorailHard.Entities;
import java.awt.*;
public class EntityMonorail {
private int Speed;
private double Weight, Step;
private Color BodyColor, WheelColor, TireColor;
public int Speed(){
return Speed;
}
public double Weight(){
return Weight;
}
public double Step(){
return Step;
}
public Color BodyColor(){
return BodyColor;
}
public Color WheelColor(){
return WheelColor;
}
public Color TireColor(){
return TireColor;
}
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;
}
}

View File

@ -0,0 +1,241 @@
package MonorailHard;
import MonorailHard.DrawningObjects.DrawningLocomotive;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.MovementStrategy.*;
import org.w3c.dom.ranges.DocumentRange;
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;
public JButton buttonSelect;
public JFrame MonorailFrame;
Canvas canv;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public Color ChooseColor(JFrame MonorailFrame){
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
return res;
}
public DrawningMonorail SelectedMonorail(){
return DrawningMonorail;
}
public void Draw(){
if(DrawningMonorail == null)
return;
canv.repaint();
}
public void ChangeMonorail(DrawningMonorail newMonorail){
newMonorail.SetPosition(0,0);
DrawningMonorail = newMonorail;
canv.DrawningMonorail = DrawningMonorail;
}
public FormMonorail(){
MonorailFrame =new JFrame ();
JButton buttonCreate = new JButton("Создать");
JButton buttonCreateLocomotive = new JButton("Создать локомотив");
JButton buttonStep = new JButton("Шаг");
buttonSelect = 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();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color choosen = ChooseColor(MonorailFrame);
if(choosen != null){
color = choosen;
}
DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000),
color,
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();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color choosen = ChooseColor(MonorailFrame);
if(choosen != null){
color = choosen;
}
choosen = ChooseColor(MonorailFrame);
if(choosen != null){
additionalColor = choosen;
}
DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000),
color,
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(),
additionalColor);
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.DISPOSE_ON_CLOSE);
MonorailFrame.setLayout(null);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
buttonSelect.setBounds(383,401, 180, 40);
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.add(buttonSelect);
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();
}
}

View File

@ -0,0 +1,209 @@
package MonorailHard;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.Generics.MonorailGenericCollection;
import MonorailHard.Generics.MonorailGenericStorage;
import MonorailHard.Generics.MonorailTrashCollection;
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.Normalizer;
import java.util.List;
public class FormMonorailCollection {
private final MonorailGenericStorage _storage;
private JList<String> listBoxStorages;
private DefaultListModel<String> listBoxModel;
private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426;
CollectionCanvas canv;
void Draw(){
if(canv == null)
return;
canv.repaint();
}
private void ReloadObjects(){
int index = listBoxStorages.getSelectedIndex();
listBoxModel.clear();
List<String> keys = _storage.Keys();
for(int i = 0; i < keys.size(); i++){
listBoxModel.addElement(keys.get(i));
}
if(listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size()))
listBoxStorages.setSelectedIndex(0);
else if(listBoxModel.size() > 0)
listBoxStorages.setSelectedIndex(index);
}
public FormMonorailCollection(){
MonorailTrashCollection<DrawningMonorail> _trashCollection = new MonorailTrashCollection<>();
JButton callTrashButton = new JButton("мусор");
_storage = new MonorailGenericStorage(pictureBoxWidth, pictureBoxHeight);
JScrollPane scrollPane = new JScrollPane();
canv = new CollectionCanvas();
JPanel toolBox = new JPanel();
JTextField storageName = new JTextField();
JButton addStorageButton = new JButton("Добавить набор");
listBoxModel = new DefaultListModel<>();
listBoxStorages= new JList<>(listBoxModel);
scrollPane.setViewportView(listBoxStorages);
JButton delStorageButton = new JButton("Удалить набор");
toolBox.setBounds(623,12, 227, 80);
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497);
toolBox.setBounds(623, 12, 227, 426);
canv.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField monorailNumb = new JTextField();
GridLayout lay = new GridLayout(9,1);
toolBox.add(storageName);
toolBox.add(addStorageButton);
toolBox.add(scrollPane);
toolBox.add(delStorageButton);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(monorailNumb);
toolBox.add(removeButton);
toolBox.add(refreshButton);
toolBox.add(callTrashButton);
collectionFrame.add(toolBox);
collectionFrame.add(canv);
collectionFrame.setVisible(true);
canv._storage = _storage;
canv.listBoxStorages = listBoxStorages;
addStorageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(storageName.getText() == null)
return;
_storage.AddSet(storageName.getText());
ReloadObjects();
}
});
delStorageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
_storage.DelSet(listBoxStorages.getSelectedValue());
ReloadObjects();
}
});
callTrashButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_trashCollection.GetSize() == 0)
return;
FormMonorail form = new FormMonorail();
form.ChangeMonorail(_trashCollection.GetTop());
_trashCollection.Pop();
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
FormMonorail form = new FormMonorail();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_monorails.Insert(form.SelectedMonorail()))
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedMonorail()._pictureWidth = pictureBoxWidth;
form.SelectedMonorail()._pictureHeight = pictureBoxHeight;
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.MonorailFrame.dispose();
Draw();
}
});
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
if(_monorails == null) {
return;
}
String tmp = monorailNumb.getText();
int numb;
try{
numb = Integer.parseInt(tmp);
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
DrawningMonorail curMonorail = _monorails.Get(numb);
_trashCollection.Push(curMonorail);
_monorails.Remove(numb);
_monorails.ShowMonorails();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
if(_monorails == null) {
return;
}
_monorails.ShowMonorails();
Draw();
}
});
}
}
class CollectionCanvas extends JComponent {
public MonorailGenericStorage _storage;
public JList<String> listBoxStorages;
public CollectionCanvas(){
}
@Override
public void paintComponent (Graphics g){
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowMonorails(), 0, 0, this);
super.repaint();
}
}

View File

@ -0,0 +1,61 @@
package MonorailHard.Generics;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.DrawningObjects.IDraw;
import MonorailHard.Entities.EntityMonorail;
import java.util.Random;
public class HardGeneric <T extends EntityMonorail,U extends IDraw>{
T[] arrFirst;
U[] arrSecond;
private int curSz;
private int CountFirst;
private int CountSecond;
private int pictureBoxWidth;
private int pictureBoxHeight;
public HardGeneric(int countFirst, int countSecond, int width, int height){
curSz = 0;
CountFirst = countFirst;
CountSecond = countSecond;
arrFirst = (T[]) new EntityMonorail[CountFirst];
arrSecond = (U[]) new IDraw[CountSecond];
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public int InsertFirst(T entityMonorail){
if(arrFirst[CountFirst-1] != null)
return -1;
for(int i = curSz -1; i>= 0; i--) {
arrFirst[i + 1] = arrFirst[i];
arrSecond[i + 1] = arrSecond[i];
}
curSz++;
arrFirst[0] = entityMonorail;
return 0;
}
public int InsertSecond(U inter){
if(arrSecond[CountSecond-1] != null)
return -1;
arrSecond[0] = inter;
return 0;
}
public DrawningMonorail MakeObject(){
Random rand = new Random();
int indFirst = rand.nextInt(0, curSz);
int indSecond = rand.nextInt(0,curSz);
EntityMonorail entity = arrFirst[indFirst];
IDraw inter = arrSecond[indSecond];
DrawningMonorail monorail = new DrawningMonorail(entity.Speed(), entity.Weight(), entity.BodyColor(),
entity.WheelColor(), entity.TireColor(), pictureBoxWidth, pictureBoxHeight);
return monorail;
}
}

View File

@ -0,0 +1,92 @@
package MonorailHard.Generics;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MonorailGenericCollection<T extends DrawningMonorail, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 133;
private final int _placeSizeHeight = 50;
private final SetGeneric<T> _collection;
public MonorailGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Size(){
return _collection.Count;
}
public boolean Insert(T obj){
if (obj == null)
{
return false;
}
return _collection.Insert(obj);
}
public boolean Remove(int position){
return _collection.Remove(position);
}
public U GetU(int pos){
T ans = _collection.Get(pos);
if(ans == null)
return null;
return (U)ans.GetMoveableObject();
}
public T Get(int position){
if(position < 0 || position >= _collection.Count)
return null;
return _collection.Get(position);
}
private void DrawBackground(Graphics g)
{
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{
g.drawLine(i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
DrawningMonorail monorail = _collection.Get(i);
if (monorail != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
monorail.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight);
monorail.DrawMonorail((Graphics2D) g);
}
}
}
public BufferedImage ShowMonorails()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
}

View File

@ -0,0 +1,49 @@
package MonorailHard.Generics;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
public class MonorailGenericStorage {
final HashMap<String, MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail>> _monorailStorages;
public List<String> Keys(){
if(_monorailStorages == null)
return null;
return _monorailStorages.keySet().stream().collect(Collectors.toList());
}
private final int _pictureWidth;
private final int _pictureHeight;
public MonorailGenericStorage(int pictureWidth, int pictureHeight){
_monorailStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(String name){
if(_monorailStorages.containsKey(name))
return;
_monorailStorages.put(name, new MonorailGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name){
if(!_monorailStorages.containsKey(name))
return;
_monorailStorages.remove(name);
}
public MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> Get(String name){
if(!_monorailStorages.containsKey(name))
return null;
return _monorailStorages.get(name);
}
public DrawningMonorail Get(String collectionName, int position){
return _monorailStorages.get(collectionName).Get(position);
}
}

View File

@ -0,0 +1,32 @@
package MonorailHard.Generics;
import MonorailHard.DrawningObjects.DrawningMonorail;
import java.util.ArrayDeque;
import java.util.Queue;
public class MonorailTrashCollection <T extends DrawningMonorail> {
Queue <T> _queue;
public MonorailTrashCollection(){
_queue = new ArrayDeque<>();
}
public void Push(T monorail){
_queue.add(monorail);
}
public int GetSize(){
return _queue.size();
}
public void Pop(){
if(_queue.size() ==0)
return;
_queue.remove();
}
public T GetTop(){
return _queue.peek();
}
}

View File

@ -0,0 +1,56 @@
package MonorailHard.Generics;
import java.util.ArrayList;
import java.util.List;
public class SetGeneric <T extends Object>{
private final List<T> _places;
public int Count;
private final int _maxCount;
public SetGeneric(int count){
_maxCount = count;
_places = new ArrayList<>();
}
public boolean Insert(T monorail){
if(_places.size() == _maxCount)
return false;
Insert(monorail, 0);
return true;
}
public boolean Insert(T monorail, int position){
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
return false;
_places.add(position, monorail);
Count++;
return true;
}
public boolean Remove(int position){
if(!(position >= 0 && position < _places.size()))
return false;
_places.remove(position);
Count--;
return true;
}
public T Get(int position){
if(!(position >= 0 && position < Count))
return null;
return (T)_places.get(position);
}
public ArrayList<T> GetMonorails(int maxMonorails){
ArrayList<T> toRet = new ArrayList<>();
for(int i = 0; i < _places.size(); i++){
toRet.add(_places.get(i));
if(i == maxMonorails)
return toRet;
}
return toRet;
}
}

View File

@ -0,0 +1,77 @@
package MonorailHard;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.DrawningObjects.DrawningWheels;
import MonorailHard.DrawningObjects.IDraw;
import MonorailHard.Entities.EntityMonorail;
import MonorailHard.Generics.HardGeneric;
import javax.swing.*;
import javax.swing.text.html.parser.Entity;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class HardForm {
private Canvas canv;
private int pictureBoxWidth;
private int pictureBoxHeight;
private EntityMonorail makeEntity(){
Random rand = new Random();
return new EntityMonorail(rand.nextInt(100, 300), rand.nextDouble(1000,3000),
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)),
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)),
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)));
}
void Draw(){
if(canv == null)
return;
canv.repaint();
}
private IDraw makeIDraw(EntityMonorail entity){
Random rand = new Random();
return new DrawningWheels(pictureBoxWidth, pictureBoxHeight, 0, 0, entity.WheelColor(), entity.TireColor());
}
public HardForm(){
Random rand = new Random();
int sz = rand.nextInt(1, 10);
JFrame HardFrame = new JFrame();
HardFrame.setSize(700, 400);
HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
canv = new Canvas();
canv.setBounds(0,0,pictureBoxWidth,pictureBoxHeight);
JButton makeObject = new JButton("Создать");
makeObject.setBounds(0, 0, 100, 40);
canv.add(makeObject);
HardFrame.setContentPane(canv);
HardFrame.setVisible(true);
pictureBoxHeight = canv.getHeight();
pictureBoxWidth = canv.getWidth();
HardGeneric<EntityMonorail, IDraw> toDraw= new HardGeneric<>(sz,
sz, pictureBoxWidth, pictureBoxHeight);
for(int i = 0; i < sz; i++){
EntityMonorail ent = makeEntity();
toDraw.InsertFirst(ent);
toDraw.InsertSecond(makeIDraw(ent));
}
makeObject.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningMonorail DrawningMonorail = toDraw.MakeObject();
DrawningMonorail.SetPosition(pictureBoxWidth / 2 - DrawningMonorail.GetWidth()/2,
pictureBoxHeight / 2 - DrawningMonorail.GetHeight()/2);
canv.DrawningMonorail = DrawningMonorail;
Draw();
}
}
);
}
}

View File

@ -0,0 +1,20 @@
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;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
FormMonorailCollection form = new FormMonorailCollection();
}
}

View 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;
}
}

View File

@ -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);
}
}

View 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);
}

View 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();
}
}
}
}

View 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();
}
}
}
}

View 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;
}
}

View File

@ -0,0 +1,7 @@
package MonorailHard.MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}

View File

@ -0,0 +1,7 @@
package MonorailHard;
public enum NumberType {
Two,
Three,
Four
}