Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problems with RepaintManager

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Location
    Minneapolis MN
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problems with RepaintManager

    I'm working on a project that draws a circle every 1000 millisec but when my loop finishes it calls the repaint manager and all my circles are removed and redrawn. Any ideas on how to prevent the repaint?

    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.image.*;
     
    public class Forest extends JApplet {
    	Random rand;
    	JButton one;
    	public void init() {
    		setLayout(new FlowLayout());
    		one = new JButton("one");
    		add(one);
    	}
     
    	public void paint(Graphics g) {
    		super.paint(g);
    		int treeX = getWidth()-50, treeY=getHeight()-50, count=0;
     
     
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
    		Color colorTree = new Color(randomGenerator(255),randomGenerator(255),randomGenerator(255));
    			//loop for painting trees
    			while(count < 3){
    				wait(1000);
    				paintTree(g2d,randomGenerator(treeX),randomGenerator(treeY),colorTree);
    	    		colorTree = new Color(randomGenerator(255),randomGenerator(255),randomGenerator(255)); 	
    	    		count = count + 1;
    			}
     
     
    	}
    	//Method that builds tree based on random x y coordinates
    	public void paintTree(Graphics2D gt, int x, int y, Color treeColor){
     
    		gt.setColor(treeColor);
    		gt.fillOval(x,y,50,50);
    		gt.setColor(new Color(128,64,0));
    		gt.fillRect((x+15),(y+47),20,40);
     
    	}
    	//method to return random number with upper bound passed
    	public int randomGenerator(int randNum){
    		rand = new Random();
    		return rand.nextInt(randNum);
    	}
    	//wait before drawing next tree
    	public void wait (int n){
            long t0,t1;
            t0=System.currentTimeMillis();
            do{
                t1=System.currentTimeMillis();
            }
            while (t1-t0<n);
    	}
     
     
     
    }
    Last edited by helloworld922; October 1st, 2011 at 11:40 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Problems with RepaintManager

    When paint is called Your code clears the screen and draws 3 times
    When the screen is to be repainted, it does it again.

    What do you want the program to do?

    If you want to save all the previously drawings, you need to use an offscreen image that you can use to accumulate your drawings on and draw that in the paint method. Or create a collection of where to draw your shapes and use that every call to paint to draw all your shapes.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Location
    Minneapolis MN
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problems with RepaintManager

    I'd like the paint to be called once. Paint 3 drawings with a short delay and then keep the images on the screen. I'd like to avoid the repaint from happening.
    What do I use to draw off screen? Would that be the Buffer graphic?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Problems with RepaintManager

    You can't stop the calls to the paint method. You could use a boolean flag to control what the paint method does. If the flag is not set, set it and do the drawing. Next call to paint will find the flag set and skip the drawing.
    What happens if another window crosses the drawings or your frame is minimized? The contents of the screen will be changed. The call to paint will not draw your stuff.
    What do I use to draw off screen? Would that be the Buffer graphic?
    Look at what's called the double buffering technique. Basically it gets a BufferedImage, draws on that image and then has the paint method draw that image on the screen,
    Last edited by Norm; October 2nd, 2011 at 10:00 AM.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Location
    Minneapolis MN
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problems with RepaintManager

    Help me understand why the repaint is even called. If I take the wait(n) method out it draws fine but all at once. Why does it get marked as "dirty" when I add the pause?
    Thanks for the help on the previous post, I'll look into the buffering technique.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Problems with RepaintManager

    Sorry, I don't know why the JVM calls the paint() method when it does.

Similar Threads

  1. MP3 problems
    By relion65 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 15th, 2011, 12:09 PM
  2. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 01:51 PM
  3. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  4. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  5. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM