使用命令将箭头从网格中的一个单元移动到另一个单元 [英] moving an arrow from cell to cell inside a grid using a command

查看:52
本文介绍了使用命令将箭头从网格中的一个单元移动到另一个单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我已经在网格中创建了一个箭头,并将其位置设置为网格中的特定单元格,现在的问题是,我无法移动它.我需要将其向左,向右和向前移动,具体取决于单元格是否被着色.
运动控制语言由三个命令组成:Ln,Rn和Fn. Ln表示将箭头向左(逆时针)旋转n个位置,其中n是整数(整数).
Rn表示将箭头右(顺时针)旋转n个位置,其中n是整数(整数).
Fn表示将箭头向前移动n个位置,其中n是整数(整数).
示例命令可能是R2F3L2F1R2F4.
我已经包含了到目前为止的代码,但不知道使箭头移动的代码,请帮助:

Hi guys

I''ve gave created an arrow in a grid and set its position to a specific cell in my grid, now the problem is, I cannot move it. I need to move it to the left, right and forward depending if the cell is shaded or not.
The movement control language is made up of three commands: Ln, Rn and Fn.,
Ln means turn the arrow left (anticlockwise) through n positions, where n is an integer (whole number).
Rn means turn the arrow right (clockwise) through n positions, where n is an integer (whole number).
Fn means move the arrow forward through n positions, where n is an integer (whole number).
A sample command might be R2F3L2F1R2F4.
i have included the code i have so far but don''t know the code to make the arrow to move, please help:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class Grid extends JFrame {

  
    GridLayout grid = new GridLayout(5, 5, 5, 5);

    static JTextField[][] textFields = new JTextField[5][5]; // Array of Object
    JTextField aTxtField = new JTextField();
    JPanel gridPanel = new JPanel();
    JPanel inputPanel = new JPanel();
    int size; // size of board (i.e. number of rows and columns )


    public Grid() {
        initializeUI();
    }

    private void initializeUI() {
        setTitle("Grid Arrow Assignment");
        setSize(300, 370);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);


        this.size = textFields.length;


        for (int row = 0; row < textFields.length; row++) {
            int randomNum = 0;
            Random rand = new Random();
            randomNum = rand.nextInt(size);

            for (int col = 0; col < textFields[row].length; col++) {
                textFields[row][col] = new JTextField(); // reflects the field coordinates
                if (row == 0 && col == 0) {
                    textFields[row][col].setBackground(Color.PINK);
                }

                if (row == (textFields.length - 1) && col == (textFields.length - 1)) {
                    textFields[row][col].setBackground(Color.YELLOW);
                    textFields[row][col].setText("\u2191");

                }

                if (row == 0 && randomNum == 0) {
                    randomNum++;
                }
                if (row == (textFields.length - 1) && randomNum == (textFields.length - 1)) {
                    randomNum--;
                }

                if (col == randomNum) {
                    textFields[row][col].setBackground(Color.BLACK);
                }

               
                textFields[row][col].setHorizontalAlignment(SwingConstants.CENTER);

                gridPanel.add(textFields[row][col]);
              
            }
        }

     
        JLabel aLabel = new JLabel("Type a command:");
        aTxtField.setPreferredSize(new Dimension(150, 25));
        JButton aButton = new JButton("Go");

        inputPanel.setLayout(new GridLayout(1, 1, 1, 1));
        inputPanel.setPreferredSize(new Dimension(300, 35));
        inputPanel.add(aLabel);
        inputPanel.add(aTxtField);
        inputPanel.add(aButton);
        aButton.addActionListener(aButtonListener);
        inputPanel.setLayout(new FlowLayout());
        // inputPanel.setBackground(Color.RED);

        gridPanel.setLayout(grid);
        gridPanel.setPreferredSize(new Dimension(300, 300));
        add(gridPanel, BorderLayout.NORTH);
        add(new JSeparator(), BorderLayout.CENTER);
        add(inputPanel, BorderLayout.SOUTH);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Grid();
            }
        });

    }
    ActionListener aButtonListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String userText = aTxtField.getText();

        }
    };// END

}

推荐答案

GridLayout.这意味着您必须使箭头"的位置动态(在创建JPanel时作为参数提供),并且必须重新绘制JPanel (JPanel.doLayout()).

因此,您需要重新设计您的方法:

-在GridLayoutJFrame之间需要一个可以重绘的JPanel .因此,将JPanel添加到JFrame中,然后将其余的内容放入JPanel中.

-将此JPanel设置为自己的Object:

that is a GridLayout. That would mean that you have to make the position of the "arrow" dynamic (given as arguments when the JPanel is created) and you would have to draw the JPanel newly (JPanel.doLayout()).

So you need to redesign your approach:

- Between GridLayout and JFrame needs to be a JPanel that can be redrawn. So add a JPanel to your JFrame and put the rest into the JPanel.

- make this JPanel an own Object:

public class GridPanel extends JPanel(){

public GridPanel(int iRow, int iCol){
  super();
  this.ignition(iRow, iCol); // for current position
}


那么它就更容易使用了,因为您只需使用一个小功能就可以更新JPanel:


it''s easier to work with it then because you can renew the JPanel simply with a little function:

public void setGridPanel(int iRow, int iCol) {
  remove(oGridPanel); // remove old grid
  repaint(); // get change in GUI (not really needed)
  oGridPanel = new GridPanel(iRow, iCol); // create new grid with new positions
  getContentPane().add(oGridPanel); // add LayoutConstrains if needed
  oGridPanel.doLayout(); // force gridPanel to layout newly
}




创建按钮时,应给按钮一个ActionCommand.我可以为此建议使用Enum ,这是创建一组可以引用的固定命令的最明智的方法之一.
有关如何使用枚举的更多信息,请参见Nagy的文章:




You should give your Buttons a ActionCommand while creating them. I can recommend to use a Enum for that, that''s one of the smartest way to create a fixed set of commands that one can refer to.
See Nagy''s Article for more about how to use enums: The Secret Classes or How I Stopped Counting and Loved the Enumerator[^]


这篇关于使用命令将箭头从网格中的一个单元移动到另一个单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆