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 float Weight;
private Color BodyColor;
private Color AdditionalColor;
private boolean IsSecondFloor;
private boolean IsStairs;
public int Step;
public float Step;
public int getSpeed() {
return Speed;
@ -21,27 +17,14 @@ public class EntityBus {
public Color getBodyColor() {
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;
Speed = speed;
BodyColor = bodyColor;
Weight = weight;
Speed = speed;
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 {
private DrawingBus _drawingBus;
private Canvas canvas = new Canvas();
private int width;
private int height;
private DrawningBus _drawningBus;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
JLabel labelCount = new JLabel("Введите число дверей:");
private JTextField fieldCount = new JTextField();
private JButton buttonCreate;
// выбор кол-ва иллюминаторов
JLabel labelCount;
private JTextField fieldCount;
// выбор стратегии
JLabel labelStrategy;
JComboBox comboBoxStrategy;
JButton buttonStrategy;
private JButton buttonCreateBus;
private JButton buttonCreateDoubleDeckerBus;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonLeft;
public FormBus() {
super("Создание автобуса");
super("Создание бензовоза");
InitializeComponent();
setVisible(true);
}
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.setName("up");
buttonUp.setIcon(new ImageIcon("images/KeyUp.png"));
buttonUp.setSize(48, 44);
buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
buttonRight = new JButton();
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon("images/KeyRight.png"));
buttonRight.setSize(48, 44);
buttonRight.setIcon(new ImageIcon("images\\KeyRight.png"));
buttonLeft = new JButton();
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon("images/KeyLeft.png"));
buttonLeft.setSize(48, 44);
buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png"));
buttonDown = new JButton();
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon("images/KeyDown.png"));
buttonDown.setSize(48, 44);
buttonDown.setIcon(new ImageIcon("images\\KeyDown.png"));
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
buttonRight.setBounds( 728, 408, 48, 44);
buttonLeft.setBounds(630, 408, 48, 44);
buttonDown.setBounds( 679, 408, 48, 44);
labelCount.setBounds(12, 435, 240, 20);
fieldCount.setBounds(160, 437, 48, 20);
fieldCount.setBounds(150, 437, 48, 20);
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(buttonRight);
add(buttonDown);
@ -71,14 +106,48 @@ public class FormBus extends JFrame {
add(fieldCount);
add(canvas);
buttonCreate.addActionListener(buttonCreateListener);
// логика формы
buttonCreateBus.addActionListener(buttonCreateBusListener);
buttonCreateDoubleDeckerBus.addActionListener(buttonCreateDoubleDeckerBusListener);
buttonStrategy.addActionListener(buttonStrategyListener);
buttonUp.addActionListener(buttonsMoveListener);
buttonRight.addActionListener(buttonsMoveListener);
buttonDown.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) {
int countDoors;
try
@ -96,15 +165,56 @@ public class FormBus extends JFrame {
}
Random rand = new Random();
_drawingBus = new DrawingBus();
_drawingBus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
_drawningBus = new DrawningDoubleDeckerBus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
countDoors,
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
rand.nextBoolean(), rand.nextBoolean(),
countDoors,
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();
}
};
@ -112,23 +222,23 @@ public class FormBus extends JFrame {
ActionListener buttonsMoveListener = new ActionListener() {
// реакция на нажатие
public void actionPerformed(ActionEvent e) {
if (_drawingBus == null)
if (_drawningBus == null)
{
return;
}
String command = ((JButton)(e.getSource())).getName();
switch (command) {
case "up":
_drawingBus.MoveTransport(Direction.Up);
_drawningBus.MoveTransport(Direction.Up);
break;
case "down":
_drawingBus.MoveTransport(Direction.Down);
_drawningBus.MoveTransport(Direction.Down);
break;
case "right":
_drawingBus.MoveTransport(Direction.Right);
_drawningBus.MoveTransport(Direction.Right);
break;
case "left":
_drawingBus.MoveTransport(Direction.Left);
_drawningBus.MoveTransport(Direction.Left);
break;
}
canvas.repaint();
@ -136,15 +246,14 @@ public class FormBus extends JFrame {
};
class Canvas extends JComponent{
public Canvas(){
}
public Canvas() {}
public void paintComponent (Graphics g){
if (_drawingBus == null){
if (_drawningBus == null){
return;
}
super.paintComponents (g);
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingBus.DrawTransport(g2d);
_drawningBus.DrawTransport(g2d);
super.repaint();
}
}