laba_2
This commit is contained in:
parent
c1a4102d9d
commit
58c66f386c
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
@ -2,7 +2,7 @@
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-22_Smirnov_A_A_RoadTrain_HARD.iml" filepath="$PROJECT_DIR$/.idea/PIbd-22_Smirnov_A_A_RoadTrain_HARD.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/PIbd-22_Smirnov_A_A_RoadTrain_HARD.iml" filepath="$PROJECT_DIR$/PIbd-22_Smirnov_A_A_RoadTrain_HARD.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -1,87 +0,0 @@
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRoadTrain {
|
||||
private WheelDrawing wheelDrawing;
|
||||
public EntityRoadTrain EntityRoadTrain;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private int _roadTrainWidth = 200;
|
||||
private int _roadTrainHeight = 100;
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean wheel, boolean door, boolean light, int numWheel, int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureHeight < _roadTrainHeight || _pictureWidth < _roadTrainWidth)
|
||||
return false;
|
||||
EntityRoadTrain = new EntityRoadTrain();
|
||||
EntityRoadTrain.Init(speed, weight, bodyColor, additionalColor,wheel, door, light, numWheel);
|
||||
|
||||
wheelDrawing = new WheelDrawing();
|
||||
wheelDrawing.setNumWheel(numWheel);
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = Math.min(x, _pictureWidth-_roadTrainWidth);
|
||||
_startPosY = Math.min(y, _pictureHeight- _roadTrainHeight);
|
||||
}
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (EntityRoadTrain == null){
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
if (_startPosX - EntityRoadTrain.Step > 0)
|
||||
{
|
||||
_startPosX -= (int) EntityRoadTrain.Step;
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityRoadTrain.Step > 0)
|
||||
{
|
||||
_startPosY -= (int) EntityRoadTrain.Step;
|
||||
}
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + _roadTrainWidth + EntityRoadTrain.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int) EntityRoadTrain.Step;
|
||||
}
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + _roadTrainHeight + EntityRoadTrain.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int) EntityRoadTrain.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
if (EntityRoadTrain == null) {
|
||||
return;
|
||||
}
|
||||
// машина
|
||||
g.drawRect(_startPosX, _startPosY + 50, 160, 20); // кузов
|
||||
g.drawRect(_startPosX + 120, _startPosY + 10, 40, 40); // кабина
|
||||
g.drawRect(_startPosX + 10, _startPosY, 90, 50); // бак с водой
|
||||
g.drawRect(_startPosX + 130, _startPosY + 20, 30, 20); // окно
|
||||
g.drawLine(_startPosX + 160, _startPosY + 70, _startPosX + 180, _startPosY + 80); // держатель для щетки
|
||||
g.drawRect(_startPosX + 170, _startPosY + 80, 40, 10); // щетка
|
||||
// обвесы
|
||||
g.setColor(EntityRoadTrain.AdditionalColor);
|
||||
wheelDrawing.Draw(_startPosX, _startPosY, EntityRoadTrain.AdditionalColor, g);
|
||||
|
||||
if (EntityRoadTrain.Door) {
|
||||
g.fillRect(_startPosX + 40, _startPosY + 20, 20, 30); // дверь
|
||||
}
|
||||
if (EntityRoadTrain.Light) {
|
||||
g.fillRect(_startPosX + 135, _startPosY, 10, 10); // мигалка
|
||||
}
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityRoadTrain {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public Color AdditionalColor;
|
||||
public boolean Wheel;
|
||||
public boolean Door;
|
||||
public boolean Light;
|
||||
public double Step;
|
||||
public int numWheel;
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean wheel, boolean door, boolean light, int numwheel)
|
||||
{
|
||||
numWheel = numwheel;
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Wheel = wheel;
|
||||
Door = door;
|
||||
Light = light;
|
||||
Step = (double)Speed * 100 / Weight;
|
||||
}
|
||||
}
|
@ -1,128 +0,0 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormRoadTrain {
|
||||
private DrawingRoadTrain _drawingRoadTrain;
|
||||
Canvas canv;
|
||||
public void Draw(){
|
||||
canv.repaint();
|
||||
}
|
||||
public FormRoadTrain(){
|
||||
JFrame w=new JFrame ("RoadTrain");
|
||||
JButton buttonCreate = new JButton("Create");
|
||||
buttonCreate.setBorderPainted(false);
|
||||
buttonCreate.setContentAreaFilled(false);
|
||||
buttonCreate.setFont(new Font("Arial", Font.PLAIN, 20));
|
||||
JButton up = new JButton();
|
||||
up.setBorderPainted(false);
|
||||
up.setContentAreaFilled(false);
|
||||
up.setName("up");
|
||||
Image img_up = new ImageIcon("images/up.png").getImage();
|
||||
Image newImg_up = img_up.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
up.setIcon(new ImageIcon(newImg_up));
|
||||
|
||||
JButton down = new JButton();
|
||||
down.setBorderPainted(false);
|
||||
down.setContentAreaFilled(false);
|
||||
down.setName("down");
|
||||
Image img_down = new ImageIcon("images/down.png").getImage();
|
||||
Image newImg_down = img_down.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
down.setIcon(new ImageIcon(newImg_down));
|
||||
|
||||
JButton left = new JButton();
|
||||
left.setBorderPainted(false);
|
||||
left.setContentAreaFilled(false);
|
||||
left.setName("left");
|
||||
Image img_left = new ImageIcon("images/left.png").getImage();
|
||||
Image newImg_left = img_left.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
left.setIcon(new ImageIcon(newImg_left));
|
||||
|
||||
JButton right = new JButton();
|
||||
right.setBorderPainted(false);
|
||||
right.setContentAreaFilled(false);
|
||||
right.setName("right");
|
||||
Image img_right = new ImageIcon("images/right.png").getImage();
|
||||
Image newImg_right = img_right.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
right.setIcon(new ImageIcon(newImg_right));
|
||||
|
||||
buttonCreate.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
Random random = new Random();
|
||||
_drawingRoadTrain = new DrawingRoadTrain();
|
||||
_drawingRoadTrain.Init(random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,random.nextInt(0, 2) == 1,
|
||||
random.nextInt(1, 4), 1000, 560);
|
||||
_drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
canv._drawingRoadTrain = _drawingRoadTrain;
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
ActionListener actioListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
if (_drawingRoadTrain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch(((JButton)(e.getSource())).getName()){
|
||||
case "up":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
};
|
||||
up.addActionListener(actioListener);
|
||||
down.addActionListener(actioListener);
|
||||
left.addActionListener(actioListener);
|
||||
right.addActionListener(actioListener);
|
||||
|
||||
w.setSize (1000, 600);
|
||||
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
||||
w.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setBounds(0, 0, 1000, 600);
|
||||
buttonCreate.setBounds(30, 500, 100, 30);
|
||||
up.setBounds(880, 450, 20, 20);
|
||||
down.setBounds(880, 510, 20, 20);
|
||||
left.setBounds(850, 480, 20, 20);
|
||||
right.setBounds(910, 480, 20, 20);
|
||||
w.add(canv);
|
||||
w.add(buttonCreate);
|
||||
w.add(up);
|
||||
w.add(down);
|
||||
w.add(left);
|
||||
w.add(right);
|
||||
w.setVisible (true);
|
||||
}
|
||||
}
|
||||
|
||||
class Canvas extends JComponent{
|
||||
public DrawingRoadTrain _drawingRoadTrain;
|
||||
public Canvas(){
|
||||
}
|
||||
public void paintComponent (Graphics g){
|
||||
if (_drawingRoadTrain == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
_drawingRoadTrain.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
63
src/AbstractStrategy.java
Normal file
63
src/AbstractStrategy.java
Normal file
@ -0,0 +1,63 @@
|
||||
package src;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _moveableObject;
|
||||
private Status _state = Status.NotInit;
|
||||
protected int FieldWidth;
|
||||
protected int 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 (IsTargetDestinaion())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
protected boolean MoveLeft() { return MoveTo(Direction.Left);}
|
||||
protected boolean MoveRight() { return MoveTo(Direction.Right);}
|
||||
protected boolean MoveUp() { return MoveTo(Direction.Up);}
|
||||
protected boolean MoveDown() { return MoveTo(Direction.Down);}
|
||||
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
|
||||
protected int GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract boolean IsTargetDestinaion();
|
||||
private boolean MoveTo(Direction Direction)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject.CheckCanMove(Direction))
|
||||
{
|
||||
_moveableObject.MoveObject(Direction);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package src;
|
||||
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
27
src/DrawingObjectTrain.java
Normal file
27
src/DrawingObjectTrain.java
Normal file
@ -0,0 +1,27 @@
|
||||
package src;
|
||||
|
||||
public class DrawingObjectTrain implements IMoveableObject {
|
||||
|
||||
private DrawingRoadTrain _drawingRoadTrain = null;
|
||||
|
||||
public DrawingObjectTrain(DrawingRoadTrain drawingRoadTrain)
|
||||
{
|
||||
_drawingRoadTrain = drawingRoadTrain;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectPosition(){
|
||||
if (_drawingRoadTrain == null || _drawingRoadTrain.EntityRoadTrain ==
|
||||
null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingRoadTrain.GetPosX(),
|
||||
_drawingRoadTrain.GetPosY(), _drawingRoadTrain.GetWidth(), _drawingRoadTrain.GetHeight());
|
||||
}
|
||||
public int GetStep(){ return (int) _drawingRoadTrain.EntityRoadTrain.Step; }
|
||||
|
||||
public boolean CheckCanMove(Direction direction) { return _drawingRoadTrain.CanMove(direction);}
|
||||
|
||||
public void MoveObject(Direction direction) { _drawingRoadTrain.MoveTransport(direction); }
|
||||
|
||||
}
|
101
src/DrawingRoadTrain.java
Normal file
101
src/DrawingRoadTrain.java
Normal file
@ -0,0 +1,101 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingRoadTrain {
|
||||
protected IWheelDrawing wheelDrawing;
|
||||
public EntityRoadTrain EntityRoadTrain;
|
||||
protected int _pictureWidth;
|
||||
protected int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected int _roadTrainWidth = 200;
|
||||
protected int _roadTrainHeight = 100;
|
||||
public DrawingRoadTrain(int speed, double weight, Color bodyColor, int numWheel, int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureHeight < _roadTrainHeight || _pictureWidth < _roadTrainWidth)
|
||||
return;
|
||||
EntityRoadTrain = new EntityRoadTrain(speed, weight, bodyColor, numWheel);
|
||||
|
||||
Random random = new Random();
|
||||
switch(random.nextInt(0, 3)){
|
||||
case 0:
|
||||
wheelDrawing = new WheelDrawing();
|
||||
break;
|
||||
case 1:
|
||||
wheelDrawing = new WheelDrawingRect();
|
||||
break;
|
||||
case 2:
|
||||
wheelDrawing = new WheelDrawingTringle();
|
||||
break;
|
||||
default:
|
||||
wheelDrawing = new WheelDrawing();
|
||||
break;
|
||||
}
|
||||
wheelDrawing.setNumWheel(numWheel);
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = Math.min(x, _pictureWidth-_roadTrainWidth);
|
||||
_startPosY = Math.min(y, _pictureHeight- _roadTrainHeight);
|
||||
}
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityRoadTrain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
_startPosX -= (int)EntityRoadTrain.Step;
|
||||
break;
|
||||
case Up:
|
||||
_startPosY -= (int)EntityRoadTrain.Step;
|
||||
break;
|
||||
case Right:
|
||||
_startPosX += (int)EntityRoadTrain.Step;
|
||||
break;
|
||||
case Down:
|
||||
_startPosY += (int)EntityRoadTrain.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
if (EntityRoadTrain == null) {
|
||||
return;
|
||||
}
|
||||
// машина
|
||||
g.drawRect(_startPosX, _startPosY + 50, 160, 20); // кузов
|
||||
g.drawRect(_startPosX + 120, _startPosY + 10, 40, 40); // кабина
|
||||
g.drawRect(_startPosX + 130, _startPosY + 20, 30, 20); // окно
|
||||
wheelDrawing.Draw(_startPosX, _startPosY, Color.BLACK, g);
|
||||
}
|
||||
public int GetPosX (){return _startPosX;}
|
||||
public int GetPosY (){return _startPosY;}
|
||||
public int GetWidth (){return _roadTrainWidth;}
|
||||
public int GetHeight (){return _roadTrainHeight;}
|
||||
public boolean CanMove(Direction direction)
|
||||
{
|
||||
if (EntityRoadTrain == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
return _startPosX - EntityRoadTrain.Step > 0;
|
||||
case Right:
|
||||
return _startPosX + _roadTrainWidth + EntityRoadTrain.Step < _pictureWidth;
|
||||
case Up:
|
||||
return _startPosY - EntityRoadTrain.Step > 0;
|
||||
case Down:
|
||||
return _startPosY + _roadTrainHeight + EntityRoadTrain.Step < _pictureHeight;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
45
src/DrawingRoadTrainWithTank.java
Normal file
45
src/DrawingRoadTrainWithTank.java
Normal file
@ -0,0 +1,45 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRoadTrainWithTank extends DrawingRoadTrain{
|
||||
|
||||
public DrawingRoadTrainWithTank(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean tank, boolean brush, int _numWheel, int width, int height) {
|
||||
super(speed, weight, bodyColor, _numWheel, width, height);
|
||||
EntityRoadTrain = new EntityRoadTrainWithTank(speed, weight, bodyColor, additionalColor, tank, brush, _numWheel);
|
||||
}
|
||||
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = Math.min(x, _pictureWidth-_roadTrainWidth);
|
||||
_startPosY = Math.min(y, _pictureHeight-_roadTrainHeight);
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
|
||||
if (EntityRoadTrain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
super.DrawTransport(g);
|
||||
|
||||
g.setColor(((EntityRoadTrainWithTank)EntityRoadTrain).AdditionalColor);
|
||||
|
||||
|
||||
wheelDrawing.Draw(_startPosX, _startPosY, ((EntityRoadTrainWithTank) EntityRoadTrain).AdditionalColor, g);
|
||||
if (((EntityRoadTrainWithTank)EntityRoadTrain).Brush) {
|
||||
g.drawLine(_startPosX + 160, _startPosY + 70, _startPosX + 180, _startPosY + 80);
|
||||
g.drawRect(_startPosX + 170, _startPosY + 80, 40, 10); //щетка
|
||||
g.fillRect(_startPosX + 170, _startPosY + 80, 40, 10);
|
||||
}
|
||||
|
||||
if (((EntityRoadTrainWithTank)EntityRoadTrain).Tank) {
|
||||
g.drawRect(_startPosX + 10, _startPosY, 90, 50); //бак с водой
|
||||
g.fillRect(_startPosX + 10, _startPosY, 90, 50);
|
||||
}
|
||||
}
|
||||
}
|
19
src/EntityRoadTrain.java
Normal file
19
src/EntityRoadTrain.java
Normal file
@ -0,0 +1,19 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityRoadTrain {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public double Step;
|
||||
public int numWheel;
|
||||
public EntityRoadTrain(int speed, double weight, Color bodyColor, int numwheel)
|
||||
{
|
||||
numWheel = numwheel;
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
Step = (double)Speed * 100 / Weight;
|
||||
}
|
||||
}
|
21
src/EntityRoadTrainWithTank.java
Normal file
21
src/EntityRoadTrainWithTank.java
Normal file
@ -0,0 +1,21 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityRoadTrainWithTank extends EntityRoadTrain{
|
||||
|
||||
public Color AdditionalColor;
|
||||
|
||||
public boolean Tank;
|
||||
|
||||
public boolean Brush;
|
||||
|
||||
public EntityRoadTrainWithTank(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean tank, boolean brush, int _numWheel)
|
||||
{
|
||||
super(speed, weight, bodyColor, _numWheel);
|
||||
AdditionalColor = additionalColor;
|
||||
Tank = tank;
|
||||
Brush = brush;
|
||||
}
|
||||
}
|
218
src/FormRoadTrain.java
Normal file
218
src/FormRoadTrain.java
Normal file
@ -0,0 +1,218 @@
|
||||
package src;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormRoadTrain {
|
||||
private DrawingRoadTrain _drawingRoadTrain;
|
||||
private AbstractStrategy abstractStrategy;
|
||||
Canvas canv;
|
||||
static int pictureBoxWidth = 980;
|
||||
static int pictureBoxHeight = 580;
|
||||
public void Draw(){
|
||||
canv.repaint();
|
||||
}
|
||||
public FormRoadTrain(){
|
||||
JFrame w=new JFrame ("RoadTrain");
|
||||
JButton buttonCreateRoadTrain = new JButton("Normal");
|
||||
buttonCreateRoadTrain.setBorderPainted(false);
|
||||
buttonCreateRoadTrain.setContentAreaFilled(false);
|
||||
buttonCreateRoadTrain.setFont(new Font("Arial", Font.PLAIN, 20));
|
||||
buttonCreateRoadTrain.setSize(100,100);
|
||||
JButton buttonCreateRoadTrainWithTank = new JButton("With Tank");
|
||||
buttonCreateRoadTrainWithTank.setBorderPainted(false);
|
||||
buttonCreateRoadTrainWithTank.setContentAreaFilled(false);
|
||||
buttonCreateRoadTrainWithTank.setFont(new Font("Arial", Font.PLAIN, 20));
|
||||
JButton buttonStrategysStep = new JButton("strategys step");
|
||||
buttonStrategysStep.setBorderPainted(false);
|
||||
buttonStrategysStep.setContentAreaFilled(false);
|
||||
buttonStrategysStep.setFont(new Font("Arial", Font.PLAIN, 15));
|
||||
JComboBox comboBoxStrategy = new JComboBox(
|
||||
new String[]{
|
||||
"к центру",
|
||||
"к краю",
|
||||
});
|
||||
comboBoxStrategy.setFont(new Font("Arial", Font.PLAIN, 15));
|
||||
JButton up = new JButton();
|
||||
up.setBorderPainted(false);
|
||||
up.setFocusPainted(false);
|
||||
up.setContentAreaFilled(false);
|
||||
up.setName("up");
|
||||
Image img_up = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\up.png").getImage();
|
||||
Image newImg_up = img_up.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
up.setIcon(new ImageIcon(newImg_up));
|
||||
|
||||
JButton down = new JButton();
|
||||
down.setBorderPainted(false);
|
||||
down.setContentAreaFilled(false);
|
||||
down.setName("down");
|
||||
Image img_down = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\down.png").getImage();
|
||||
Image newImg_down = img_down.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
down.setIcon(new ImageIcon(newImg_down));
|
||||
|
||||
JButton left = new JButton();
|
||||
left.setBorderPainted(false);
|
||||
left.setContentAreaFilled(false);
|
||||
left.setName("left");
|
||||
Image img_left = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\left.png").getImage();
|
||||
Image newImg_left = img_left.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
left.setIcon(new ImageIcon(newImg_left));
|
||||
|
||||
JButton right = new JButton();
|
||||
right.setBorderPainted(false);
|
||||
right.setContentAreaFilled(false);
|
||||
right.setName("right");
|
||||
Image img_right = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\right.png").getImage();
|
||||
Image newImg_right = img_right.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
|
||||
right.setIcon(new ImageIcon(newImg_right));
|
||||
|
||||
buttonCreateRoadTrain.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
Random random = new Random();
|
||||
_drawingRoadTrain = new DrawingRoadTrain(
|
||||
random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
random.nextInt(1, 4),
|
||||
pictureBoxWidth,
|
||||
pictureBoxHeight);
|
||||
_drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
canv._drawingRoadTrain = _drawingRoadTrain;
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreateRoadTrainWithTank.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
Random random = new Random();
|
||||
_drawingRoadTrain = new DrawingRoadTrainWithTank(
|
||||
random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
random.nextInt(0, 2) == 1,
|
||||
random.nextInt(0, 2) == 1,
|
||||
random.nextInt(1, 4),
|
||||
pictureBoxWidth,
|
||||
pictureBoxHeight);
|
||||
_drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
canv._drawingRoadTrain = _drawingRoadTrain;
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonStrategysStep.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
if (_drawingRoadTrain == 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 DrawingObjectTrain(_drawingRoadTrain), pictureBoxWidth, pictureBoxHeight);
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
if (_drawingRoadTrain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch(((JButton)(e.getSource())).getName()){
|
||||
case "up":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawingRoadTrain.MoveTransport(Direction.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
};
|
||||
up.addActionListener(actionListener);
|
||||
down.addActionListener(actionListener);
|
||||
left.addActionListener(actionListener);
|
||||
right.addActionListener(actionListener);
|
||||
|
||||
w.setSize (1000, 600);
|
||||
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
||||
w.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
|
||||
buttonCreateRoadTrain.setBounds(2, 540, 150, 20);
|
||||
buttonCreateRoadTrainWithTank.setBounds(160, 540, 200, 20);
|
||||
up.setBounds(900, 480, 40, 40);
|
||||
down.setBounds(900, 520, 40, 40);
|
||||
left.setBounds(860, 520, 40, 40);
|
||||
right.setBounds(940, 520, 40, 40);
|
||||
comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20);
|
||||
buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20);
|
||||
w.add(canv);
|
||||
w.add(buttonCreateRoadTrain);
|
||||
w.add(buttonCreateRoadTrainWithTank);
|
||||
w.add(up);
|
||||
w.add(down);
|
||||
w.add(left);
|
||||
w.add(right);
|
||||
w.add(comboBoxStrategy);
|
||||
w.add(buttonStrategysStep);
|
||||
w.setVisible (true);
|
||||
}
|
||||
}
|
||||
|
||||
class Canvas extends JComponent{
|
||||
public DrawingRoadTrain _drawingRoadTrain;
|
||||
public Canvas(){
|
||||
}
|
||||
public void paintComponent (Graphics g){
|
||||
if (_drawingRoadTrain == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
_drawingRoadTrain.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
14
src/IMoveableObject.java
Normal file
14
src/IMoveableObject.java
Normal file
@ -0,0 +1,14 @@
|
||||
package src;
|
||||
|
||||
public interface IMoveableObject {
|
||||
|
||||
ObjectParameters GetObjectPosition();
|
||||
|
||||
int GetStep();
|
||||
|
||||
boolean CheckCanMove(Direction direction);
|
||||
|
||||
void MoveObject(Direction direction);
|
||||
|
||||
}
|
||||
|
13
src/IWheelDrawing.java
Normal file
13
src/IWheelDrawing.java
Normal file
@ -0,0 +1,13 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IWheelDrawing {
|
||||
|
||||
public NumWheel getNumWheel();
|
||||
|
||||
public void setNumWheel(int kWheel);
|
||||
|
||||
public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g2d);
|
||||
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package src;
|
||||
|
||||
public class Main
|
||||
{
|
||||
public static void main(String[] args) { new FormRoadTrain(); }
|
51
src/MoveToBorder.java
Normal file
51
src/MoveToBorder.java
Normal file
@ -0,0 +1,51 @@
|
||||
package src;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
|
||||
protected boolean IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder() <= FieldWidth &&
|
||||
objParams.RightBorder() + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder() <= FieldHeight &&
|
||||
objParams.DownBorder() + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder() - FieldWidth;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder() - FieldHeight;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
51
src/MoveToCenter.java
Normal file
51
src/MoveToCenter.java
Normal file
@ -0,0 +1,51 @@
|
||||
package src;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy {
|
||||
|
||||
protected boolean IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return
|
||||
Math.abs(objParams.ObjectMiddleHorizontal() - FieldWidth / 2) <= GetStep()
|
||||
&&
|
||||
Math.abs(objParams.ObjectMiddleVertical() - FieldHeight / 2) <= GetStep();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
package src;
|
||||
|
||||
public enum NumWheel
|
||||
{
|
||||
oneWheel,
|
25
src/ObjectParameters.java
Normal file
25
src/ObjectParameters.java
Normal file
@ -0,0 +1,25 @@
|
||||
package src;
|
||||
|
||||
public class ObjectParameters {
|
||||
|
||||
private int _x;
|
||||
private int _y;
|
||||
private int _width;
|
||||
private int _height;
|
||||
public int RightBorder () {return _x + _width;}
|
||||
|
||||
public int DownBorder () {return _y + _height;}
|
||||
|
||||
public int ObjectMiddleHorizontal () {return _x + _width / 2;}
|
||||
|
||||
public int ObjectMiddleVertical () {return _y + _height / 2;}
|
||||
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
||||
|
8
src/Status.java
Normal file
8
src/Status.java
Normal file
@ -0,0 +1,8 @@
|
||||
package src;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class WheelDrawing {
|
||||
public class WheelDrawing implements IWheelDrawing{
|
||||
private NumWheel numWheel;
|
||||
public NumWheel getSomeProperty() {
|
||||
public NumWheel getNumWheel() {
|
||||
return numWheel;
|
||||
}
|
||||
public void setNumWheel(int kWheel){
|
||||
@ -22,20 +24,29 @@ public class WheelDrawing {
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){
|
||||
public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){
|
||||
g.setColor(color);
|
||||
switch (numWheel) {
|
||||
case oneWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawOval(_startPosX + 125, _startPosY + 75, 20, 20);
|
||||
}
|
||||
case twoWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawOval(_startPosX + 125, _startPosY + 75, 20, 20);
|
||||
|
||||
g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30);
|
||||
g.drawOval(_startPosX + 55, _startPosY + 75, 20, 20);
|
||||
}
|
||||
case threeWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawOval(_startPosX + 55, _startPosY + 70, 30, 30);
|
||||
g.drawOval(_startPosX + 125, _startPosY + 75, 20, 20);
|
||||
|
||||
g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30);
|
||||
g.drawOval(_startPosX + 55, _startPosY + 75, 20, 20);
|
||||
|
||||
g.drawOval(_startPosX + 15, _startPosY + 70, 30, 30);
|
||||
g.drawOval(_startPosX + 20, _startPosY + 75, 20, 20);
|
||||
}
|
||||
};
|
||||
}
|
57
src/WheelDrawingRect.java
Normal file
57
src/WheelDrawingRect.java
Normal file
@ -0,0 +1,57 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class WheelDrawingRect implements IWheelDrawing{
|
||||
|
||||
private NumWheel numWheel;
|
||||
|
||||
public NumWheel getNumWheel() {
|
||||
return numWheel;
|
||||
}
|
||||
|
||||
public void setNumWheel(int kWheel){
|
||||
switch(kWheel){
|
||||
case 1:
|
||||
numWheel = NumWheel.oneWheel;
|
||||
break;
|
||||
case 2:
|
||||
numWheel = NumWheel.twoWheel;
|
||||
break;
|
||||
case 3:
|
||||
numWheel = NumWheel.threeWheel;
|
||||
break;
|
||||
default:
|
||||
numWheel = NumWheel.oneWheel;
|
||||
System.out.println("Ошибка! Количество " + kWheel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){
|
||||
g.setColor(color);
|
||||
switch (numWheel) {
|
||||
case oneWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawRect(_startPosX + 125, _startPosY + 75, 20, 20);
|
||||
}
|
||||
case twoWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawRect(_startPosX + 125, _startPosY + 75, 20, 20);
|
||||
|
||||
g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30);
|
||||
g.drawRect(_startPosX + 55, _startPosY + 75, 20, 20);
|
||||
}
|
||||
case threeWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawRect(_startPosX + 125, _startPosY + 75, 20, 20);
|
||||
|
||||
g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30);
|
||||
g.drawRect(_startPosX + 55, _startPosY + 75, 20, 20);
|
||||
|
||||
g.drawOval(_startPosX + 15, _startPosY + 70, 30, 30);
|
||||
g.drawRect(_startPosX + 20, _startPosY + 75, 20, 20);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
72
src/WheelDrawingTringle.java
Normal file
72
src/WheelDrawingTringle.java
Normal file
@ -0,0 +1,72 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class WheelDrawingTringle implements IWheelDrawing{
|
||||
private NumWheel numWheel;
|
||||
public NumWheel getNumWheel() {
|
||||
return numWheel;
|
||||
}
|
||||
public void setNumWheel(int kWheel){
|
||||
switch(kWheel){
|
||||
case 1:
|
||||
numWheel = NumWheel.oneWheel;
|
||||
break;
|
||||
case 2:
|
||||
numWheel = NumWheel.twoWheel;
|
||||
break;
|
||||
case 3:
|
||||
numWheel = NumWheel.threeWheel;
|
||||
break;
|
||||
default:
|
||||
numWheel = NumWheel.oneWheel;
|
||||
System.out.println("Ошибка! Количество " + kWheel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){
|
||||
g.setColor(color);
|
||||
switch (numWheel) {
|
||||
case oneWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawPolygon(
|
||||
new int[]{ _startPosX + 135, _startPosX + 125, _startPosX + 145 },
|
||||
new int[]{ _startPosY + 70, _startPosY + 95, _startPosY + 95 },
|
||||
3);
|
||||
}
|
||||
case twoWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawPolygon(
|
||||
new int[]{ _startPosX + 135, _startPosX + 125, _startPosX + 145 },
|
||||
new int[]{ _startPosY + 70, _startPosY + 95, _startPosY + 95 },
|
||||
3);
|
||||
|
||||
g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30);
|
||||
g.drawPolygon(
|
||||
new int[]{_startPosX + 65, _startPosX + 55, _startPosX + 75},
|
||||
new int[]{_startPosY + 70, _startPosY + 95, _startPosY + 95},
|
||||
3
|
||||
);
|
||||
}
|
||||
case threeWheel -> {
|
||||
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
|
||||
g.drawPolygon(
|
||||
new int[]{ _startPosX + 135, _startPosX + 125, _startPosX + 145 },
|
||||
new int[]{ _startPosY + 70, _startPosY + 95, _startPosY + 95 },
|
||||
3);
|
||||
|
||||
g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30);
|
||||
g.drawPolygon(
|
||||
new int[]{_startPosX + 65, _startPosX + 55, _startPosX + 75},
|
||||
new int[]{_startPosY + 70, _startPosY + 95, _startPosY + 95},
|
||||
3);
|
||||
|
||||
g.drawOval(_startPosX + 15, _startPosY + 70, 30, 30);
|
||||
g.drawPolygon(
|
||||
new int[]{_startPosX + 30, _startPosX + 20, _startPosX + 40},
|
||||
new int[]{_startPosY + 70, _startPosY + 95, _startPosY + 95},
|
||||
3);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user