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 4 of 4

Thread: How do I use the repaint method to "undraw"? (I am a noob)

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    My Mood
    Hungover
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How do I use the repaint method to "undraw"? (I am a noob)

    I made this very simple program as a test to experiment with animation. I have successfully animated the circle yes, but it leaves a black trail for every iteration of the circle.

    How do I "undraw" the circle for every iteration so that visually it appears as just a circle moving with no trail? Do I use the repaint method? I have inserted repaint into the code, but it didn't work... Here's my code:
    import java.awt.*;
    import jpb.*;
     
    public class moveCircle
    {
    	public static void main(String[] args)
    	throws java.lang.InterruptedException
    	{
    		// window is created
    		DrawableFrame window = new DrawableFrame("Check out the moving circle, yeah");
    		window.show();
    		window.setSize(1010, 700);
    		Graphics g = window.getGraphicsContext(); // graphics allowed within window
     
    		// circle is created
    		int xCoordinate = 795;
    		while(true)
    		{
    			Thread.sleep(10);
    			xCoordinate++;
    			if(xCoordinate > 1010)
    				xCoordinate = -75;
    			g.setColor(Color.lightGray);
    			g.fillOval(xCoordinate, 295, 60, 60);
    			g.setColor(Color.black);
    			g.drawOval(xCoordinate, 295, 60, 60);
    			window.repaint();
    		}
    	}
    }
    Last edited by racecar333; October 31st, 2011 at 03:09 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: How do I use the repaint method to "undraw"? (I am a noob)

    To properly draw using Swing, you should override the paintComponent method of a JPanel, and add that JPanel to a JFrame. To draw over the previous, make a call to the parent method (super.paintComponent()). I would never advise grabbing and drawing to a Graphics object through a call to getGraphics (I presume this is what the method getGraphicsContext does but given you don't provide the code for that class cannot say for sure)
    See the following for more information: Trail: 2D Graphics (The Java™ Tutorials)
    Lastly, if DrawableFrame extends JFrame, show is deprecated...use setVisible

  3. The Following User Says Thank You to copeg For This Useful Post:

    racecar333 (October 31st, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    My Mood
    Hungover
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do I use the repaint method to "undraw"? (I am a noob)

    Hmm, the above imports I am using allow for this code to work (other than it not "repainting"), and is for a college assignment. It seems that we are to use this deprecated stuff instead of swing, as we have not been taught swing yet...

    So, I'm going to study this link that you gave me Copeg and learn about using swing, but do you know of a way to make the above deprecated code function without a code overhaul?

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do I use the repaint method to "undraw"? (I am a noob)

    Deprecated doesn't mean it doesn't work, it mostly means you shouldn't use it. Do a find replace for show() with setVisible(true)

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  3. "The import ___ cannot be resolved" (stupid noob question)
    By RobG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2010, 02:09 PM
  4. "showMessageDialog" method in swing package
    By Delmi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 02:52 PM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM

Tags for this Thread