File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Paint in Swing" Watch "Paint in Swing" New topic
Author

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);
else
setCursor(c);
setTitle("ShowAnime");
setResizable(false);
setBounds(200,200,500,500);
setVisible(true);
startAnim();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
}
public void startAnim()
{
th = new Thread(this);
th.start();
}
public void run()
{
actimage++;
if (actimage == MAX_IMAGE - 1) actimage = 0;
repaint();
}
public void paintComponent(Graphics g)
{
this.paintComponent(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 ???
Thanks
Anonymous
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:
<PRE>
setCursor
public void setCursor(int cursorType)
Deprecated. As of JDK version 1.1, replaced by Component.setCursor(Cursor).
</PRE>
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.
HTH
[This message has been edited by Bodie Minster (edited February 28, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Paint in Swing