-
HELP somewhat new to java applets
Hello to all:
My question: How do you make graphic animated with the " public void paint (Graphics g)". In other words, I made two graphics and want one to display and then disappear (appear then disappear). For instance, some of the graphics that I have are rectangles and I want them to change colors with out a blended color effect. For Example I have the following in the paint method.
Rectangle 1 g.setColor (Color.blue);
g.fillRoundRect (235,100,60,60,35,35);
// this is the same square but different color
Rectangle2 g.setColor (Color.red);
g.fillRoundRect (235,100,60,60,35,35);
I have tried to use this follow statement:
try {Thread.sleep(1000); } catch (InterruptedException e) {}
but that above thread causes a delay only then makes the rectangles appear. I want them to appear first then disappear for good until the my full animation runs completely then the cycle can start over agian.
Any help or point to a tutor site that shows how to do this.
ThankYou.
-
here's a base class you could use
Code:
/** Class for demonstrating simple animation.
* <ul>
* <li> added <tt>volatile</tt> keyword
* <li> Changed to use <tt>isRunning</tt> flag
* <li> {@link #init()} sets delay
* <li> Stops on interruption
* </ul>
*
*/
public class AnimationApplet
extends java.applet.Applet
implements java.lang.Runnable {
public void init() {
try {
_delay = Integer.parseInt(getParameter("delay"));
} catch (NullPointerException ex) {
// ignore this
} catch (NumberFormatException ex) {
// should LOG the problem
}
}
public void start() {
_isRunning = true;
new Thread(this).start();
}
public void stop() {
_isRunning = false;
}
public void run() {
while (_isRunning) {
try {
Thread.sleep(_delay);
} catch (InterruptedException e) {
_isRunning = false;
}
repaint();
}
}
public void setDelay(int delay) {
this._delay = delay;
}
public int getDelay() {
return _delay;
}
protected volatile boolean _isRunning = false;
protected volatile int _delay = 100;
}
And here's one that adds double buffering
Code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
/** An extension of animation applet to handle double-buffering.
* <ul>
* <li> Offscreen area resized on window resize.
* <li> <tt>paintFrame</tt> removed.
* <li> <tt>initAnimator</tt> removed. (rely on overriders
* calling super.init())
* </ul>
*
*
*/
public abstract class DBAnimationApplet extends AnimationApplet {
public void init() {
super.init();
_d = getSize();
}
public final void update(Graphics g) {
if (_doubleBuffered) {
Image image;
Graphics offscreen;
do {
image = this._offscreenImage;
offscreen = this._offscreenGraphics;
Dimension current = getSize();
if (image == null
|| current.getWidth() != image.getWidth(this)
|| current.getHeight() != image.getHeight(this))
allocateOffscreen();
} while (image == null);
super.update(offscreen);
g.drawImage(image, 0, 0, this);
} else {
super.update(g);
}
}
private synchronized void allocateOffscreen() {
_d = getSize();
if (_d.width <= 0) _d.width = 1;
if (_d.height <= 0) _d.height = 1;
_offscreenImage = createImage(_d.width, _d.height);
_offscreenGraphics = _offscreenImage.getGraphics();
}
protected DBAnimationApplet(boolean doubleBuffered) {
this._doubleBuffered = doubleBuffered;
}
protected DBAnimationApplet() {
this(true);
}
protected boolean _doubleBuffered;
protected Dimension _d;
protected Image _offscreenImage;
protected Graphics _offscreenGraphics;
}
you can extend DBAnimationApplet and write your paint method. Just supply a delay parameter to the applet tag. Your going to want to increment some variable in the paint method to know what to draw because that method will be executed every delay ms. Make sure you call super.init()
Similar Threads
-
Replies: 0
Last Post: 04-17-2006, 07:04 AM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Jimmy D in forum Enterprise
Replies: 0
Last Post: 08-18-2000, 10:35 AM
-
By Jimmy D in forum web.announcements
Replies: 0
Last Post: 08-18-2000, 10:32 AM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks