codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  DIRECTION  nicegyal at 02:37 on Wednesday, April 26, 2006
 

i need to thank both crwood and WhiteWizard for being kind enough to help me....

i'm still trying to finish the GUI application that i've started
http://www.codetoad.com/forum/17_25899.asp <==== this is the link for the original codes....but now i'm struggling with something new in the same program.

Ok I know that i'm as for alot....but reallllyy i need help and what is needed to be coded first is the rotate button then the forward 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.
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
start = tbtPath[9][0];


But I don't know how to organize that in the code..

this is a link of the program pic ====>http://server2.uploadit.org/files/inspiration-pr2.JPG

this the code now:
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);
}
}
}

}



  Re: DIRECTION  nicegyal at 20:53 on Wednesday, April 26, 2006
 

i want to include the suggestion below into the code above but don't know how:

Ok this what I thought could be done…..but I don’t know how to organize it in my code: so help plz

If the starting square is (9,0). the default direction is set to North.
so we can store the direction in an int variable, which looks like this:

public static final int NORTH = 1;
public static final int NORTH EAST = 2;
public static final int EAST = 3;
public static final int SOUTH EAST = 4;
public static final int SOUTH = 5;
public static final int SOUTH WEST = 6;
public static final int WEST = 7;
public static final int NORTH WEST = 8;
private int direction = NORTH;


the actual position could be stored in the following way:

private int positionX = 0;
private int positionY = 0;

pointing at square (9,0)
^^^^I’m not sure if that is right or not!!!!!!!

also if we can 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)

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


switch(direction){
case NORTH:
positionX = positionX-1;
// check if reached bound. if so, dont move
if (positionX<0) positionX=0;
break;
case NORTH EAST:
positionX = positionX-1;
positionY = positionY+1;
/// it is hard to check the bound for the north east
break;
EAST
positionY = positionY+1;
if (positionY>maxY) positionY=maxY;
break;
SOUTH EAST
positionX = positionX+1;
positionY = positionY+1;
/// it is hard to check the bound for the south east
break;
case SOUTH:
positionX = positionX+1;
if (positionX>maxX) positionX=maxX;
break;
case SOUTH WEST:
positionX = positionX+1;
positionY = positionY-1;
/// it is hard to check the bound for the south west
break;
case WEST:
positionY = positionY-1;
if (positionY<0) positionY=0;
break;
case NORTH WEST:
positionX = positionX-1;
positionY = positionY-1;
/// it is hard to check the bound for the north west
break;
}

so, when hitting forward, it will determine the next square and then repaint the view.
by using JButtons to display the fields, then the following sequence, could be used:
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)

I don't have to explain how NW, SE etc. will function.

And about the next feature which is the rotate button. when pressing this button the use of a switch statement could be used (rotating clockwise):

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



  Re: DIRECTION  crwood at 00:45 on Thursday, April 27, 2006
 

Some initial observations about your program (top post):
1 — In the main method you have

RobotPath frame = new RobotPath();

The name of the enclosing class is "RP". The main method will instantiate and load a different class into the gui than "RP".
2 — it is generally considered good practice to hide as much as possible, ie, to expose as instance variables (declarations in class scope) only the minimum required and to hide the rest. This keeps things simple, avoids unnecessary clutter, makes your code easier to read and potentially more self–contained (encapsulation). So, for instance, the JPanels "buttonPanel", "rootPathPanel" and "directionPanel" and the JMenus "fileMenu" and "helpMenu" do not need to be declared in class scope as instance variables. They can be declared inside the class constructor as local variables. Of course, if your teacher wants you to declare everything as an instance variable then do so by all means and ignore this suggestion.
3 — once you're happy with your gui code and begin writing the event code you won't need to listen for tbtPath JButton events anymore so you can eliminate the ButtonListener class.

about the second post above:
we can store the direction in an int variable
Make these instance variables:

public class RP extends JFrame implements ActionListener {
JMenuItem exitItem, aboutItem, istructionsItem;
...
public static final int NORTH = 1;
...

Declaring them "static" can be helpful if you want to refer to them from outside the class.

the actual position could be stored in the following way:

private int positionX = 0;
private int positionY = 0;

pointing at square (9,0)
If you are going to use positionX for the "col" and positionY as the "row" to access JButtons in the tbtPath array then they would point to the upper left button in the array.
Changing positionY to "9" would point to square(9,0).

if we can have some variables to determine the bounds
You could do this. You could also use the tbtPath array for this. The "col/x" would have to be zero or greater and "tbtPath[0].length-1" or less. The "row/y" would have to be zero or greater and less than or equal to "tbtPath.length-1".

when the user presses forward, you have a switch statement , which helps you to determine the next square
I recommend something like this:

int col = 0;
int row = 0;
switch(direction)
{
case N:
row = -1;
break;
case NE:
col = 1;
row = -1;
break;
...

returning the "row" and "column" increments for the next move according to the "direction". Then, in the method that is coordinating/orchestrating the next move (called from your event code) check that the new position to be created by adding these increments to the current position is within bounds (a single check vs. repeated checking in the switch statement). If so make the move; if not do nothing.

And about the next feature which is the rotate button. when pressing this button the use of a switch statement could be used (rotating clockwise):
If you do not keep the direction in an instance variable you can make a method that you can call from the buttons (tbtRotate) event listener that will read jtfDirection. So, with the int direction value you could increment it like this

direction++;
if(direction > NW)
direction = N;

and avoid the extra switch statement.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: C++ Beginner question
•  Re: function within loop problem
•  moncler outlet
•  Re: Display swf file in pdf
•  Re: how to create forum using asp.net with c# language?
•  Discount coach store
•  Create Better Maps with Global Mapper
•  Re: How to display a message box when record is temporary stored in session/grid view
•  Sharing object in perl


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2012