работа 2

This commit is contained in:
bekodeg 2023-11-16 17:45:11 +04:00
parent bf7d70d980
commit 0d75f13101
4 changed files with 60 additions and 14 deletions

View File

@ -114,7 +114,7 @@ public class DrawningLocomotive {
}
switch (direction) {
case Down -> {
return _startPosX + _locomotiveHeight + entityLocomotive.Step < _pictureHeight;
return _startPosY + _locomotiveHeight + entityLocomotive.Step < _pictureHeight;
}
case Up -> {
return _startPosY - entityLocomotive.Step > 0;

View File

@ -11,6 +11,7 @@ import java.util.Random;
public class FormElectricLocomotive extends JFrame {
private DrawningLocomotive _drawningLocomotive;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
private JButton buttonCreateElectricLocomotive;
private JButton buttonCreateLocomotive;
@ -18,8 +19,9 @@ public class FormElectricLocomotive extends JFrame {
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonStep;
private JTextField numberField;
private JComboBox<String> comboBoxStrategy;
private JComboBox<AbstractStrategy> comboBoxStrategy;
public void Draw() {
if (_drawningLocomotive == null)
{
@ -34,9 +36,13 @@ public class FormElectricLocomotive extends JFrame {
buttonCreateLocomotive = new JButton("Создать локомотив");
buttonCreateLocomotive.setMargin(new Insets(0, 0, 0, 0));
buttonStep = new JButton("шаг");
buttonStep.setName("шаг");
comboBoxStrategy = new JComboBox<>();
comboBoxStrategy.addItem("идти к центру экрана");
comboBoxStrategy.addItem("идти к краю экрана");
comboBoxStrategy.addItem(new MoveToCenter());
comboBoxStrategy.addItem(new MoveToBorder());
buttonUp = new JButton();
buttonUp.setBorderPainted(false);
@ -77,7 +83,7 @@ public class FormElectricLocomotive extends JFrame {
setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, 960, 560);
canvas.setBounds(0, 0, 980, 560);
buttonCreateElectricLocomotive.setBounds(10, 470, 200, 40);
buttonCreateLocomotive.setBounds(10, 520, 200, 40);
buttonUp.setBounds(50, 380, 40, 40);
@ -85,6 +91,7 @@ public class FormElectricLocomotive extends JFrame {
buttonRight.setBounds(90, 420, 40, 40);
buttonLeft.setBounds(10, 420, 40, 40);
numberField.setBounds(220, 520, 40, 40);
buttonStep.setBounds(770, 50, 200, 30);
comboBoxStrategy.setBounds(770, 10, 200, 30);
@ -95,18 +102,18 @@ public class FormElectricLocomotive extends JFrame {
add(buttonRight);
add(buttonLeft);
add(numberField);
add(buttonStep);
add(comboBoxStrategy);
add(canvas);
}
private void InitializeLogic(){
buttonCreateElectricLocomotive.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
public void actionPerformed(ActionEvent e) {
int countWheels;
try {
countWheels = Integer.parseInt(numberField.getText());
}
catch (Exception ex){
} catch (Exception ex) {
countWheels = 0;
}
System.out.println(e.getActionCommand());
@ -121,7 +128,8 @@ public class FormElectricLocomotive extends JFrame {
random.nextInt(0, 256)),
countWheels,
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
1000, 560);
canvas.getWidth(), canvas.getHeight()
);
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
}
@ -152,6 +160,35 @@ public class FormElectricLocomotive extends JFrame {
}
}
);
buttonStep.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(((JButton)(e.getSource())).getName());
if (_drawningLocomotive == null){
return;
}
if (comboBoxStrategy.isEnabled()){
_abstractStrategy = (AbstractStrategy) comboBoxStrategy.getSelectedItem();
if (_abstractStrategy == null){
return;
}
_abstractStrategy.SetData(new DrawningObjectLocomotive(_drawningLocomotive),
canvas.getWidth(), canvas.getHeight());
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){

View File

@ -19,9 +19,13 @@ public class MoveToBorder extends AbstractStrategy{
if (objParams == null){
return false;
}
return objParams.ObjectMiddleHorizontal() <= FieldHeight() &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() &&
objParams.ObjectMiddleVertical() <= FieldHeight() &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight();
return objParams.RightBorder() <= FieldWidth() &&
objParams.RightBorder() + GetStep() >= FieldWidth() &&
objParams.DownBorder() <= FieldHeight() &&
objParams.DownBorder() + GetStep() >= FieldHeight();
}
public String toString(){
return "идти к краю экрана";
}
}

View File

@ -1,6 +1,7 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
@ -40,9 +41,13 @@ public class MoveToCenter extends AbstractStrategy{
if (objParams == null){
return false;
}
return objParams.ObjectMiddleHorizontal() <= FieldHeight() / 2 &&
return objParams.ObjectMiddleHorizontal() <= FieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() / 2 &&
objParams.ObjectMiddleVertical() <= FieldHeight() / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight() / 2;
}
public String toString(){
return "идти к центру экрана";
}
}