Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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:

Java
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

}
Posted
Updated 4-Jun-12 23:10pm
v2
Comments
Member 15461547 9-Dec-21 10:27am    
do you perhaps mind posting the proper solution
Member 15461547 9-Dec-21 10:27am    
or let me rather say the fully functioning solution
Member 15461547 9-Dec-21 10:27am    
in need of the solution urgently

1 solution

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:

Java
public class GridPanel extends JPanel(){

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

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

Java
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
}


EDIT:
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[^]
 
Share this answer
 
v3
Comments
Member 15461547 9-Dec-21 10:27am    
i tried this solution and it doesnt seem to be working i have a similar type of problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900