?
Solved

Simple Animation

Posted on 2003-02-24
2
Medium Priority
?
206 Views
Last Modified: 2010-03-31
i have written the following applet to display a red and black chess board :

import java.awt.*;
import java.applet.*;

public class Checker extends Applet {

    public void paint(Graphics g) {
   
        int x,y,xint,yint;
        xint = 0;
        yint = 0;
   
        g.setColor(Color.black);
        g.fillRect(0,0,159,159);
       
        g.setColor(Color.red);
       
        for(y = 1; y <= 8; y++) {
            for(x = 1; x<=8; x++){
                if((((y % 2) == 0) && ((x % 2) == 0)) || (((y % 2) != 0) && ((x % 2) != 0))){
                    g.fillRect(xint,yint,20,20);
                }
                xint += 20;
            }
            yint +=20;
            xint = 0;
        }
    }
}

is there a way to tweak this so as to produce an animation where the squares alternate colors between red and black ?
0
Comment
Question by:4mod3
2 Comments
 

Author Comment

by:4mod3
ID: 8014728
Just to clarify, my question is based on how to actually animate it within a browser.
I know how to alternate, just need help animating.
0
 
LVL 2

Accepted Solution

by:
cocojohn earned 400 total points
ID: 8014927
To animate the applet, you have to repaint it in intervals:

public class Checker extends Applet implements Runnable {

   Thread thread;
   boolean colorFlag = false;

   //start the animation thread when starting the applet
   public void init(){
    thread = new Thread(this);
    thread.start();
   }

   //swap colors all 500 ms
   public void run(){
     try{
     while(thread.isAlive()){
       thread.sleep(500);
       colorFlag = !colorFlag;
       repaint();
     }    
     }catch(Exception ex){}
   }

   public void paint(Graphics g) {
   
       //...

       if(colorFlag){
         g.setColor(Color.black);
       }else{
         g.setColor(Color.red);
       }
       g.fillRect(0,0,159,159);
       
       if(colorFlag){
         g.setColor(Color.red);
       }else{
         g.setColor(Color.black);
       }
       
       //...
   }
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month5 days, 20 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question