?
Solved

Simple Animation

Posted on 2003-02-24
2
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 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