Initial commit

This commit is contained in:
MayDayR 2023-12-15 20:06:51 +03:00
parent d51aa123df
commit 05308d07cb
2 changed files with 150 additions and 58 deletions

View File

@ -6,11 +6,7 @@ public class EntityBus {
private int Speed; private int Speed;
private float Weight; private float Weight;
private Color BodyColor; private Color BodyColor;
private Color AdditionalColor; public float Step;
private boolean IsSecondFloor;
private boolean IsStairs;
public int Step;
public int getSpeed() { public int getSpeed() {
return Speed; return Speed;
@ -21,26 +17,13 @@ public class EntityBus {
public Color getBodyColor() { public Color getBodyColor() {
return BodyColor; return BodyColor;
} }
public Color getAdditionalColor() {
return AdditionalColor;
}
public boolean IsSecondFloor() {
return IsSecondFloor;
}
public boolean IsStairs() {
return IsStairs;
}
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs) public EntityBus(int speed, float weight, Color bodyColor)
{ {
Weight = weight; Weight = weight;
Speed = speed; Speed = speed;
BodyColor = bodyColor; BodyColor = bodyColor;
AdditionalColor = additionalColor;
IsSecondFloor = isSecondFloor;
IsStairs = isStairs;
Step = Speed * 100 / (int) Weight; Step = Speed * 100 / (int) Weight;
} }

View File

@ -7,62 +7,97 @@ import MovementStrategy.*;
public class FormBus extends JFrame { public class FormBus extends JFrame {
private DrawingBus _drawingBus; private int width;
private Canvas canvas = new Canvas(); private int height;
private DrawningBus _drawningBus;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
JLabel labelCount = new JLabel("Введите число дверей:"); // выбор кол-ва иллюминаторов
private JTextField fieldCount = new JTextField(); JLabel labelCount;
private JButton buttonCreate; private JTextField fieldCount;
// выбор стратегии
JLabel labelStrategy;
JComboBox comboBoxStrategy;
JButton buttonStrategy;
private JButton buttonCreateBus;
private JButton buttonCreateDoubleDeckerBus;
private JButton buttonUp; private JButton buttonUp;
private JButton buttonDown; private JButton buttonDown;
private JButton buttonRight; private JButton buttonRight;
private JButton buttonLeft; private JButton buttonLeft;
public FormBus() { public FormBus() {
super("Создание автобуса"); super("Создание бензовоза");
InitializeComponent(); InitializeComponent();
setVisible(true); setVisible(true);
} }
private void InitializeComponent() private void InitializeComponent()
{ {
buttonCreate = new JButton("Создать автобус"); canvas = new Canvas();
labelCount = new JLabel("Введите число дверей:");
fieldCount = new JTextField();
labelStrategy = new JLabel("Шаг стратегии:");
comboBoxStrategy = new JComboBox(new Integer[] {0, 1});
buttonStrategy = new JButton("Выбрать стратегию");
buttonStrategy.setMargin(new Insets(0, 0, 0, 0));
buttonCreateBus = new JButton("Создать автобус");
buttonCreateBus.setMargin(new Insets(0, 0, 0, 0));
buttonCreateDoubleDeckerBus = new JButton("Создать двухэтажный автобус");
buttonCreateDoubleDeckerBus.setMargin(new Insets(0, 0, 0, 0));
buttonUp = new JButton(); buttonUp = new JButton();
buttonUp.setName("up"); buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon("images/KeyUp.png")); buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
buttonUp.setSize(48, 44);
buttonRight = new JButton(); buttonRight = new JButton();
buttonRight.setName("right"); buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon("images/KeyRight.png")); buttonRight.setIcon(new ImageIcon("images\\KeyRight.png"));
buttonRight.setSize(48, 44);
buttonLeft = new JButton(); buttonLeft = new JButton();
buttonLeft.setName("left"); buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon("images/KeyLeft.png")); buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png"));
buttonLeft.setSize(48, 44);
buttonDown = new JButton(); buttonDown = new JButton();
buttonDown.setName("down"); buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon("images/KeyDown.png")); buttonDown.setIcon(new ImageIcon("images\\KeyDown.png"));
buttonDown.setSize(48, 44);
setSize(800,500); setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null); setLayout(null);
buttonCreate.setBounds(12, 375, 170, 44); buttonCreateBus.setBounds(12, 375, 146, 33);
buttonCreateDoubleDeckerBus.setBounds(182, 375, 146, 33);
labelCount.setBounds(42, 405, 240, 20);
fieldCount.setBounds(240, 407, 48, 20);
labelStrategy.setBounds(630, 20, 146, 33);
comboBoxStrategy.setBounds(630, 50, 146, 20);
buttonStrategy.setBounds(630, 80, 146, 33);
buttonUp.setBounds(679, 363, 48, 44); buttonUp.setBounds(679, 363, 48, 44);
buttonRight.setBounds( 728, 408, 48, 44); buttonRight.setBounds( 728, 408, 48, 44);
buttonLeft.setBounds(630, 408, 48, 44); buttonLeft.setBounds(630, 408, 48, 44);
buttonDown.setBounds( 679, 408, 48, 44); buttonDown.setBounds( 679, 408, 48, 44);
labelCount.setBounds(12, 435, 240, 20); labelCount.setBounds(12, 435, 240, 20);
fieldCount.setBounds(160, 437, 48, 20); fieldCount.setBounds(150, 437, 48, 20);
canvas.setBounds(0,0,800, 460); canvas.setBounds(0,0,800, 460);
add(buttonCreate); add(buttonCreateBus);
add(buttonCreateDoubleDeckerBus);
add(labelCount);
add(fieldCount);
add(labelStrategy);
add(comboBoxStrategy);
add(buttonStrategy);
add(buttonUp); add(buttonUp);
add(buttonRight); add(buttonRight);
add(buttonDown); add(buttonDown);
@ -71,14 +106,48 @@ public class FormBus extends JFrame {
add(fieldCount); add(fieldCount);
add(canvas); add(canvas);
buttonCreate.addActionListener(buttonCreateListener); // логика формы
buttonCreateBus.addActionListener(buttonCreateBusListener);
buttonCreateDoubleDeckerBus.addActionListener(buttonCreateDoubleDeckerBusListener);
buttonStrategy.addActionListener(buttonStrategyListener);
buttonUp.addActionListener(buttonsMoveListener); buttonUp.addActionListener(buttonsMoveListener);
buttonRight.addActionListener(buttonsMoveListener); buttonRight.addActionListener(buttonsMoveListener);
buttonDown.addActionListener(buttonsMoveListener); buttonDown.addActionListener(buttonsMoveListener);
buttonLeft.addActionListener(buttonsMoveListener); buttonLeft.addActionListener(buttonsMoveListener);
} }
ActionListener buttonCreateListener = new ActionListener() { ActionListener buttonCreateBusListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int countDoors;
try
{
countDoors = Integer.parseInt(fieldCount.getText());
}
catch (Exception ex)
{
countDoors = 0;
}
if (countDoors != 2 && countDoors != 3 && countDoors != 4)
{
JOptionPane.showMessageDialog(null, "Число должно быть равно 3, 4 или 5.\nКол-во дверей приравнено к 3");
countDoors = 2;
}
Random rand = new Random();
_drawningBus = new DrawningBus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
countDoors,
canvas.getWidth(), canvas.getHeight());
_drawningBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonCreateDoubleDeckerBusListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int countDoors; int countDoors;
try try
@ -96,15 +165,56 @@ public class FormBus extends JFrame {
} }
Random rand = new Random(); Random rand = new Random();
_drawingBus = new DrawingBus(); _drawningBus = new DrawningDoubleDeckerBus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
_drawingBus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
countDoors,
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),
countDoors,
canvas.getWidth(), canvas.getHeight()); canvas.getWidth(), canvas.getHeight());
_drawingBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); _drawningBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonStrategyListener = new ActionListener() {
@Override
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), canvas.getWidth(), canvas.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
canvas.repaint(); canvas.repaint();
} }
}; };
@ -112,23 +222,23 @@ public class FormBus extends JFrame {
ActionListener buttonsMoveListener = new ActionListener() { ActionListener buttonsMoveListener = new ActionListener() {
// реакция на нажатие // реакция на нажатие
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_drawingBus == null) if (_drawningBus == null)
{ {
return; return;
} }
String command = ((JButton)(e.getSource())).getName(); String command = ((JButton)(e.getSource())).getName();
switch (command) { switch (command) {
case "up": case "up":
_drawingBus.MoveTransport(Direction.Up); _drawningBus.MoveTransport(Direction.Up);
break; break;
case "down": case "down":
_drawingBus.MoveTransport(Direction.Down); _drawningBus.MoveTransport(Direction.Down);
break; break;
case "right": case "right":
_drawingBus.MoveTransport(Direction.Right); _drawningBus.MoveTransport(Direction.Right);
break; break;
case "left": case "left":
_drawingBus.MoveTransport(Direction.Left); _drawningBus.MoveTransport(Direction.Left);
break; break;
} }
canvas.repaint(); canvas.repaint();
@ -136,15 +246,14 @@ public class FormBus extends JFrame {
}; };
class Canvas extends JComponent{ class Canvas extends JComponent{
public Canvas(){ public Canvas() {}
}
public void paintComponent (Graphics g){ public void paintComponent (Graphics g){
if (_drawingBus == null){ if (_drawningBus == null){
return; return;
} }
super.paintComponents (g); super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
_drawingBus.DrawTransport(g2d); _drawningBus.DrawTransport(g2d);
super.repaint(); super.repaint();
} }
} }