Первая лабораторная работа
@ -1,3 +1,5 @@
|
||||
package DoubleDeckerBus;
|
||||
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
@ -1,3 +1,5 @@
|
||||
package DoubleDeckerBus;
|
||||
|
||||
public enum DoorNumberType {
|
||||
Three,
|
||||
Four,
|
190
DoubleDeckerBus/DrawningObjects/DrawningBus.java
Normal file
@ -0,0 +1,190 @@
|
||||
package DoubleDeckerBus.DrawningObjects;
|
||||
|
||||
import DoubleDeckerBus.DirectionType;
|
||||
import DoubleDeckerBus.Entities.EntityBus;
|
||||
import java.util.Scanner;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningBus {
|
||||
protected JPanel BusPanel;
|
||||
protected EntityBus EntityBus;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected int _busWidth = 160;
|
||||
protected int _busHeight = 100;
|
||||
|
||||
|
||||
private IDraw DrawningDoor;
|
||||
|
||||
public EntityBus EntityBus() {
|
||||
return EntityBus;
|
||||
}
|
||||
|
||||
public DrawningBus(int speed, double weight, Color bodyColor, int width, int height, JPanel busPanel) {
|
||||
if (width <= _busWidth || height <= _busHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
busPanel.setSize(width, height);
|
||||
BusPanel = busPanel;
|
||||
busPanel.paint(BusPanel.getGraphics());
|
||||
_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, busPanel);
|
||||
} else if (option == 2) {
|
||||
DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY, busPanel);
|
||||
} else if (option == 3) {
|
||||
DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY, busPanel);
|
||||
}
|
||||
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
|
||||
}
|
||||
|
||||
protected DrawningBus(int speed, double weight, Color bodyColor,
|
||||
int width, int height, int busWidth, int busHeight, JPanel busPanel) {
|
||||
if (width <= _busWidth || height <= _busHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
busPanel.setSize(width, height);
|
||||
BusPanel = busPanel;
|
||||
busPanel.paint(BusPanel.getGraphics());
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_busWidth = busWidth;
|
||||
_busHeight = busHeight;
|
||||
EntityBus = new EntityBus(speed, weight, bodyColor);
|
||||
Random random = new Random();
|
||||
int option = random.nextInt(4) + 1;
|
||||
if (option == 1) {
|
||||
DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY, busPanel);
|
||||
} else if (option == 2) {
|
||||
DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY, busPanel);
|
||||
} else if (option == 3) {
|
||||
DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY, busPanel);
|
||||
}
|
||||
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y){
|
||||
if (x < 0 || x + _busWidth > _pictureWidth)
|
||||
{
|
||||
x = Math.max(0, _pictureWidth - _busWidth);
|
||||
}
|
||||
if (y < 0 || y + _busHeight > _pictureHeight)
|
||||
{
|
||||
y = Math.max(0, _pictureHeight - _busHeight);
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public int GetPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
|
||||
public int GetPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
|
||||
public int GetWidth() {
|
||||
return _busWidth;
|
||||
}
|
||||
|
||||
public int GetHeight() {
|
||||
return _busHeight;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
BusPanel.paint(BusPanel.getGraphics());
|
||||
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 = (Graphics2D)BusPanel.getGraphics();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,8 +1,12 @@
|
||||
package DoubleDeckerBus.DrawningObjects;
|
||||
|
||||
import DoubleDeckerBus.DoorNumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningDoor {
|
||||
JPanel DoubleDeckerBusPanel;
|
||||
public class DrawningDoor implements IDraw {
|
||||
JPanel BusPanel;
|
||||
private DoorNumberType DoorNumberType;
|
||||
private Color blackColor;
|
||||
private int Width;
|
||||
@ -10,14 +14,20 @@ public class DrawningDoor {
|
||||
public int CurX;
|
||||
public int CurY;
|
||||
|
||||
public boolean Init(int width, int height, int curX, int curY, JPanel doubleDeckerBusPanel) {
|
||||
public DrawningDoor(int width, int height, int curX, int curY, JPanel busPanel) {
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
blackColor = Color.BLACK;
|
||||
DoubleDeckerBusPanel = doubleDeckerBusPanel;
|
||||
return true;
|
||||
BusPanel = busPanel;
|
||||
}
|
||||
|
||||
public void ChangeX(int x){
|
||||
CurX = x;
|
||||
}
|
||||
public void ChangeY(int y){
|
||||
CurY = y;
|
||||
}
|
||||
|
||||
public void ChangeDoorsNumber(int x) {
|
||||
@ -36,22 +46,27 @@ public class DrawningDoor {
|
||||
return DoorNumberType;
|
||||
}
|
||||
|
||||
public void DrawDoor(int x, int y){
|
||||
Graphics2D g2d = (Graphics2D)BusPanel.getGraphics();
|
||||
g2d.fillRect(x, y, 10, 15);
|
||||
}
|
||||
|
||||
public void DrawDoors() {
|
||||
Graphics2D g2d = (Graphics2D) DoubleDeckerBusPanel.getGraphics();
|
||||
Graphics2D g2d = (Graphics2D) BusPanel.getGraphics();
|
||||
g2d.setColor(blackColor);
|
||||
g2d.fillRect(CurX + 55, CurY + 65, 10, 15);
|
||||
g2d.fillRect(CurX + 70, CurY + 65, 10, 15);
|
||||
DrawDoor(CurX + 55, CurY + 65);
|
||||
DrawDoor(CurX + 70, CurY + 65);
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
||||
g2d.fillRect(CurX + 85, CurY + 65, 10, 15);
|
||||
DrawDoor(CurX + 85, CurY + 65);
|
||||
}
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
||||
g2d.fillRect(CurX + 100, CurY + 65, 10, 15);
|
||||
DrawDoor(CurX + 100, CurY + 65);
|
||||
}
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Five) {
|
||||
g2d.fillRect(CurX + 115, CurY + 65, 10, 15);
|
||||
DrawDoor(CurX + 115, CurY + 65);
|
||||
}
|
||||
}
|
||||
}
|
72
DoubleDeckerBus/DrawningObjects/DrawningDoorOval.java
Normal file
@ -0,0 +1,72 @@
|
||||
package DoubleDeckerBus.DrawningObjects;
|
||||
|
||||
import DoubleDeckerBus.DoorNumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningDoorOval implements IDraw {
|
||||
JPanel BusPanel;
|
||||
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, JPanel busPanel) {
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
blackColor = Color.BLACK;
|
||||
BusPanel = busPanel;
|
||||
}
|
||||
|
||||
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(int x, int y){
|
||||
Graphics2D g2d = (Graphics2D)BusPanel.getGraphics();
|
||||
g2d.fillOval(x, y, 10, 20);
|
||||
}
|
||||
|
||||
public void DrawDoors() {
|
||||
Graphics2D g2d = (Graphics2D) BusPanel.getGraphics();
|
||||
g2d.setColor(blackColor);
|
||||
DrawDoor(CurX + 55, CurY + 60);
|
||||
DrawDoor(CurX + 70, CurY + 60);
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
||||
DrawDoor(CurX + 85, CurY + 60);
|
||||
}
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
||||
DrawDoor(CurX + 100, CurY + 60);
|
||||
}
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Five) {
|
||||
DrawDoor(CurX + 115, CurY + 60);
|
||||
}
|
||||
}
|
||||
}
|
74
DoubleDeckerBus/DrawningObjects/DrawningDoorTriangle.java
Normal file
@ -0,0 +1,74 @@
|
||||
package DoubleDeckerBus.DrawningObjects;
|
||||
|
||||
import DoubleDeckerBus.DoorNumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningDoorTriangle implements IDraw {
|
||||
JPanel BusPanel;
|
||||
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, JPanel busPanel) {
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
blackColor = Color.BLACK;
|
||||
BusPanel = busPanel;
|
||||
}
|
||||
|
||||
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(int x, int y){
|
||||
Graphics2D g2d = (Graphics2D)BusPanel.getGraphics();
|
||||
int[] xPoints = {x, x + 5, x + 10};
|
||||
int[] yPoints = {y + 20, y, y + 20};
|
||||
g2d.fillPolygon(xPoints, yPoints, 3);
|
||||
}
|
||||
|
||||
public void DrawDoors() {
|
||||
Graphics2D g2d = (Graphics2D) BusPanel.getGraphics();
|
||||
g2d.setColor(blackColor);
|
||||
DrawDoor(CurX + 55, CurY + 60);
|
||||
DrawDoor(CurX + 70, CurY + 60);
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
||||
DrawDoor(CurX + 85, CurY + 60);
|
||||
}
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
|
||||
DrawDoor(CurX + 100, CurY + 60);
|
||||
}
|
||||
|
||||
if (DoorNumberType() == DoorNumberType.Five) {
|
||||
DrawDoor(CurX + 115, CurY + 60);
|
||||
}
|
||||
}
|
||||
}
|
53
DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java
Normal file
@ -0,0 +1,53 @@
|
||||
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, JPanel busPanel) {
|
||||
super(speed, weight, bodyColor, width, height, busPanel);
|
||||
if (EntityBus != null) {
|
||||
EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, doorNumber, secondFloor, tailpipe);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport() {
|
||||
|
||||
if (!(EntityBus instanceof EntityDoubleDeckerBus)) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.DrawTransport();
|
||||
|
||||
EntityDoubleDeckerBus _bus = (EntityDoubleDeckerBus) EntityBus;
|
||||
Graphics2D g2d = (Graphics2D)BusPanel.getGraphics();
|
||||
|
||||
// второй этаж
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
12
DoubleDeckerBus/DrawningObjects/IDraw.java
Normal file
@ -0,0 +1,12 @@
|
||||
package DoubleDeckerBus.DrawningObjects;
|
||||
|
||||
import DoubleDeckerBus.DoorNumberType;
|
||||
|
||||
public interface IDraw {
|
||||
public void ChangeDoorsNumber(int x);
|
||||
public DoorNumberType DoorNumberType();
|
||||
public void DrawDoor(int x, int y);
|
||||
public void DrawDoors();
|
||||
public void ChangeX(int x);
|
||||
public void ChangeY(int y);
|
||||
}
|
33
DoubleDeckerBus/Entities/EntityBus.java
Normal file
@ -0,0 +1,33 @@
|
||||
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;
|
||||
}
|
||||
}
|
37
DoubleDeckerBus/Entities/EntityDoubleDeckerBus.java
Normal file
@ -0,0 +1,37 @@
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
167
DoubleDeckerBus/FormDoubleDeckerBus.java
Normal file
@ -0,0 +1,167 @@
|
||||
package DoubleDeckerBus;
|
||||
|
||||
import DoubleDeckerBus.DrawningObjects.DrawningDoubleDeckerBus;
|
||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
||||
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 static void main(String[] args) throws IOException {
|
||||
String[] items = {"Довести до центра", "Довести до края"};
|
||||
|
||||
JComboBox comboBoxStrategy = new JComboBox(items);
|
||||
comboBoxStrategy.setBounds(562,12,151,28);
|
||||
JFrame BusFrame = new JFrame();
|
||||
BusFrame.setResizable(false);
|
||||
JPanel BusPanel = new JPanel();
|
||||
BusFrame.setLayout(new BorderLayout());
|
||||
BusFrame.setSize(743, 576);
|
||||
BusFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
BusFrame.setLayout(new BorderLayout(1,1));
|
||||
BusPanel.setLayout(null);
|
||||
BufferedImage RightIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png"));
|
||||
BufferedImage LeftIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png"));
|
||||
BufferedImage UpIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png"));
|
||||
BufferedImage DownIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.png"));
|
||||
|
||||
JButton RightButton = new JButton(new ImageIcon(RightIcon));
|
||||
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
|
||||
JButton UpButton = new JButton(new ImageIcon(UpIcon));
|
||||
JButton DownButton = new JButton(new ImageIcon(DownIcon));
|
||||
JButton CreateButton = new JButton();
|
||||
JButton CreateDoubleDeckerBusButton = new JButton();
|
||||
JButton buttonStep = new JButton();
|
||||
|
||||
CreateDoubleDeckerBusButton.setBounds(198,477,180, 40);
|
||||
CreateDoubleDeckerBusButton.setText("Создать двухэтажный автобус");
|
||||
CreateButton.setText("Создать");
|
||||
buttonStep.setBounds(619, 46, 94, 29);
|
||||
buttonStep.setText("шаг");
|
||||
CreateButton.setBounds(12, 477, 180, 40);
|
||||
RightButton.setBounds(683,487,30,30);
|
||||
LeftButton.setBounds(611,487,30,30);
|
||||
UpButton.setBounds(647,451,30,30);
|
||||
DownButton.setBounds(647,487,30,30);
|
||||
|
||||
BusPanel.add(CreateButton);
|
||||
BusPanel.add(CreateDoubleDeckerBusButton);
|
||||
BusPanel.add(RightButton);
|
||||
BusPanel.add(LeftButton);
|
||||
BusPanel.add(UpButton);
|
||||
BusPanel.add(DownButton);
|
||||
BusPanel.add(comboBoxStrategy);
|
||||
BusPanel.add(buttonStep);
|
||||
comboBoxStrategy.setSelectedIndex(-1);
|
||||
|
||||
BusFrame.add(BusPanel, BorderLayout.CENTER);
|
||||
Random random = new Random();
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
DrawningBus = new DrawningBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
BusPanel.getWidth(), BusPanel.getHeight(), BusPanel);
|
||||
DrawningBus.DrawTransport();
|
||||
comboBoxStrategy.enable(true);
|
||||
}
|
||||
});
|
||||
|
||||
CreateDoubleDeckerBusButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
DrawningBus = new DrawningDoubleDeckerBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
random.nextInt(1, 6), BusPanel.getWidth(), BusPanel.getHeight(), random.nextBoolean(), random.nextBoolean(),
|
||||
BusPanel);
|
||||
DrawningBus.DrawTransport();
|
||||
comboBoxStrategy.enable(true);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
buttonStep.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (DrawningBus == null) {
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled()) {
|
||||
if (comboBoxStrategy.getSelectedIndex() == 0) {
|
||||
_abstractStrategy = new MoveToCenter();
|
||||
}
|
||||
else if (comboBoxStrategy.getSelectedIndex() == 1) {
|
||||
_abstractStrategy = new MoveToBorder();
|
||||
} else {
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
if (_abstractStrategy == null) {
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawningObjectBus(DrawningBus), BusPanel.getWidth(),
|
||||
BusPanel.getHeight());
|
||||
comboBoxStrategy.enable(false);
|
||||
}
|
||||
if (_abstractStrategy == null) {
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
DrawningBus.DrawTransport();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish) {
|
||||
comboBoxStrategy.enable(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
RightButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningBus.EntityBus() == null) {
|
||||
return;
|
||||
}
|
||||
DrawningBus.MoveTransport(DirectionType.Right);
|
||||
DrawningBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
LeftButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningBus.EntityBus() == null)
|
||||
return;
|
||||
DrawningBus.MoveTransport(DirectionType.Left);
|
||||
DrawningBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
UpButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningBus.EntityBus() == null)
|
||||
return;
|
||||
DrawningBus.MoveTransport(DirectionType.Up);
|
||||
DrawningBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
DownButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningBus.EntityBus() == null)
|
||||
return;
|
||||
DrawningBus.MoveTransport(DirectionType.Down);
|
||||
DrawningBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
|
||||
BusFrame.setVisible(true);
|
||||
}
|
||||
}
|
98
DoubleDeckerBus/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,98 @@
|
||||
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;
|
||||
}
|
||||
}
|
40
DoubleDeckerBus/MovementStrategy/DrawningObjectBus.java
Normal file
@ -0,0 +1,40 @@
|
||||
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);
|
||||
}
|
||||
}
|
10
DoubleDeckerBus/MovementStrategy/IMoveableObject.java
Normal file
@ -0,0 +1,10 @@
|
||||
package DoubleDeckerBus.MovementStrategy;
|
||||
|
||||
import DoubleDeckerBus.DirectionType;
|
||||
|
||||
public interface IMoveableObject {
|
||||
public ObjectParameters GetObjectParameters();
|
||||
public int GetStep();
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
37
DoubleDeckerBus/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,37 @@
|
||||
package DoubleDeckerBus.MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy {
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
ObjectParameters 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() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
int diffX = objParams.RightBorder - FieldWidth();
|
||||
if (Math.abs(diffX) >= GetStep()) {
|
||||
if (diffX < 0) {
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
|
||||
int diffY = objParams.DownBorder - FieldHeight();
|
||||
if (Math.abs(diffY) >= GetStep()) {
|
||||
if (diffY < 0) {
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
50
DoubleDeckerBus/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,50 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
DoubleDeckerBus/MovementStrategy/ObjectParameters.java
Normal file
@ -0,0 +1,29 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
7
DoubleDeckerBus/MovementStrategy/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package DoubleDeckerBus.MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
@ -1,137 +0,0 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningDoubleDeckerBus {
|
||||
JPanel DoubleDeckerBusPanel;
|
||||
private EntityDoubleDeckerBus EntityDoubleDeckerBus;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX = 0;
|
||||
private int _startPosY = 0;
|
||||
private final int _busWidth = 160;
|
||||
private final int _busHeight = 100;
|
||||
private DrawningDoor DrawningDoor;
|
||||
|
||||
public EntityDoubleDeckerBus EntityDoubleDeckerBus() {
|
||||
return EntityDoubleDeckerBus;
|
||||
}
|
||||
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, int doorNumber,
|
||||
int width, int height, boolean secondFloor, boolean tailpipe, JPanel doubleDeckerBusPanel) {
|
||||
if (width <= _busWidth || height <= _busHeight) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
doubleDeckerBusPanel.setSize(width, height);
|
||||
DoubleDeckerBusPanel = doubleDeckerBusPanel;
|
||||
doubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics());
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityDoubleDeckerBus = new EntityDoubleDeckerBus();
|
||||
EntityDoubleDeckerBus.Init(speed, weight, bodyColor, additionalColor, doorNumber, secondFloor, tailpipe);
|
||||
DrawningDoor = new DrawningDoor();
|
||||
DrawningDoor.Init(_busWidth, _busHeight, _startPosX, _startPosY, doubleDeckerBusPanel);
|
||||
Random random = new Random();
|
||||
DrawningDoor.ChangeDoorsNumber(random.nextInt(5) + 2);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y){
|
||||
if (EntityDoubleDeckerBus == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
if (x + _busWidth <= _pictureWidth || y + _busHeight <= _pictureHeight) {
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction){
|
||||
if (EntityDoubleDeckerBus == null)
|
||||
return;
|
||||
DoubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics());
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
if (_startPosX - EntityDoubleDeckerBus.Step() >= 0)
|
||||
_startPosX -= (int)EntityDoubleDeckerBus.Step();
|
||||
else
|
||||
_startPosX = 0;
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityDoubleDeckerBus.Step() >= 0)
|
||||
_startPosY -= (int)EntityDoubleDeckerBus.Step();
|
||||
else
|
||||
_startPosY = 0;
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + EntityDoubleDeckerBus.Step() + _busWidth < _pictureWidth)
|
||||
_startPosX += (int)EntityDoubleDeckerBus.Step();
|
||||
else
|
||||
_startPosX = _pictureWidth - _busWidth;
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + EntityDoubleDeckerBus.Step() + _busHeight < _pictureHeight)
|
||||
_startPosY += (int)EntityDoubleDeckerBus.Step();
|
||||
else
|
||||
_startPosY = _pictureHeight - _busHeight;
|
||||
break;
|
||||
}
|
||||
DrawningDoor.CurX = _startPosX;
|
||||
DrawningDoor.CurY = _startPosY;
|
||||
}
|
||||
|
||||
public void DrawTransport() {
|
||||
Graphics2D g2d = (Graphics2D) DoubleDeckerBusPanel.getGraphics();
|
||||
|
||||
if (EntityDoubleDeckerBus == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Границы первого этажа автобуса
|
||||
g2d.setColor(EntityDoubleDeckerBus.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();
|
||||
|
||||
// Второй этаж
|
||||
if (EntityDoubleDeckerBus.SecondFloor()) {
|
||||
g2d.setColor(EntityDoubleDeckerBus.AdditionalColor());
|
||||
// Границы второго этажа автобуса
|
||||
g2d.fillRect(_startPosX + 40, _startPosY, 120, 40);
|
||||
|
||||
// Окна второго этажа
|
||||
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 (EntityDoubleDeckerBus.Tailpipe()) {
|
||||
g2d.setColor(EntityDoubleDeckerBus.AdditionalColor());
|
||||
g2d.fillRect(_startPosX, _startPosY + 65, 40, 15);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityDoubleDeckerBus {
|
||||
private int Speed;
|
||||
private int DoorNumber;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
private Color AdditionalColor;
|
||||
private boolean SecondFloor;
|
||||
private boolean Tailpipe;
|
||||
private double Step;
|
||||
|
||||
public int Speed() {
|
||||
return Speed;
|
||||
}
|
||||
|
||||
public int DoorNumber() {
|
||||
return DoorNumber;
|
||||
}
|
||||
|
||||
public double Weight() {
|
||||
return Weight;
|
||||
}
|
||||
|
||||
public Color BodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
|
||||
public Color AdditionalColor() {
|
||||
return AdditionalColor;
|
||||
}
|
||||
|
||||
public boolean SecondFloor() {
|
||||
return SecondFloor;
|
||||
}
|
||||
|
||||
public boolean Tailpipe() {
|
||||
return Tailpipe;
|
||||
}
|
||||
public double Step() {
|
||||
return Step;
|
||||
}
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
int doorNumber, boolean secondFloor ,boolean tailpipe) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
Step = (double) Speed * 100 / Weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
SecondFloor = secondFloor;
|
||||
DoorNumber = doorNumber;
|
||||
Tailpipe = tailpipe;
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
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 {
|
||||
public static void main(String[] args) throws IOException {
|
||||
JFrame DoubleDeckerBusFrame = new JFrame();
|
||||
JPanel DoubleDeckerBusPanel = new JPanel();
|
||||
DoubleDeckerBusFrame.setLayout(new BorderLayout());
|
||||
DoubleDeckerBusFrame.setSize(900, 500);
|
||||
DoubleDeckerBusFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
DoubleDeckerBusFrame.setLayout(new BorderLayout(1,1));
|
||||
DrawningDoubleDeckerBus DrawningDoubleDeckerBus = new DrawningDoubleDeckerBus();
|
||||
DoubleDeckerBusPanel.setLayout(null);
|
||||
BufferedImage RightIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png"));
|
||||
BufferedImage LeftIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png"));
|
||||
BufferedImage UpIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png"));
|
||||
BufferedImage DownIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.png"));
|
||||
|
||||
JButton RightButton = new JButton(new ImageIcon(RightIcon));
|
||||
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
|
||||
JButton UpButton = new JButton(new ImageIcon(UpIcon));
|
||||
JButton DownButton = new JButton(new ImageIcon(DownIcon));
|
||||
JButton CreateButton = new JButton();
|
||||
CreateButton.setText("Create");
|
||||
CreateButton.setBounds(12, 401, 90, 40);
|
||||
RightButton.setBounds(840,411,30,30);
|
||||
LeftButton.setBounds(768,411,30,30);
|
||||
UpButton.setBounds(804,375,30,30);
|
||||
DownButton.setBounds(804,411,30,30);
|
||||
DoubleDeckerBusPanel.add(CreateButton);
|
||||
DoubleDeckerBusPanel.add(RightButton);
|
||||
DoubleDeckerBusPanel.add(LeftButton);
|
||||
DoubleDeckerBusPanel.add(UpButton);
|
||||
DoubleDeckerBusPanel.add(DownButton);
|
||||
DoubleDeckerBusFrame.add(DoubleDeckerBusPanel, BorderLayout.CENTER);
|
||||
Random random = new Random();
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
DrawningDoubleDeckerBus.Init(random.nextInt(201) + 100, random.nextDouble() * 2000 + 1000,
|
||||
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
|
||||
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
|
||||
random.nextInt(4) + 2, DoubleDeckerBusPanel.getWidth(), DoubleDeckerBusPanel.getHeight(),
|
||||
random.nextBoolean(), random.nextBoolean(), DoubleDeckerBusPanel);
|
||||
DrawningDoubleDeckerBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
RightButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
|
||||
return;
|
||||
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Right);
|
||||
DrawningDoubleDeckerBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
LeftButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
|
||||
return;
|
||||
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Left);
|
||||
DrawningDoubleDeckerBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
UpButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
|
||||
return;
|
||||
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Up);
|
||||
DrawningDoubleDeckerBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
DownButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
|
||||
return;
|
||||
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Down);
|
||||
DrawningDoubleDeckerBus.DrawTransport();
|
||||
}
|
||||
});
|
||||
|
||||
DoubleDeckerBusFrame.setVisible(true);
|
||||
}
|
||||
}
|
BIN
Resources/down.png
Normal file
After Width: | Height: | Size: 9.9 KiB |
BIN
Resources/left.png
Normal file
After Width: | Height: | Size: 9.4 KiB |
BIN
Resources/right.png
Normal file
After Width: | Height: | Size: 8.6 KiB |
BIN
Resources/up.png
Normal file
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 9.9 KiB |
After Width: | Height: | Size: 9.4 KiB |
After Width: | Height: | Size: 8.6 KiB |
After Width: | Height: | Size: 10 KiB |