Compare commits
No commits in common. "LabHard03" and "main" have entirely different histories.
1
.gitignore
vendored
@ -23,5 +23,4 @@
|
|||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
hs_err_pid*
|
hs_err_pid*
|
||||||
replay_pid*
|
replay_pid*
|
||||||
.idea
|
|
||||||
|
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
package DoubleDeckerBus;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import DoubleDeckerBus.DrawningObjects.*;
|
|
||||||
|
|
||||||
public class Canvas extends JComponent {
|
|
||||||
public DrawningBus DrawningBus;
|
|
||||||
public void paintComponent (Graphics g){
|
|
||||||
if (DrawningBus == null){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
super.paintComponents (g) ;
|
|
||||||
Graphics2D g2d = (Graphics2D)g;
|
|
||||||
DrawningBus.DrawTransport(g2d);
|
|
||||||
super.repaint();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package DoubleDeckerBus;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
|
||||||
import DoubleDeckerBus.Generics.TheBusesCollectionGeneric;
|
|
||||||
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class CollectionCanvas extends JComponent {
|
|
||||||
public TheBusesCollectionGeneric<DrawningBus, DrawningObjectBus> _bus;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void paintComponent (Graphics g){
|
|
||||||
if (_bus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
super.paintComponents(g);
|
|
||||||
Graphics2D g2d = (Graphics2D)g;
|
|
||||||
g2d.drawImage(_bus.ShowTheBuses(), 0, 0, this);
|
|
||||||
super.repaint();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package DoubleDeckerBus;
|
|
||||||
public enum DirectionType {
|
|
||||||
Up,
|
|
||||||
Down,
|
|
||||||
Left,
|
|
||||||
Right
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
package DoubleDeckerBus;
|
|
||||||
public enum DoorNumberType {
|
|
||||||
Three,
|
|
||||||
Four,
|
|
||||||
Five
|
|
||||||
}
|
|
@ -1,186 +0,0 @@
|
|||||||
package DoubleDeckerBus.DrawningObjects;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DirectionType;
|
|
||||||
import DoubleDeckerBus.Entities.EntityBus;
|
|
||||||
import DoubleDeckerBus.MovementStrategy.*;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class DrawningBus {
|
|
||||||
protected EntityBus EntityBus;
|
|
||||||
public int _pictureWidth;
|
|
||||||
public int _pictureHeight;
|
|
||||||
protected int _startPosX;
|
|
||||||
protected int _startPosY;
|
|
||||||
protected int _busWidth = 160;
|
|
||||||
protected int _busHeight = 100;
|
|
||||||
protected IDraw DrawningDoor;
|
|
||||||
|
|
||||||
public EntityBus EntityBus() {
|
|
||||||
return EntityBus;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DrawningBus(int speed, double weight, Color bodyColor, int width, int height) {
|
|
||||||
if (width <= _busWidth || height <= _busHeight) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_startPosX = 0;
|
|
||||||
_startPosY = 0;
|
|
||||||
_pictureWidth = width;
|
|
||||||
_pictureHeight = height;
|
|
||||||
EntityBus = new EntityBus(speed, weight, bodyColor);
|
|
||||||
Random random = new Random();
|
|
||||||
int option = random.nextInt(1,4);
|
|
||||||
if (option == 1) {
|
|
||||||
DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY);
|
|
||||||
} else if (option == 2) {
|
|
||||||
DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY);
|
|
||||||
} else if (option == 3) {
|
|
||||||
DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY);
|
|
||||||
}
|
|
||||||
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected DrawningBus(int speed, double weight, Color bodyColor,
|
|
||||||
int width, int height, int busWidth, int busHeight) {
|
|
||||||
if (width <= _busWidth || height <= _busHeight) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_startPosX = 0;
|
|
||||||
_startPosY = 0;
|
|
||||||
_pictureWidth = width;
|
|
||||||
_pictureHeight = height;
|
|
||||||
_busWidth = busWidth;
|
|
||||||
_busHeight = busHeight;
|
|
||||||
EntityBus = new EntityBus(speed, weight, bodyColor);
|
|
||||||
Random random = new Random();
|
|
||||||
int option = random.nextInt(1,4);
|
|
||||||
if (option == 1) {
|
|
||||||
DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY);
|
|
||||||
} else if (option == 2) {
|
|
||||||
DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY);
|
|
||||||
} else if (option == 3) {
|
|
||||||
DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY);
|
|
||||||
}
|
|
||||||
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetPosition(int x, int y){
|
|
||||||
if (EntityBus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_startPosX = x;
|
|
||||||
_startPosY = y;
|
|
||||||
DrawningDoor.ChangeX(_startPosX);
|
|
||||||
DrawningDoor.ChangeY(_startPosY);
|
|
||||||
|
|
||||||
if (x + _busWidth >= _pictureWidth || y + _busHeight >= _pictureHeight || x < 0 || y < 0) {
|
|
||||||
_startPosX = 0;
|
|
||||||
_startPosY = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetPosX() {
|
|
||||||
return _startPosX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetPosY() {
|
|
||||||
return _startPosY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetWidth() {
|
|
||||||
return _busWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetHeight() {
|
|
||||||
return _busHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IMoveableObject GetMoveableObject(){
|
|
||||||
return new DrawningObjectBus(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean CanMove(DirectionType direction)
|
|
||||||
{
|
|
||||||
if (EntityBus == null)
|
|
||||||
return false;
|
|
||||||
boolean can = false;
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case Left:
|
|
||||||
can = _startPosX - EntityBus.Step() >= 0;
|
|
||||||
break;
|
|
||||||
case Right:
|
|
||||||
can = _startPosX + EntityBus.Step() + _busWidth < _pictureWidth;
|
|
||||||
break;
|
|
||||||
case Down:
|
|
||||||
can = _startPosY + EntityBus.Step() + _busHeight < _pictureHeight;
|
|
||||||
break;
|
|
||||||
case Up:
|
|
||||||
can = _startPosY - EntityBus.Step() >= 0;
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
return can;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void MoveTransport(DirectionType direction){
|
|
||||||
if (!CanMove(direction) || EntityBus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case Left:
|
|
||||||
if (_startPosX - EntityBus.Step() >= 0) {
|
|
||||||
_startPosX -= (int) EntityBus.Step();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Up:
|
|
||||||
if (_startPosY - EntityBus.Step() >= 0) {
|
|
||||||
_startPosY -= (int) EntityBus.Step();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Right:
|
|
||||||
if (_startPosX + EntityBus.Step() + _busWidth <= _pictureWidth) {
|
|
||||||
_startPosX += (int) EntityBus.Step();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Down:
|
|
||||||
if (_startPosY + EntityBus.Step() + _busHeight <= _pictureHeight) {
|
|
||||||
_startPosY += (int) EntityBus.Step();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
DrawningDoor.ChangeX(_startPosX);
|
|
||||||
DrawningDoor.ChangeY(_startPosY);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawTransport(Graphics2D g2d) {
|
|
||||||
if (EntityBus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Границы первого этажа автобуса
|
|
||||||
g2d.setColor(EntityBus.BodyColor());
|
|
||||||
g2d.fillRect(_startPosX + 40, _startPosY + 40, 120, 40);
|
|
||||||
|
|
||||||
// Колеса
|
|
||||||
g2d.setColor(Color.black);
|
|
||||||
g2d.fillOval(_startPosX + 45, _startPosY + 75, 20, 20);
|
|
||||||
g2d.fillOval(_startPosX + 127, _startPosY + 75, 20, 20);
|
|
||||||
|
|
||||||
// Окна
|
|
||||||
g2d.setColor(Color.blue);
|
|
||||||
g2d.fillOval(_startPosX + 50, _startPosY + 45, 20, 20);
|
|
||||||
g2d.fillOval(_startPosX + 75, _startPosY + 45, 20, 20);
|
|
||||||
g2d.fillOval(_startPosX + 100, _startPosY + 45, 20, 20);
|
|
||||||
g2d.fillOval(_startPosX + 125, _startPosY + 45, 20, 20);
|
|
||||||
|
|
||||||
// двери
|
|
||||||
DrawningDoor.DrawDoors(g2d);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
package DoubleDeckerBus.DrawningObjects;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DoorNumberType;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class DrawningDoor implements IDraw {
|
|
||||||
private DoorNumberType DoorNumberType;
|
|
||||||
private Color blackColor;
|
|
||||||
private int Width;
|
|
||||||
private int Height;
|
|
||||||
public int CurX;
|
|
||||||
public int CurY;
|
|
||||||
|
|
||||||
public DrawningDoor(int width, int height, int curX, int curY) {
|
|
||||||
Width = width;
|
|
||||||
Height = height;
|
|
||||||
CurX = curX;
|
|
||||||
CurY = curY;
|
|
||||||
blackColor = Color.BLACK;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ChangeX(int x){
|
|
||||||
CurX = x;
|
|
||||||
}
|
|
||||||
public void ChangeY(int y){
|
|
||||||
CurY = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ChangeDoorsNumber(int x) {
|
|
||||||
if (x <= 3) {
|
|
||||||
DoorNumberType = DoorNumberType.Three;
|
|
||||||
}
|
|
||||||
if (x == 4) {
|
|
||||||
DoorNumberType = DoorNumberType.Four;
|
|
||||||
}
|
|
||||||
if (x >= 5) {
|
|
||||||
DoorNumberType = DoorNumberType.Five;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public DoorNumberType DoorNumberType() {
|
|
||||||
return DoorNumberType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawDoor(Graphics2D g2d, int x, int y){
|
|
||||||
g2d.fillRect(x, y, 10, 15);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawDoors(Graphics2D g2d) {
|
|
||||||
g2d.setColor(blackColor);
|
|
||||||
DrawDoor(g2d, CurX + 55, CurY + 65);
|
|
||||||
DrawDoor(g2d, CurX + 70, CurY + 65);
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 85, CurY + 65);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 100, CurY + 65);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 115, CurY + 65);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
package DoubleDeckerBus.DrawningObjects;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DoorNumberType;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class DrawningDoorOval implements IDraw {
|
|
||||||
private DoorNumberType DoorNumberType;
|
|
||||||
private Color blackColor;
|
|
||||||
private int Width;
|
|
||||||
private int Height;
|
|
||||||
public int CurX;
|
|
||||||
public int CurY;
|
|
||||||
|
|
||||||
public DrawningDoorOval(int width, int height, int curX, int curY) {
|
|
||||||
Width = width;
|
|
||||||
Height = height;
|
|
||||||
CurX = curX;
|
|
||||||
CurY = curY;
|
|
||||||
blackColor = Color.BLACK;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ChangeX(int x){
|
|
||||||
CurX = x;
|
|
||||||
}
|
|
||||||
public void ChangeY(int y){
|
|
||||||
CurY = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ChangeDoorsNumber(int x) {
|
|
||||||
if (x <= 3) {
|
|
||||||
DoorNumberType = DoorNumberType.Three;
|
|
||||||
}
|
|
||||||
if (x == 4) {
|
|
||||||
DoorNumberType = DoorNumberType.Four;
|
|
||||||
}
|
|
||||||
if (x >= 5) {
|
|
||||||
DoorNumberType = DoorNumberType.Five;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public DoorNumberType DoorNumberType() {
|
|
||||||
return DoorNumberType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawDoor(Graphics2D g2d, int x, int y){
|
|
||||||
g2d.fillOval(x, y, 10, 15);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawDoors(Graphics2D g2d) {
|
|
||||||
g2d.setColor(blackColor);
|
|
||||||
DrawDoor(g2d, CurX + 55, CurY + 65);
|
|
||||||
DrawDoor(g2d, CurX + 70, CurY + 65);
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 85, CurY + 65);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 100, CurY + 65);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 115, CurY + 65);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,70 +0,0 @@
|
|||||||
package DoubleDeckerBus.DrawningObjects;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DoorNumberType;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class DrawningDoorTriangle implements IDraw {
|
|
||||||
private DoubleDeckerBus.DoorNumberType DoorNumberType;
|
|
||||||
private Color blackColor;
|
|
||||||
private int Width;
|
|
||||||
private int Height;
|
|
||||||
public int CurX;
|
|
||||||
public int CurY;
|
|
||||||
|
|
||||||
public DrawningDoorTriangle(int width, int height, int curX, int curY) {
|
|
||||||
Width = width;
|
|
||||||
Height = height;
|
|
||||||
CurX = curX;
|
|
||||||
CurY = curY;
|
|
||||||
blackColor = Color.BLACK;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ChangeX(int x){
|
|
||||||
CurX = x;
|
|
||||||
}
|
|
||||||
public void ChangeY(int y){
|
|
||||||
CurY = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ChangeDoorsNumber(int x) {
|
|
||||||
if (x <= 3) {
|
|
||||||
DoorNumberType = DoorNumberType.Three;
|
|
||||||
}
|
|
||||||
if (x == 4) {
|
|
||||||
DoorNumberType = DoorNumberType.Four;
|
|
||||||
}
|
|
||||||
if (x >= 5) {
|
|
||||||
DoorNumberType = DoorNumberType.Five;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public DoorNumberType DoorNumberType() {
|
|
||||||
return DoorNumberType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawDoor(Graphics2D g2d, int x, int y){
|
|
||||||
int[] xPoints = {x, x + 5, x + 10};
|
|
||||||
int[] yPoints = {y + 20, y, y + 20};
|
|
||||||
g2d.fillPolygon(xPoints, yPoints, 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawDoors(Graphics2D g2d) {
|
|
||||||
g2d.setColor(blackColor);
|
|
||||||
DrawDoor(g2d, CurX + 55, CurY + 60);
|
|
||||||
DrawDoor(g2d, CurX + 70, CurY + 60);
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 85, CurY + 60);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 100, CurY + 60);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DoorNumberType() == DoorNumberType.Five) {
|
|
||||||
DrawDoor(g2d, CurX + 115, CurY + 60);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
package DoubleDeckerBus.DrawningObjects;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.Entities.EntityDoubleDeckerBus;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class DrawningDoubleDeckerBus extends DrawningBus {
|
|
||||||
|
|
||||||
public DrawningDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, int doorNumber,
|
|
||||||
int width, int height, boolean secondFloor, boolean tailpipe) {
|
|
||||||
super(speed, weight, bodyColor, width, height);
|
|
||||||
if (EntityBus != null) {
|
|
||||||
EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, doorNumber, secondFloor, tailpipe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void DrawTransport(Graphics2D g2d) {
|
|
||||||
|
|
||||||
if (!(EntityBus instanceof EntityDoubleDeckerBus)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
super.DrawTransport(g2d);
|
|
||||||
|
|
||||||
EntityDoubleDeckerBus _bus = (EntityDoubleDeckerBus) EntityBus;
|
|
||||||
|
|
||||||
// второй этаж
|
|
||||||
if (_bus.SecondFloor())
|
|
||||||
{
|
|
||||||
g2d.setColor(_bus.BodyColor());
|
|
||||||
// Границы второго этажа автобуса
|
|
||||||
g2d.setColor(_bus.AdditionalColor());
|
|
||||||
g2d.fillRect(_startPosX + 40, _startPosY, 120, 40);
|
|
||||||
|
|
||||||
// Окна второго этажа
|
|
||||||
g2d.setColor(Color.BLUE);
|
|
||||||
g2d.setColor(Color.blue);
|
|
||||||
g2d.fillOval(_startPosX + 50, _startPosY +10,20, 20);
|
|
||||||
g2d.fillOval(_startPosX + 75, _startPosY +10, 20, 20);
|
|
||||||
g2d.fillOval(_startPosX + 100, _startPosY +10,20, 20);
|
|
||||||
g2d.fillOval(_startPosX + 125, _startPosY +10,20,20);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_bus.Tailpipe())
|
|
||||||
{
|
|
||||||
g2d.setColor(_bus.AdditionalColor());
|
|
||||||
g2d.fillRect(_startPosX, _startPosY + 65, 40, 15);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package DoubleDeckerBus.DrawningObjects;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DoorNumberType;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public interface IDraw {
|
|
||||||
public void ChangeDoorsNumber(int x);
|
|
||||||
public DoorNumberType DoorNumberType();
|
|
||||||
public void DrawDoor(Graphics2D g2d, int x, int y);
|
|
||||||
public void DrawDoors(Graphics2D g2d);
|
|
||||||
public void ChangeX(int x);
|
|
||||||
public void ChangeY(int y);
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package DoubleDeckerBus.Entities;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class EntityBus {
|
|
||||||
public int Speed;
|
|
||||||
public double Weight;
|
|
||||||
public Color BodyColor;
|
|
||||||
public double Step;
|
|
||||||
|
|
||||||
public int Speed() {
|
|
||||||
return Speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double Weight() {
|
|
||||||
return Weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color BodyColor() {
|
|
||||||
return BodyColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double Step() {
|
|
||||||
return Step;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EntityBus(int speed, double weight, Color bodyColor) {
|
|
||||||
Speed = speed;
|
|
||||||
Weight = weight;
|
|
||||||
Step = (double) Speed * 100 / Weight;
|
|
||||||
BodyColor = bodyColor;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package DoubleDeckerBus.Entities;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class EntityDoubleDeckerBus extends EntityBus {
|
|
||||||
public Color AdditionalColor;
|
|
||||||
public boolean SecondFloor;
|
|
||||||
public boolean Tailpipe;
|
|
||||||
|
|
||||||
public Color AdditionalColor() {
|
|
||||||
return AdditionalColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean SecondFloor() {
|
|
||||||
return SecondFloor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean Tailpipe() {
|
|
||||||
return Tailpipe;
|
|
||||||
}
|
|
||||||
public double Step() {
|
|
||||||
return Step;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor,
|
|
||||||
int doorNumber , boolean secondFloor ,boolean tailpipe) {
|
|
||||||
super(speed, weight, bodyColor);
|
|
||||||
AdditionalColor = additionalColor;
|
|
||||||
SecondFloor = secondFloor;
|
|
||||||
Tailpipe = tailpipe;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,109 +0,0 @@
|
|||||||
package DoubleDeckerBus;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
|
||||||
import DoubleDeckerBus.Generics.TheBusesCollectionGeneric;
|
|
||||||
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.*;
|
|
||||||
|
|
||||||
public class FormBusCollection {
|
|
||||||
private final TheBusesCollectionGeneric<DrawningBus, DrawningObjectBus> _bus;
|
|
||||||
private int pictureBoxWidth = 605;
|
|
||||||
private int pictureBoxHeight = 426;
|
|
||||||
|
|
||||||
CollectionCanvas canvas;
|
|
||||||
|
|
||||||
public void Draw() {
|
|
||||||
if (canvas == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
canvas.repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
public FormBusCollection() {
|
|
||||||
_bus = new TheBusesCollectionGeneric<>(pictureBoxWidth, pictureBoxHeight);
|
|
||||||
canvas = new CollectionCanvas();
|
|
||||||
JPanel toolBox = new JPanel();
|
|
||||||
JFrame collectionFrame = new JFrame();
|
|
||||||
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
|
||||||
collectionFrame.setSize(880,497);
|
|
||||||
toolBox.setBounds(623, 12, 227, 426);
|
|
||||||
canvas.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
|
|
||||||
JButton addButton = new JButton("Добавить");
|
|
||||||
JButton removeButton = new JButton("Удалить");
|
|
||||||
JButton refreshButton = new JButton("Обновить");
|
|
||||||
JTextField busNumb = new JTextField();
|
|
||||||
GridLayout lay = new GridLayout(4,1);
|
|
||||||
toolBox.setLayout(lay);
|
|
||||||
toolBox.add(addButton);
|
|
||||||
toolBox.add(busNumb);
|
|
||||||
toolBox.add(removeButton);
|
|
||||||
toolBox.add(refreshButton);
|
|
||||||
collectionFrame.add(toolBox);
|
|
||||||
collectionFrame.add(canvas);
|
|
||||||
collectionFrame.setVisible(true);
|
|
||||||
|
|
||||||
addButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if(_bus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
FormDoubleDeckerBus form = new FormDoubleDeckerBus();
|
|
||||||
form.buttonSelect.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (_bus.Insert(form.SelectedBus()) != -1)
|
|
||||||
{
|
|
||||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|
||||||
form.SelectedBus()._pictureWidth = pictureBoxWidth;
|
|
||||||
form.SelectedBus()._pictureHeight = pictureBoxHeight;
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|
||||||
}
|
|
||||||
canvas._bus = _bus;
|
|
||||||
form.BusFrame.dispose();
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
removeButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if(_bus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String tmp = busNumb.getText();
|
|
||||||
int numb;
|
|
||||||
|
|
||||||
try {
|
|
||||||
numb = Integer.parseInt(tmp);
|
|
||||||
} catch (Exception ex){
|
|
||||||
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_bus.Remove(numb);
|
|
||||||
_bus.ShowTheBuses();
|
|
||||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
refreshButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if(_bus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_bus.ShowTheBuses();
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,218 +0,0 @@
|
|||||||
package DoubleDeckerBus;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DrawningObjects.DrawningDoubleDeckerBus;
|
|
||||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
|
||||||
import DoubleDeckerBus.Entities.EntityBus;
|
|
||||||
import DoubleDeckerBus.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 FormDoubleDeckerBus {
|
|
||||||
static DrawningBus DrawningBus;
|
|
||||||
static AbstractStrategy _abstractStrategy;
|
|
||||||
|
|
||||||
public JButton buttonSelect;
|
|
||||||
public JFrame BusFrame;
|
|
||||||
|
|
||||||
Canvas canvas;
|
|
||||||
static int pictureBoxWidth = 882;
|
|
||||||
static int pictureBoxHeight = 453;
|
|
||||||
|
|
||||||
public Color ChooseColor(JFrame BusFrame){
|
|
||||||
JColorChooser dialog = new JColorChooser();
|
|
||||||
Color res = JColorChooser.showDialog(BusFrame, "Выберите цвет", Color.WHITE);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DrawningBus SelectedBus(){
|
|
||||||
return DrawningBus;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Draw() {
|
|
||||||
if (DrawningBus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
canvas.repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
public FormDoubleDeckerBus() {
|
|
||||||
BusFrame = new JFrame();
|
|
||||||
JButton buttonCreate = new JButton("Создать");
|
|
||||||
JButton buttonCreateDoubleDeckerBus = new JButton("Создать двухэтажный автобус");
|
|
||||||
JButton buttonStep = new JButton("Шаг");
|
|
||||||
buttonSelect = new JButton ("Выбрать");
|
|
||||||
JComboBox comboBoxStrategy = new JComboBox(new String[]{"Довести до центра", "Довести до края"});
|
|
||||||
JButton UpButton = new JButton();
|
|
||||||
UpButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\UpArrow.png"));
|
|
||||||
JButton DownButton = new JButton();
|
|
||||||
DownButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\DownArrow.png"));
|
|
||||||
JButton LeftButton = new JButton();
|
|
||||||
LeftButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\LeftArrow.png"));
|
|
||||||
JButton RightButton = new JButton();
|
|
||||||
RightButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\RightArrow.png"));
|
|
||||||
|
|
||||||
buttonStep.addActionListener(
|
|
||||||
new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent e){
|
|
||||||
if (DrawningBus == 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 DrawningObjectBus(DrawningBus), 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(BusFrame);
|
|
||||||
if (choosen != null){
|
|
||||||
color = choosen;
|
|
||||||
}
|
|
||||||
DrawningBus = new DrawningBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
|
||||||
color, pictureBoxWidth, pictureBoxHeight);
|
|
||||||
canvas.DrawningBus = DrawningBus;
|
|
||||||
comboBoxStrategy.enable(true);
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
buttonCreateDoubleDeckerBus.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(BusFrame);
|
|
||||||
if (choosen != null){
|
|
||||||
color = choosen;
|
|
||||||
}
|
|
||||||
choosen = ChooseColor(BusFrame);
|
|
||||||
if (choosen != null){
|
|
||||||
additionalColor = choosen;
|
|
||||||
}
|
|
||||||
DrawningBus = new DrawningDoubleDeckerBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
|
||||||
color, additionalColor, random.nextInt(1, 6), pictureBoxWidth, pictureBoxHeight,
|
|
||||||
random.nextBoolean(), random.nextBoolean());
|
|
||||||
canvas.DrawningBus = DrawningBus;
|
|
||||||
comboBoxStrategy.enable(true);
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
RightButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (DrawningBus.EntityBus() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
DrawningBus.MoveTransport(DirectionType.Right);
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
LeftButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (DrawningBus.EntityBus() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
DrawningBus.MoveTransport(DirectionType.Left);
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
UpButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (DrawningBus.EntityBus() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
DrawningBus.MoveTransport(DirectionType.Up);
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
DownButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (DrawningBus.EntityBus() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
DrawningBus.MoveTransport(DirectionType.Down);
|
|
||||||
Draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
BusFrame.setSize(900, 500);
|
|
||||||
BusFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
BusFrame.setLayout(null);
|
|
||||||
canvas = new Canvas();
|
|
||||||
canvas.setSize(pictureBoxWidth, pictureBoxHeight);
|
|
||||||
buttonSelect.setBounds(453,401, 180, 40);
|
|
||||||
buttonCreateDoubleDeckerBus.setBounds(198, 401, 250, 40);
|
|
||||||
buttonCreate.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);
|
|
||||||
BusFrame.add(canvas);
|
|
||||||
BusFrame.add(buttonCreate);
|
|
||||||
BusFrame.add(buttonCreateDoubleDeckerBus);
|
|
||||||
BusFrame.add(UpButton);
|
|
||||||
BusFrame.add(DownButton);
|
|
||||||
BusFrame.add(LeftButton);
|
|
||||||
BusFrame.add(RightButton);
|
|
||||||
BusFrame.add(comboBoxStrategy);
|
|
||||||
BusFrame.add(buttonStep);
|
|
||||||
BusFrame.add(buttonSelect);
|
|
||||||
BusFrame.setVisible(true);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
package DoubleDeckerBus.Generics;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
|
||||||
import DoubleDeckerBus.DrawningObjects.IDraw;
|
|
||||||
import DoubleDeckerBus.Entities.EntityBus;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class HardGeneric <T extends EntityBus,U extends IDraw> {
|
|
||||||
ArrayList<T> Buses;
|
|
||||||
ArrayList<U> Doors;
|
|
||||||
|
|
||||||
private int CountBuses;
|
|
||||||
private int MaxCountBuses;
|
|
||||||
private int CountDoors;
|
|
||||||
private int MaxCountDoors;
|
|
||||||
|
|
||||||
private int pictureBoxWidth;
|
|
||||||
|
|
||||||
private int pictureBoxHeight;
|
|
||||||
|
|
||||||
public HardGeneric(int maxCountBuses, int maxCountDoors, int width, int height) {
|
|
||||||
MaxCountBuses = maxCountBuses;
|
|
||||||
MaxCountDoors = maxCountDoors;
|
|
||||||
Buses = new ArrayList<T>(MaxCountBuses);
|
|
||||||
Doors = new ArrayList<U>(MaxCountDoors);
|
|
||||||
CountBuses = 0;
|
|
||||||
CountDoors = 0;
|
|
||||||
pictureBoxHeight = height;
|
|
||||||
pictureBoxWidth = width;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean Add(T bus) {
|
|
||||||
if (bus == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (CountBuses > MaxCountBuses) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Buses.add(CountBuses++, bus);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean Add(U door) {
|
|
||||||
if (door == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (CountDoors > MaxCountDoors) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Doors.add(CountDoors++, door);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DrawningBus MakeObject() {
|
|
||||||
if (CountBuses == 0 || CountDoors == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Random rand = new Random();
|
|
||||||
int indBus = rand.nextInt(0, CountBuses);
|
|
||||||
int indDoors = rand.nextInt(0,CountDoors);
|
|
||||||
T entity = Buses.get(indBus);
|
|
||||||
U door = Doors.get(indDoors);
|
|
||||||
return new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(), pictureBoxWidth, pictureBoxHeight);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
package DoubleDeckerBus.Generics;
|
|
||||||
|
|
||||||
public class SetGeneric<T extends Object>{
|
|
||||||
private final Object[] _places;
|
|
||||||
|
|
||||||
public int Count;
|
|
||||||
|
|
||||||
public SetGeneric(int count){
|
|
||||||
_places = new Object[count];
|
|
||||||
Count = count;
|
|
||||||
}
|
|
||||||
public int Insert(T bus){
|
|
||||||
return Insert(bus, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Insert(T bus, int position){
|
|
||||||
if (!(position >= 0 && position < Count)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_places[position] == null){
|
|
||||||
_places[position] = bus;
|
|
||||||
} else {
|
|
||||||
int place = -1;
|
|
||||||
|
|
||||||
for (int i = position; i < Count; i++) {
|
|
||||||
if (_places[i] == null) {
|
|
||||||
place = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (place == -1) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = place - 1; i >= position; i--) {
|
|
||||||
_places[i + 1] = _places[i];
|
|
||||||
}
|
|
||||||
_places[position] = bus;
|
|
||||||
}
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean Remove(int position){
|
|
||||||
if (!(position >= 0 && position < Count)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
_places[position] = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T Get(int position){
|
|
||||||
if (!(position >= 0 && position < Count)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return (T)_places[position];
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
package DoubleDeckerBus.Generics;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
|
||||||
import DoubleDeckerBus.MovementStrategy.IMoveableObject;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
|
|
||||||
public class TheBusesCollectionGeneric<T extends DrawningBus, U extends IMoveableObject> {
|
|
||||||
private final int _pictureWidth;
|
|
||||||
|
|
||||||
private final int _pictureHeight;
|
|
||||||
|
|
||||||
private final int _placeSizeWidth = 172;
|
|
||||||
|
|
||||||
private final int _placeSizeHeight = 112;
|
|
||||||
|
|
||||||
private final SetGeneric<T> _collection;
|
|
||||||
|
|
||||||
public TheBusesCollectionGeneric(int picWidth, int picHeight){
|
|
||||||
int width = picWidth / _placeSizeWidth;
|
|
||||||
int height = picHeight / _placeSizeHeight;
|
|
||||||
_pictureWidth = picWidth;
|
|
||||||
_pictureHeight = picHeight;
|
|
||||||
_collection = new SetGeneric<T>(width * height);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Insert(T obj){
|
|
||||||
if (obj == null) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
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 BufferedImage ShowTheBuses() {
|
|
||||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
|
|
||||||
Graphics gr = bmp.createGraphics();
|
|
||||||
DrawBackground(gr);
|
|
||||||
DrawObjects(gr);
|
|
||||||
return bmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
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++) {
|
|
||||||
DrawningBus bus = _collection.Get(i);
|
|
||||||
if (bus != null) {
|
|
||||||
int inRow = _pictureWidth / _placeSizeWidth;
|
|
||||||
bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight);
|
|
||||||
bus.DrawTransport((Graphics2D) g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package DoubleDeckerBus;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
public static void main(String[] args) throws IOException {
|
|
||||||
FormBusCollection formbuscollection = new FormBusCollection();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,98 +0,0 @@
|
|||||||
package DoubleDeckerBus.MovementStrategy;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
package DoubleDeckerBus.MovementStrategy;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
|
||||||
import DoubleDeckerBus.DirectionType;
|
|
||||||
|
|
||||||
public class DrawningObjectBus implements IMoveableObject {
|
|
||||||
private final DrawningBus _drawningBus;
|
|
||||||
|
|
||||||
public DrawningObjectBus(DrawningBus drawningBus){
|
|
||||||
_drawningBus = drawningBus;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ObjectParameters GetObjectParameters(){
|
|
||||||
if (_drawningBus == null || _drawningBus.EntityBus() == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new ObjectParameters(_drawningBus.GetPosX(), _drawningBus.GetPosY(),
|
|
||||||
_drawningBus.GetWidth(), _drawningBus.GetHeight());
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetStep(){
|
|
||||||
if (_drawningBus.EntityBus() == null) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return (int)_drawningBus.EntityBus().Step();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean CheckCanMove(DirectionType direction){
|
|
||||||
if (_drawningBus == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return _drawningBus.CanMove(direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void MoveObject(DirectionType direction){
|
|
||||||
if (_drawningBus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_drawningBus.MoveTransport(direction);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package DoubleDeckerBus.MovementStrategy;
|
|
||||||
|
|
||||||
import DoubleDeckerBus.DirectionType;
|
|
||||||
|
|
||||||
public interface IMoveableObject {
|
|
||||||
public ObjectParameters GetObjectParameters();
|
|
||||||
public int GetStep();
|
|
||||||
boolean CheckCanMove(DirectionType direction);
|
|
||||||
void MoveObject(DirectionType direction);
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
package DoubleDeckerBus.MovementStrategy;
|
|
||||||
|
|
||||||
public class MoveToBorder extends AbstractStrategy {
|
|
||||||
@Override
|
|
||||||
protected boolean IsTargetDestination() {
|
|
||||||
var objParams = GetObjectParameters();
|
|
||||||
|
|
||||||
if (objParams == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package DoubleDeckerBus.MovementStrategy;
|
|
||||||
public class MoveToCenter extends AbstractStrategy{
|
|
||||||
@Override
|
|
||||||
protected boolean IsTargetDestination() {
|
|
||||||
ObjectParameters 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() {
|
|
||||||
ObjectParameters objParams = GetObjectParameters();
|
|
||||||
|
|
||||||
if (objParams == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth() / 2;
|
|
||||||
if (Math.abs(diffX) > GetStep()) {
|
|
||||||
if (diffX > 0) {
|
|
||||||
MoveLeft();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
MoveRight();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight() / 2;
|
|
||||||
if (Math.abs(diffY) > GetStep()) {
|
|
||||||
if (diffY > 0) {
|
|
||||||
MoveUp();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
MoveDown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package DoubleDeckerBus.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package DoubleDeckerBus.MovementStrategy;
|
|
||||||
|
|
||||||
public enum Status {
|
|
||||||
NotInit,
|
|
||||||
InProgress,
|
|
||||||
Finish
|
|
||||||
}
|
|
Before Width: | Height: | Size: 9.9 KiB |
Before Width: | Height: | Size: 9.4 KiB |
Before Width: | Height: | Size: 8.6 KiB |
BIN
Resources/up.png
Before Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 9.9 KiB |
Before Width: | Height: | Size: 9.4 KiB |
Before Width: | Height: | Size: 8.6 KiB |
Before Width: | Height: | Size: 10 KiB |