0

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);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3587186
  • 49
  • 1
  • 8
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Nov 21 '14 at 22:41
  • thanks but this is all predefined by the professor – user3587186 Nov 21 '14 at 22:53
  • *"this is all predefined by the professor"* Which is exactly why I wrote.. *"..please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)."* I did not write that for the amusement of reading it back. **Tell them to visit the link!** – Andrew Thompson Nov 21 '14 at 23:00

3 Answers3

1

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

Juan Henao
  • 220
  • 1
  • 3
  • 14
  • this works in that it changes colors randomly however it does it automatically, without me having to minimize/maximize the applet. i thought paint was called only when it was repainted in the browser and not automatically? – user3587186 Nov 20 '14 at 22:11
  • @user3587186 What do you think `setBackground` is doing? It is triggering a `repaint` request each time you call it...but you have broken the paint chain... – MadProgrammer Nov 20 '14 at 23:16
1

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...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • thanks, this idea definitely works however I'm having trouble coming up with a way to force it to repaint. I know I essentially have to "delete" the color object each time and make a new one each time its called to get a new one but I can't figure out how to do that. – user3587186 Nov 21 '14 at 23:07
  • Call `repaint` on the applet. You could use some kind of `Timer` or background `Thread` – MadProgrammer Nov 22 '14 at 00:05
0

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());
}
martinez314
  • 12,162
  • 5
  • 36
  • 63