Results 1 to 4 of 4
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Random box game : unsuccessful Buttons & Graphics listneres and JPanel Placement.

    I am working on a small project for fun. It basically randomly creates a grid and and starts you as a box in the top left corner. You can move with the arrow keys and and hit the "r" key to randomly generate a new grid.

    Basically I got it working wothout the add of another JPanel. When I add a new panel with buttons I loose control of the graphics KeyListeners.

    1) is there a way to have listeners for both graphics and buttons? How?

    2) Is there a way to move the 2nd JPanel down toward bottom like say 90% of screen? So basically a GridLayout(2,1) where the first row is 90%.

    please feel free to critique my code if you think it is inefficient, thank you :)





    This is the GUI where the issue is I believe
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    
    public class GUI extends JFrame implements KeyListener, ActionListener {
    
        private DrawBoard board = new DrawBoard();
        private JPanel options = new JPanel();
        private JButton moveBlockB; JTextField moveBlockT;
        private boolean movable = true;
    
        GUI() {
            super("Packy AI");
            setSize(561, 583);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            moveBlockB = new JButton("Movable Blocks?");
            moveBlockT = new JTextField(Boolean.toString(movable), 10);
            moveBlockT.setEditable(false);
            options.add(moveBlockB);
            options.add(moveBlockT);
            add(board);
            [COLOR="Red"]//add(options);                          //All this is causes major problems. 
            //setLayout(new GridLayout(2,1));
            //moveBlockB.addActionListener(this);[/COLOR]
            this.addKeyListener(this);
            setVisible(true);
        }
    
        public void keyPressed(KeyEvent event) {
            if(event.getKeyCode() == 37) {
                board.moveLeft();
            }
            if(event.getKeyCode() == 38) {
                board.moveUp();
            }
            if(event.getKeyCode() == 39) {
                board.moveRight();
            }
            if(event.getKeyCode() == 40) {
                board.moveDown();
            }
            if(event.getKeyCode() == 82) {
            board.drawRandBox();
            }
        }
        public void keyReleased(KeyEvent event) {
        }
        public void keyTyped(KeyEvent event) {
        }
    
        public void actionPerformed(ActionEvent event) {
          //  if (movable) movable = false;
          //  else movable = true;
          //  moveBlockT.setText(Boolean.toString(movable));
        //}
    }
    The rest of the code is on next post.
    Last edited by AcousticBruce; 01-12-2011 at 10:40 PM.

  2. #2
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    This is probably not needed. But just in case anyone was curious.









    This is the DrawBoard class which contains the graphics and boundries.
    Java Code:
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.util.Random;
    
    
    public class DrawBoard extends JPanel {
        private int x = 2*55+10, y = 65;
        private final int SPEED = 55;
        private boolean[][] maze = new boolean[10][10];
        boolean draw = true;
        Random rand = new Random();
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            this.setBackground(Color.BLACK);
    
            //drawing graphics
            if (draw) {
                drawMaze();
            }
            for(int row = 0; row < 10; row++) {
                for (int col = 0; col < 10; col++) {
                    if (maze[row][col]) {
                        g.setColor(Color.WHITE);
                        g.fillRect((row*55),(col*55), 50, 50);
                    }
                }
            }
            g.setColor(Color.RED);
            g.fillRect(this.x, this.y, 30, 30);
        }
        private void drawMaze() {
            for (int row = 0; row < 10; row++) {
                for (int col = 0; col < 10; col++) {
                    boolean fill = rand.nextBoolean();
                    if (fill && row * 55 + 10 == this.x && col * 55 + 10 == this.y) {
                        break;
                    }
                    maze[row][col] = fill;
                }
            }
            draw = false;
        }
    
        private boolean checkBounds() {
            if(x < 10 || y < 10 || x > 505 || y > 505 || maze[this.x/55][this.y/55])
                return false;
            return true;
        }
    
        public void drawRandBox() {
            drawMaze();
            repaint();
        }
        public void moveLeft() {
    
            this.x -= SPEED;
            if (!checkBounds()) {
                int xEq = this.x/55, yEq = this.y/55;
                if(xEq - 1 >= 0 && !maze[xEq-1][yEq]) {
                    maze[xEq][yEq] = false;
                    maze[xEq-1][yEq] = true;
    
                } else {
                    this.x += SPEED;
                }
            }
            repaint();
    
        }
        public void moveUp() {
            this.y -= SPEED;
            if (!checkBounds()) {
                int xEq = this.x/55, yEq = this.y/55;
                if(yEq - 1 >= 0 && !maze[xEq][yEq-1]) {
                    maze[xEq][yEq] = false;
                    maze[xEq][yEq-1] = true;
    
                } else {
                    this.y += SPEED;
                }
            }
            repaint();
    
        }
        public void moveRight() {
            this.x += SPEED;
            if (!checkBounds()) {
                int xEq = this.x/55, yEq = this.y/55;
                if(xEq + 1 < 10 && !maze[xEq+1][yEq]) {
                    maze[xEq][yEq] = false;
                    maze[xEq+1][yEq] = true;
    
                } else {
                    this.x -= SPEED;
                }
            }
            repaint();
    
        }
        public void moveDown() {
            this.y += SPEED;
            if (!checkBounds()) {
                int xEq = this.x/55, yEq = this.y/55;
                if(yEq + 1 < 10 && !maze[xEq][yEq+1]) {
                    maze[xEq][yEq] = false;
                    maze[xEq][yEq+1] = true;
    
                } else {
                    this.y -= SPEED;
                }
            }
            repaint();
    
        }
    }


    Main Class
    Java Code:
    public class Main {
        public static void main(String[] args) {
            new GUI();
        }
    }
    Last edited by AcousticBruce; 01-12-2011 at 09:04 PM.

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    Basically I got it working wothout the add of another JPanel. When I add a new panel with buttons I loose control of the graphics KeyListeners.

    1) is there a way to have listeners for both graphics and buttons? How?
    Don't use KeyListeners but instead use Key Binding. The Swing tutorial on this will help get you started.


    2) Is there a way to move the 2nd JPanel down toward bottom like say 90% of screen? So basically a GridLayout(2,1) where the first row is 90%.
    I'm not sure I understand your requirement here.

  4. #4
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Quote Originally Posted by Fubarable View Post
    Don't use KeyListeners but instead use Key Binding. The Swing tutorial on this will help get you started.
    What do you use KeyListenersfor?




    [CODE]I'm not sure I understand your requirement here.[/QUOTE]

    if you do GridLayout(2,1) there is 2 panels and they both take up 50% of the window.
    Is there a way to change the size of each grid so one can take up 90% and the other 10%.
    Last edited by AcousticBruce; 01-12-2011 at 10:45 PM.

Similar Threads

  1. Having difficulty adding graphics to a JPanel
    By DrKilljoy in forum New To Java
    Replies: 20
    Last Post: 07-20-2010, 08:40 PM
  2. custom graphics, buttons & drop boxes
    By jaytee in forum New To Java
    Replies: 1
    Last Post: 03-14-2010, 12:27 AM
  3. Jpanel and displaying graphics
    By jdsflash in forum New To Java
    Replies: 6
    Last Post: 11-21-2009, 01:14 AM
  4. Clear Graphics Objects from Jpanel
    By DavidG24 in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2009, 09:34 PM
  5. Newbie need help on JPanel graphics
    By junpogi in forum AWT / Swing
    Replies: 7
    Last Post: 10-21-2008, 07:44 AM

Posting Permissions

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