Results 1 to 5 of 5

Thread: Project Help

  1. #1
    XxHEXSORxX is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default 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();
    }
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

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

  3. #3
    abhishek ims is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    i need ource code of physical library management system

  4. #4
    abhishek ims is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    i need source code of physical library management system in java

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    i need source code of physical library management system in java
    So write it then. If you create a program and have a problem with it, then by all means post your code and ask your question, but don't just demand code as this smells of laziness. This is not a code mill.

Similar Threads

  1. Could anyone help with project?
    By billdara in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 05:05 PM
  2. First Project Need Big Help
    By earl in forum New To Java
    Replies: 1
    Last Post: 01-18-2008, 06:12 PM
  3. Replies: 0
    Last Post: 12-15-2007, 08:29 AM
  4. First project, need some tips..
    By Komala_aradhya in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •