Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-10-2008, 07:35 PM
Member
 
Join Date: Feb 2008
Posts: 1
XxHEXSORxX is on a distinguished road
Project Help
For a project, I need to make a GUI that displays a 7x7 grid of buttons. Each of the buttons are set to the default button specifications except for one, which has a yellow background and is labeled P. This button is the button controlled by the user. The user is then suppose to be able to use the keyboard to move the user's button around the button grid. As of now, mine displays the buttons and keeps track of the player button's position in the button grid using an X and Y coordinate. When a key on the directional pad is hit, it displays which direction was hit and the new coordinates of the player. Now, here is my problem. Once I know the new player button's location, I don't know how to display the new button grid... Can anyone help me? Here's the code I have so far:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.lang.*;
import java.util.*;

public class KeyButtonMovement{
static int playerLocationX = 0;
static int playerLocationY = 0;
public static void create (){
JFrame frame = new JFrame("Button Movement");
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(7, 7));
Random generator = new Random();
playerLocationX = generator.nextInt((6));
playerLocationX++;
Random generator2 = new Random();
playerLocationY = generator.nextInt((6));
playerLocationY++;
KeyboardPanel keyboardPanel = new KeyboardPanel();
frame.add(keyboardPanel);
keyboardPanel.setFocusable(true);
playerLocationX = keyboardPanel.playerLocationX;
playerLocationY = keyboardPanel.playerLocationY;
for(int i=0; i <(7*7); i++){
int playerLocation = ((playerLocationY-1)*7)+(playerLocationX-1);
if(i==playerLocation){
JButton button = new JButton("P");
button.setBackground(Color.YELLOW);
button.setForeground(Color.BLACK);
panel1.add(button);
}else{
JButton button = new JButton();
panel1.add(button);
}
}
frame.add(panel1, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setLocation(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
create();
}
}
class KeyboardPanel extends JPanel implements KeyListener {
int playerLocationX;
int playerLocationY;
public KeyboardPanel(){
addKeyListener(this);
KeyButtonMovement k = new KeyButtonMovement();
playerLocationX = k.playerLocationX;
playerLocationY = k.playerLocationY;
}
public void keyReleased (KeyEvent e){}
public void keyTyped (KeyEvent e){}
public void keyPressed (KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_DOWN:
if ((playerLocationY +1)!=8){
playerLocationY = playerLocationY +1;
System.out.println("DOWN "+playerLocationX+" "+playerLocationY);
}
break;
case KeyEvent.VK_UP:
if ((playerLocationY -1)!=0){
playerLocationY = playerLocationY -1;
System.out.println("UP "+playerLocationX+" "+playerLocationY);
}
break;
case KeyEvent.VK_LEFT:
if ((playerLocationX -1)!=0){
playerLocationX = playerLocationX -1;
System.out.println("LEFT "+playerLocationX+" "+playerLocationY);
}
break;
case KeyEvent.VK_RIGHT:
if ((playerLocationX +1)!=8){
playerLocationX = playerLocationX +1;
System.out.println("RIGHT "+playerLocationX+" "+playerLocationY);
}
break;
//default: keyChar = e.getKeyChar();
}
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-17-2008, 01:46 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class KBM{ public static void create (){ JFrame frame = new JFrame("Button Movement"); KeyboardPanel keyboardPanel = new KeyboardPanel(); keyboardPanel.setFocusable(true); frame.add(keyboardPanel); frame.setSize(600, 600); frame.setLocation(100, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String args[]) { new KBM().create(); } } class KeyboardPanel extends JPanel implements KeyListener { int playerLocationX; int playerLocationY; Point lastCell = new Point(); JButton[][] cells = new JButton[7][7]; public KeyboardPanel(){ super(new GridLayout(7, 7)); Random generator = new Random(); playerLocationX = generator.nextInt((6)); // playerLocationX++; // Random generator2 = new Random(); playerLocationY = generator.nextInt((6)); // playerLocationY++; lastCell.setLocation(playerLocationX, playerLocationY); for(int j = 0; j < cells.length; j++){ for(int k = 0; k < cells[0].length; k++) { cells[j][k] = new JButton(); add(cells[j][k]); } } setSelection(); addKeyListener(this); // KeyButtonMovement k = new KeyButtonMovement(); // playerLocationX = k.playerLocationX; // playerLocationY = k.playerLocationY; } public void keyReleased (KeyEvent e){} public void keyTyped (KeyEvent e){} public void keyPressed (KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_DOWN: if ((playerLocationY +1)!=8){ playerLocationY = playerLocationY +1; System.out.println("DOWN "+playerLocationX+ " "+playerLocationY); } break; case KeyEvent.VK_UP: if ((playerLocationY -1)!=0){ playerLocationY = playerLocationY -1; System.out.println("UP "+playerLocationX+ " "+playerLocationY); } break; case KeyEvent.VK_LEFT: if ((playerLocationX -1)!=0){ playerLocationX = playerLocationX -1; System.out.println("LEFT "+playerLocationX+ " "+playerLocationY); } break; case KeyEvent.VK_RIGHT: if ((playerLocationX +1)!=8){ playerLocationX = playerLocationX +1; System.out.println("RIGHT "+playerLocationX+ " "+playerLocationY); } break; // default: keyChar = e.getKeyChar(); } setSelection(); } private void setSelection() { cells[lastCell.x][lastCell.y].setText(""); cells[lastCell.x][lastCell.y].setBackground( UIManager.getColor("Button.background")); cells[playerLocationX][playerLocationY].setText("P"); cells[playerLocationX][playerLocationY].setBackground(Color.YELLOW); // cells[playerLocationX][playerLocationY].setForeground(Color.BLACK); lastCell.setLocation(playerLocationX, playerLocationY); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could anyone help with project? billdara New To Java 1 03-12-2008 06:05 PM
Ant Project JavaForums Java Blogs 0 02-16-2008 11:53 PM
First Project Need Big Help earl New To Java 1 01-18-2008 07:12 PM
Pls help with a project. About doing passwords. Thanks saytri New To Java 0 12-15-2007 09:29 AM
First project, need some tips.. Komala_aradhya New To Java 1 08-03-2007 02:25 PM


All times are GMT +3. The time now is 02:58 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org