Display problem

My program displays correctly on OS X but when I run on XP I have display issues. I believe that there is something wrong with my paint function. When the game is started it looks fine but when a repaint occurs another paint occurs in the incorrect position thus displaying two game board backgrounds. I am sure that it is simple. Would somebody mind having a look? Cheers.

public void paintComponent( Graphics g ) {

Graphics2D g2d = (Graphics2D)g;

if(gameData.gameStatus == 4 || gameData.gameStatus == 5) {

g2d.drawImage(backgroundImage, 50, 0, 625, 418, observer);

clip.stop();

}

if(gameData.gameStatus == 1) {

System.out.println("Player One wins");

g2d.drawImage(playerOneWinImage, 50, 0, 625, 418, observer);

}

if(gameData.gameStatus == 2) {

System.out.println("Player Two wins");

g2d.drawImage(playerTwoWinImage, 50, 0, 625, 418, observer);

}

if(gameData.gameStatus == 3) {

}

for(int i=0; i<7; i++) {

for(int j=0; j<6; j++) {

if(gameData.gameBoard[j]==1) {

g2d.setPaint(Color.yellow);

circle.setFrame(114 + (i*80), 344 - (j*60), 20, 20);

g2d.fill(circle);

}

if(gameData.gameBoard[j]==2) {

g2d.setPaint(Color.red);

circle.setFrame(114 + (i*80), 344 - (j*60), 20, 20);

g2d.fill(circle);

}

}

}

if(gameData.gameStatus != 4 && gameData.gameStatus != 5) {

gameData = gameFunctions.resetGame(gameData);

gameData.gameStatus = 4;

if(gameData.gameStatus != 3) {

clip.play();

}

}

}

[1639 byte] By [rivasa] at [2007-11-27 7:36:55]
# 1

You shouldn't set state in your painting methods. (which you seem to be doing in the last couple blocks of paintComponent)

Also it's nasty to track state with ints or Strings when the state in question isn't specifically about numbers or text.So instead of checking to see if gameStatus is 4 or 5 or 1 or whatever, consider defining an enum. Better yet, you could put some of that behavior -- drawing the image, or changing whether the clip is playing -- directly into objects representing the state.

When you post code, please wrap it in [code][/code] tags so it's easy to read.

paulcwa at 2007-7-12 19:17:26 > top of Java-index,Java Essentials,Java Programming...