DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2006

    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.

  2. #2
    Join Date
    Oct 2005
    here's a base class you could use
    /** 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 {
          } catch (InterruptedException e) {
            _isRunning = false;
      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
    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() {
        _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))
          } while (image == null);
          g.drawImage(image, 0, 0, this);
        } else {
      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() {
      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

  1. Replies: 0
    Last Post: 04-17-2006, 07:04 AM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  3. Replies: 0
    Last Post: 08-18-2000, 10:35 AM
  4. Create Interactive Java Applets without Learning Java
    By Jimmy D in forum web.announcements
    Replies: 0
    Last Post: 08-18-2000, 10:32 AM
  5. Re: VB vs. Visual Age for Java
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.