if u think u can


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: if u think u can

  1. #1
    Join Date
    Apr 2006
    Posts
    5

    please your help is so much needed

    I consider myself a beginner in programming so I need a little help if that is possible…so if you think that you can some how help me…plz do…




    The other problems that I’m facing now are:

    the GUI application is designed to l emulate a movement of the robot in 10x10 grids of buttons. as soon as you run the application certain buttons should be coloured in black to form an ‘M’ shape.

    the problem i'm facing now is:

    The FORWARD button wich sould allow the user to colour the buttons to black and moving one square forwarding any direction decided by the ROTATE button.

    and the ROTATE button should help the user in choosing the direction by rotating to N, NE, E, SE, S, SW, W, and NW.


    ANY HELP IS APPRECIATED

    Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JButton;
    
    public class RP extends JFrame 
    implements ActionListener {
    	
    		JPanel buttonPanel, robotPathPanel, directionPanel;
    		JMenuBar robotMenuBar;
        	        JMenu fileMenu, helpMenu;
        	        JMenuItem exitItem, aboutItem, istructionsItem;
    		JButton tbtForward, tbtRotate, tbtClear, tbtExit;
    	//	JButton[][] tbtPath;    
    		int row = 10, col = 10;
    		JButton[][] tbtPath  = new JButton[row][col];
    		int x = 0, y = 0;
    		JTextField jtfSquare = new JTextField();
    		JTextField jtfDirection = new JTextField();
    		
            		
    	public static void main (String[] args) {
    		RobotPath frame = new RobotPath();
    		frame.setTitle("Robot Path Emulator - Java Application"); //frame title
    		frame.setBounds(250, 90, 	// setting the position
    						750, 600);	//setting the size
    		frame.createGUI();
    		frame.setResizable(false);
    		frame.setVisible(true);     //displaying the window
    	
    	}
    	
    	
    	private void createGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container window = getContentPane();
    		window.setLayout(new BorderLayout() ); // Set border layout manager
        	
        	//set the menu bar
        	robotMenuBar = new JMenuBar();
            setJMenuBar(robotMenuBar);
            
            // file menu, with  Exit
            fileMenu = new JMenu("File");
    
            exitItem = new JMenuItem("Exit");
            fileMenu.add(exitItem);
            exitItem.addActionListener(this);
            
            robotMenuBar.add(fileMenu);
        	
        	// help menu, with  about, instructions
            helpMenu = new JMenu("Help");
    
            aboutItem = new JMenuItem("About");
            helpMenu.add(aboutItem);
            aboutItem.addActionListener(this);
    
            istructionsItem = new JMenuItem("Istructions");
            helpMenu.add(istructionsItem);
            istructionsItem.addActionListener(this);
    
            robotMenuBar.add(helpMenu );
        	
    		//JPanel for the buttons
    		//buttonPanel = new JPanel();
    		//buttonPanel.setLayout(new GridLayout(20, 1));
    		buttonPanel = new JPanel();
    		buttonPanel.setLayout(new GridBagLayout());
    		GridBagConstraints gbc = new GridBagConstraints();
    		gbc.gridwidth = gbc.REMAINDER;
    		gbc.fill = gbc.HORIZONTAL;
    		
    		tbtForward = new JButton("Forward");	//Creating buttons: Forward, 
            buttonPanel.add(tbtForward, gbc);			//Rotate, Clear, Exit
            tbtForward.addActionListener(this);
            
            tbtRotate = new JButton("Rotate");
            buttonPanel.add(tbtRotate, gbc);
            tbtRotate.addActionListener(this);
            
            tbtClear = new JButton("Clear");
           	//ButtonListener buttonListener = new ButtonListener();
            buttonPanel.add(tbtClear, gbc);
            tbtClear.addActionListener(this);
            
            tbtExit = new JButton("Exit");
            buttonPanel.add(tbtExit, gbc);
            tbtExit.addActionListener(this);
            
            gbc.weighty = 1.0;
    		gbc.anchor = GridBagConstraints.NORTH;
    		buttonPanel.add(new JLabel(), gbc);
    		
    		//JPanel for the Robot Path Emulator
    		//robotPathPanel = new JPanel();
    		robotPathPanel = new JPanel(new GridLayout(10,0,2,2));
    		robotPathPanel.setBackground(Color.white);
    		
    		ButtonListener buttonListener = new ButtonListener();	
    	//	JButton[][] tbtPath = new JButton[row][col];            // Array to store buttons
        	for (int x = 0 ; x < row ; x++)
    		{
    	   		for (int y = 0 ; y < col ; y++)
    	    	{
    				tbtPath [x] [y] = new JButton ("(" + x + "," + y + ")");
    				robotPathPanel.add(tbtPath[x][y]);
    		//		tbtPath [x] [y].setBackground (Color.green);
    				tbtPath [x] [y].addActionListener (buttonListener);
    	    	}
    		}
    		
    		colorButtons(tbtPath, Color.green);
    		
    		//JPanel for the directions
    		directionPanel = new JPanel();
    		directionPanel.setLayout(new FlowLayout());
    		
    		directionPanel.add(new JLabel("Square: "));
    		directionPanel.add(jtfSquare = new JTextField(4));
    		jtfSquare.addActionListener (buttonListener);
    		
    		directionPanel.add(new JLabel("Direction: "));
    		directionPanel.add(jtfDirection = new JTextField(3));
    				
    		
    		window.add("East", buttonPanel);
    		window.add("Center", robotPathPanel);
    		window.add("South", directionPanel);
    		
    	}
    	
    	public void colorButtons( JButton array[][], Color aColor)	//a class to re-color the buttos back to green
    	{
    	   for (int x = 0 ; x < row ; x++) {
    	      for (int y = 0 ; y < col ; y++) {
    	         array [x] [y].setBackground (aColor);
    	      }
    	   }
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		 if(e.getSource() == exitItem) {
                System.exit(0);
                }
             if(e.getSource() == tbtExit) {
                System.exit(0);
                }
              if(e.getSource() == tbtClear){				//the clear button is not working
    			colorButtons(tbtPath, Color.green);		
    			}
            
    	}
    	
    	private class ButtonListener implements ActionListener {
    		Color selectedColor = Color.black;
    		
    		
    		public void actionPerformed(ActionEvent e) {
    			JButton button = (JButton)e.getSource();
    			jtfSquare.setText( button.getText() );
    			System.out.println(button.getText());
    			System.out.println(button.getBackground() != selectedColor);
    				if(button.getBackground() != selectedColor) {
    				button.setBackground(selectedColor);
    				}
    				else {
    				button.setBackground(Color.green);
    				}
    		}
    	}
    	
    }
    Last edited by nicegyal; 04-25-2006 at 08:40 PM.

  2. #2
    Join Date
    Apr 2006
    Posts
    5
    please if anyone can help!!!!

  3. #3
    Join Date
    Apr 2006
    Posts
    5
    Ok what i need help in coding first is the rotate button then the foreward button….

    What I thought could be done is whenever the rotate button is clicked the jTextField for the direction will show the 8 paths/directions the user want (N, NE, E, SE, S, SW, W, and NW) ….

    and the user can click the forward button, which will read the direction in jTextField ….and from there it will move one square forward according to the direction.

    What I need is how to set this up….


    i know this is what I need to set the path/direction….
    Code:
    for (int x = 0 ; x < row ; x++){
    for (int y = 0 ; y < col ; y++){            
    north = tbtPath[x-1][y];
    north east = tbtPath[x-1][y+1];
    east = tbtPath[x][y+1];
                south east = tbtPath[x+1][y+1];
    	south = tbtPath[x+1][y];
                south west  = tbtPath [x-1][y-1];
                west  = tbtPath[x][y-1];
    	north west  = tbtPath[x-1][y-1];
                tbtPath[x][y].setNeighbors(north, north east, east, south east, south, south west  ,west, north west);
             }
         }
    I also thought that there should be a start state
    Code:
        start  = tbtPath[9][0];
    But I don’t know how to organize that in the code…..

  4. #4
    Join Date
    Apr 2006
    Posts
    5
    is it possible to have a rotate string that increases with the direction, from that both the text field and direction are worked out. ???!!!!

  5. #5
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    let's asume you have a starting square, eg. (0,0). the default direction is set to E(ast).
    you have stored the direction in a in variable, which looks like this:
    public static final int NORTH = 1;
    public static final int EAST = 2;
    public static final int SOUTH = 3;
    public static final int WEST = 4;
    private int direction = EAST;

    the actual position is stored in the following way:
    private int positionX = 0;
    private int positionY = 0;
    pointing at square (0,0)

    also you have some variables to determine the bounds:
    private int maxX = 9;
    private int maxY = 9;
    which defines the 10x10 field with max square (9,9) (since index starting at 0)

    now, when the user presses forward, you have a switch statement , which helps you to determine the next square:

    switch(direction){
    case EAST:
    positionX = positionX+1;
    // check if reached bound. if so, dont move
    if (positionX>maxX) positionX=maxX;
    break;
    case NORTH:
    positionY = positionY-1;
    if (positionY<0) positionY=0;
    break;
    case SOUTH:
    positionY = positionY+1;
    if (positionY>maxY) positionY=maxY;
    break;
    case WEST:
    positionX = positionX-1;
    if (positionX<0) positionX=0;
    break;
    }

    so, when hitting forward, you determine the next square and then repaint your view.
    if you use JButtons to display the fields, then think of the following sequence:
    1. set old field (before moving) to inactive background (green)
    2. make the switch to determine the next square
    3. set new field to active (bg=black or whatever)

    I think, I don't have to explain how NW, SE etc. will function. just add the neccessary constants and switch conditions.

    the next featrue is the rotate. pressing this button you can also use a switch statement (assuming you always rotate clockwise):

    switch(direction){
    case EAST:
    direction=SOUTH;
    break;
    case WEST:
    direction=EAST;
    break;
    ... similar for other directions
    }

    shurely, there are more 1337 ways to do this, for ex. with a state table, like parsers do. but this way, you will see what happens and you can understand it better.

  6. #6
    Join Date
    Apr 2006
    Posts
    5
    thanks...for the help...i have implement my codes according to what you have said… but for some reason I’m can’t get it to work yet….i don’t know how to include it in the right way into the code…. :

  7. #7
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    well, i would suggest to use a object oriented aproach with design patterns, but if you're a beginner, this will be quiet difficult to you.
    for a dirty hack-approach try following:
    place all suggested variables into the main class (RP).
    the codes for moving and rotating you can place into the actionlisteners of the corresponding button.
    eg:
    JButton jbRotate = ...
    private int direction = ....

    jbRotate.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    switch(direction){
    ...
    }
    // if doing some drawing for direction, update it
    // eg directionTextfield.setText(direction); // new direction
    }
    }
    );

    same for the move button.

  8. #8
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    if you need further help, let me see your actual code. by the way: it would be easier to read if you remove any unused (and commented) code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center