• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Paint in Swing

Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
please buy my thing and then I'll have more money:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic