Results 1 to 4 of 4
  1. #1
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

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

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  3. #3
    mkjp2011 is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    Try this!
    Java Code:
    private void initBoard() {
    			domeinController.startSpel();
    			this.setBackground(Color.darkGray);
    			displayBoard();
                            setVisible(true);
    		}

  4. #4
    mkjp2011 is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    Anytime that you add components to a container you have to revalidate it

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2011, 12:43 AM
  2. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  3. Replies: 6
    Last Post: 06-16-2010, 06:03 AM
  4. Replies: 5
    Last Post: 11-02-2008, 06:41 AM
  5. How to print an object-filled array?
    By Verna_Venisa in forum New To Java
    Replies: 1
    Last Post: 10-20-2008, 08:09 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
  •