It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Paint in Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Paint in Swing" Watch "Paint in Swing" New topic

Paint in Swing

Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
I want to show a little animation in a swingframe, but it only shows ... nothing. I use paintComponents, should I use paint ? Here is the code, can you help me, please :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Toolkit.*;
public class ShowAnim extends JFrame implements Runnable
Image img[] = new Image[MAX_IMAGE];
public static final int MAX_IMAGE = 2;
int actimage = 0;
Thread th;
public ShowAnim()
Cursor c = getToolkit().createCustomCursor( new ImageIcon("gif/cur.gif").getImage(), new Point(1,1), "Cursor");
Dimension d;
int actima = 0;

for (int i = 1; i < MAX_IMAGE; i++) img[i] = getToolkit().getImage("gif/"+i+".gif"); // get Pictures
d = getToolkit().getBestCursorSize(32,32);
if ((d.width != 32) | | (d.height != 32)) setCursor(CROSSHAIR_CURSOR);
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent event)
public void startAnim()
th = new Thread(this);
public void run()
if (actimage == MAX_IMAGE - 1) actimage = 0;
public void paintComponent(Graphics g)
g.drawImage(img[actimage], 1, 1, this);
public static void main(String arg[])
JFrame f = new ShowAnim();
The start of the thread should call run() and so repaint().
One more question : The compiler always say the function setCursor(CROSS_HAIR..); is deprecated, but the setCursor(c); is not deprecated. WHY ???
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have had some trouble acquiring images using the DefaultToolkit unless I specify the full path and file name. If this is your problem, you should be able to find out by using a MediaTracker to track the loading of your image files.
With respect to the depricated method, the 1.3 API says the following:
public void setCursor(int cursorType)
Deprecated. As of JDK version 1.1, replaced by Component.setCursor(Cursor).
This was found in the Frame class, which is the superclass of JFrame. You used to be able to set the cursor for the application by setting the cursor for a frame. Now you are supposed to set the cursor for each component that wants to use it. If you want the entire painted area of your application to show a specific cursor, try adding a JPanel to your frame and set the cursor for the JPanel.
[This message has been edited by Bodie Minster (edited February 28, 2001).]
I agree. Here's the link:
subject: Paint in Swing
It's not a secret anymore!