Results 1 to 4 of 4
- 05-16-2011, 10:53 AM #1
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
updating a gameBoard (JPanel filled with JLabels)
Hello again,
My GUI is finished and working (ignoring some minor flaws) but my displayBoard method seems to be extremely inefficient. I made this on my desktop PC and I didn't really notice how CPU or memory intensive it was until our french presentation today. A colleague showed the game on his laptop (which wasn't rubbish or old at all and should definitely be able to cope with a 'game' like this) and it 'lagged' terribly. It took around 1-2 secs to see the player change position.
TL;DR:
The method displayBoard() is called whenever a change occurs to the gameboard (a player moves or an opponent is defeated, opponents don't move and everything else remains the same). The only way I got this to work is by removing all the components (removeAll() ) and readding them with a double for-loop.
This is the inner class I made for the BoardPanel:
My question:Java Code:private class BoardPanel extends JPanel { private LblFinish lblFinish; private LblEmpty lblEmpty; private LblObst lblObst; private LblPlayer lblPlayer; private LblDeserter lblDes; private LblAlien lblAlien; private LblMercenary lblMerc; private LblSmuggler lblSmug; public BoardPanel() { super(); initBoard(); } private void initBoard() { domeinController.startSpel(); this.setBackground(Color.darkGray); displayBoard(); } public String[][] getBoard(){ return domeinController.getSpelbord(); // gets the board from the domainController as a 2D String Array } public void displayBoard() { this.removeAll(); String[][] spelbord = getBoard(); GridLayout grid = new GridLayout(spelbord.length, spelbord.length); int w = grid.getColumns() * 30; int h = grid.getRows() * 30; this.setSize(w, h); this.setLayout(grid); for (int row = 0; row < spelbord.length; row++) { for (int col = 0; col < spelbord[0].length; col++) { if (row == spelbord.length - 2 && col == spelbord[row].length - 2) { lblFinish = new LblFinish(); this.add(lblFinish); } else { if (spelbord[row][col].equals(" ")) { lblEmpty = new LblEmpty(); this.add(lblEmpty); } else if (spelbord[row][col].equals(" O")) { lblObst = new LblObst(); this.add(lblObst); } else if (spelbord[row][col].equals(" H")) { lblMerc = new LblMercenary(); this.add(lblMerc); } else if (spelbord[row][col].equals(" A")) { lblAlien = new LblAlien(); this.add(lblAlien); } else if (spelbord[row][col].equals(" S")) { lblSmug = new LblSmuggler(); this.add(lblSmug); } else if (spelbord[row][col].equals(" D")) { lblDes = new LblDeserter(); this.add(lblDes); } else if (spelbord[row][col].equals(" P")) { lblPlayer = new LblPlayer(); this.add(lblPlayer); } } } } this.validate(); } }
I would create a second method updateBoard() to update the board without removing all of the components and re-adding them. (changes to the board are made outside of this class or the parent-class)
Is there a way to somehow only modify the player JLabel and opponent JLabel and update the gameBoard?
I was thinking along the lines of a double for-loop again that checks for the player/opponent?
Or should I look for a solution with getComponent() method and the like?
I know this isn't a 'Finish my homework plz plz plz' forum but I'm a bit stumped on how to approach this because I only got 3 hours of sleep and the project is due tomorrow so I really don't have a lot of time to start experimenting.
Any help, pointers in the right direction, suggestions, anything would be greatly appreciated.
Best regards,
Bloitz
- 05-16-2011, 06:42 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I have no idea what your game does or what all your custom classes are but I would guess you can just use label.setIcon(...) method to update your board.
When you move a player, you would use label.setIcon(null) at the old position and then label.setIcon(player) at the new position. Then only those two labels will be repainted since nothing else in the game changes.
- 07-07-2011, 06:06 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
Try this!
Java Code:private void initBoard() { domeinController.startSpel(); this.setBackground(Color.darkGray); displayBoard(); setVisible(true); }
- 07-07-2011, 06:31 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
How can I add JLabels, JButtons, JTextFields to a JPanel then JFrame?
By crazysketch in forum New To JavaReplies: 5Last Post: 03-05-2011, 12:43 AM -
JPanel and JLabels Not Repainting
By phosphide in forum AWT / SwingReplies: 6Last Post: 11-13-2010, 03:17 AM -
new JLabels not showing in JPanel (doing revalidate&repaint)
By r00tb33r in forum AWT / SwingReplies: 6Last Post: 06-16-2010, 06:03 AM -
n00b : Ghost image while using mouseDragged event to move a filled jPanel
By ankitmcgill in forum New To JavaReplies: 5Last Post: 11-02-2008, 06:41 AM -
How to print an object-filled array?
By Verna_Venisa in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks