4 Replies - 243 Views - Last Post: 17 November 2011 - 12:14 PM Rate Topic: -----

#1 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Why is my screen not clearing?

Posted 17 November 2011 - 01:50 AM

It's almost 4am and this is annoying the hell out of me.

I made a class as an action listener to change the difficulty mode of a game I made.

When the user clicks a button, the code executes and it's supposed to execute Main.clearScreen = true;

But it's not working, the screen is not clearing.



package game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChoseMode implements ActionListener{	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("EASY")){
	    	Main.setMode(100);
	    }
	    if(e.getActionCommand().equals("NORMAL")){
	    	Main.setMode(50);
	    }
	    if(e.getActionCommand().equals("HARD")){
	    	Main.setMode(20);
	    }	
	    Main.gameOver = false;
	    Main.clearScreen = true; //<---------------
	    Main.start = true;
	    removeListener();
	}
	
	private void removeListener(){
		Main.easy.setVisible(false);
		Main.normal.setVisible(false);
		Main.hard.setVisible(false);
		Main.easy.removeActionListener(this);
		Main.normal.removeActionListener(this);
		Main.hard.removeActionListener(this);
	}
}



and here's the relevant code in the main class (I omitted a lot of stuff):

public static boolean clearScreen = false; 

	public void paint(Graphics g){
		if(start){
			if(clearScreen){
				clearScreen(g);
			}
	}	

	private void clearScreen(Graphics g){
		g.setColor(getBackground());
		g.fillRect(0, 0, WIDTH, HEIGHT+50);
		clearScreen = false;
	}	



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my screen not clearing?

#2 MrSooty  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 85
  • Joined: 14-September 11

Re: Why is my screen not clearing?

Posted 17 November 2011 - 03:02 AM

Don't you need to call repaint() somewhere?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why is my screen not clearing?

Posted 17 November 2011 - 07:42 AM

And what does your paint() supposed to do if you are not in a "clearScreen" state ?
Was This Post Helpful? 0
  • +
  • -

#4 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Why is my screen not clearing?

Posted 17 November 2011 - 09:17 AM

View Postpbl, on 17 November 2011 - 07:42 AM, said:

And what does your paint() supposed to do if you are not in a "clearScreen" state ?

	public void paint(Graphics g){
		if(start){
			if(clearScreen){
				clearScreen(g);
			}
			if(!gameOver){
				paintObjects(g);
				heading(g);
			}			
			if(gameOver){
				gameOver(g);
			}	
		}
		else{
			clickToBegin(g);
		}



View PostMrSooty, on 17 November 2011 - 03:02 AM, said:

Don't you need to call repaint() somewhere?

	public void actionPerformed(ActionEvent e){	   
    	if(!gameOver && start){
    		if(bonus && !bonusShots){
    			bonusCount++;
    		}
    		spaceShipColor();
    		count++;
    		count %= 500;
	        addNewAlien();
	        addNewMeteor();
	        addNewBonus();
	        gotStar();
	        moveStar();
	        endBonus();
	        moveBullet();
	        moveAlien();
	        moveMeteor();
	    	hit(); 
	    	lose();
	    	bulletOutOfBounts();
	    	repaint();
    	}
    	if(gameOver){
    		addListener();
    		highScore();    		
    		reset();
    	}
    }


Was This Post Helpful? 0
  • +
  • -

#5 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Why is my screen not clearing?

Posted 17 November 2011 - 12:14 PM

Okay I figured a way around it. Thanks anyways.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1