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:
Code Java:
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();
}
}
}
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
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?
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)