Reputation: 49
I have an applet that's only purpose is to create a box and each time it's painted it changes color. Right now it is not changing color at all, it simply creates a random background color to start and sticks with it whenever painted but I need it to change. Any help on what I'm doing wrong would be appreciated.
import java.applet.*;
import java.awt.*;
import java.util.*;
public class AppletSubClass2 extends Applet {
public void init() {
System.err.println("Hello from AnAppletSubClass.init");
setBackground(color);
}
public void paint(Graphics g) {
System.err.println("Hello from .paint!This time the applet will change colors when painted");
setBackground(new Color(randomNum1, randomNum2, randomNum3));
}
Random rand = new Random();
int randomNum1 = rand.nextInt(251);
int randomNum2 = rand.nextInt(251);
int randomNum3 = rand.nextInt(251);
Color color = new Color(randomNum1, randomNum2, randomNum3);
}
Upvotes: 0
Views: 139
Reputation: 347214
You've basically broken the paint chain, nothing is actually painting your background color...
You could do something like...
public void paint(Graphics g) {
int randomNum1 = rand.nextInt(251);
int randomNum2 = rand.nextInt(251);
int randomNum3 = rand.nextInt(251);
Color color = new Color(randomNum1, randomNum2, randomNum3);
setBackground(color);
super.paint(g);
}
But this will set up a infinite cycle of repaint requests which will eventually consume your CPU cycles and make you PC unusable (not to mention flicker like crazy)...
A better solution might be to override the getBackgroundColor
method...
@Override
public Color getBackground() {
int randomNum1 = rand.nextInt(251);
int randomNum2 = rand.nextInt(251);
int randomNum3 = rand.nextInt(251);
Color color = new Color(randomNum1, randomNum2, randomNum3);
return color;
}
This will mean that each time this method is called, it will generate a random color. You can then use, some other process, to force the applet to repaint...
Upvotes: 1
Reputation: 12332
This part of your code only runs once, when you AppletSubClass2 object is instantiated.
Random rand = new Random();
int randomNum1 = rand.nextInt(251);
int randomNum2 = rand.nextInt(251);
int randomNum3 = rand.nextInt(251);
Color color = new Color(randomNum1, randomNum2, randomNum3);
So every call to repaint() after that will use the same values of randomNum1, randomNum2, and randomNum3.
What you probably want is a way to generate a random color, in a method:
public Color generateRandomColor() {
Random rand = new Random();
int randomNum1 = rand.nextInt(251);
int randomNum2 = rand.nextInt(251);
int randomNum3 = rand.nextInt(251);
return new Color(randomNum1, randomNum2, randomNum3);
}
Then use that in repaint():
public void paint(Graphics g) {
setBackground(generateRandomColor());
}
Upvotes: 0
Reputation: 220
try this, for me is working:
setBackground(new Color(rand.nextInt(251), rand.nextInt(251), rand.nextInt(251)));
your applet not change color, because define a random color in the begining, and each time it paint repaint with the same random color declared in the begin.
i hope this help you
Upvotes: 1