Results 1 to 4 of 4
- 01-12-2011, 08:43 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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
The rest of the code is on next post.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)); //} }Last edited by AcousticBruce; 01-12-2011 at 10:40 PM.
- 01-12-2011, 08:44 PM #2
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
-
Don't use KeyListeners but instead use Key Binding. The Swing tutorial on this will help get you started.
I'm not sure I understand your requirement here.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%.
- 01-12-2011, 10:43 PM #4
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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
-
Having difficulty adding graphics to a JPanel
By DrKilljoy in forum New To JavaReplies: 20Last Post: 07-20-2010, 08:40 PM -
custom graphics, buttons & drop boxes
By jaytee in forum New To JavaReplies: 1Last Post: 03-14-2010, 12:27 AM -
Jpanel and displaying graphics
By jdsflash in forum New To JavaReplies: 6Last Post: 11-21-2009, 01:14 AM -
Clear Graphics Objects from Jpanel
By DavidG24 in forum AWT / SwingReplies: 2Last Post: 05-20-2009, 09:34 PM -
Newbie need help on JPanel graphics
By junpogi in forum AWT / SwingReplies: 7Last Post: 10-21-2008, 07:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks