aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Applet background color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Applet background color" Watch "Applet background color" New topic
Author

Applet background color

Mala Gupta
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 251
    
    9
I have 2 classes - MyThread and MyThread_1.

===================
MyThread
===================
import java.applet.*;
import java.awt.*;
import java.util.Date;

public class MyThread extends Applet implements Runnable {

Color colorArray[] = {Color.red, Color.blue, Color.cyan, Color.black,Color.white};
int counter = 0;

int clockCtr, backgroundCtr= 0; //used for debugging

Thread t = new Thread(this);

public void init() {
setBackground(Color.red);
t.start();

while(true) {
System.out.println("clock.... " + ++clockCtr);
showStatus((new Date()).toString());

try {
Thread.sleep(1000);
repaint();
}
catch (Exception e) {
}
}
}

public void run() {
while (true) {
System.out.println("background.... " + ++backgroundCtr);
setBackground(colorArray[counter]);

counter++;
if (counter == colorArray.length)
counter = 0;

try {
Thread.sleep(1000);
repaint();
}
catch (Exception e) {
}
}
}
}



===================
MyThread_1
===================
import java.applet.*;
import java.awt.*;
import java.util.Date;

public class MyThread_1 extends Applet implements Runnable {

Color colorArray[] = {Color.red, Color.blue, Color.cyan, Color.black,Color.white};
int counter = 0;

int clockCtr, backgroundCtr= 0; //used for debugging

Thread t = new Thread(this);

public void init() {
setBackground(Color.red);
t.start();
}

public void run() {
while (true) {
System.out.println("background.... " + ++backgroundCtr);
setBackground(colorArray[counter]);

System.out.println("clock.... " + ++clockCtr);
showStatus((new Date()).toString());

counter++;
if (counter == colorArray.length)
counter = 0;

try {
Thread.sleep(1000);
repaint();
}
catch (Exception e) {
}
}
}
}

Can anyone please explain why MyThread class does not changes the background colour of the Applet, although I can see the output of the println statments in both the cases (in both the classes).


Thanks.


Author of Manning's OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet background color