简体   繁体   English

Java Applet更改为Paint

[英]Java Applet Changed With Paint

I have an applet that's only purpose is to create a box and each time it's painted it changes color. 我有一个applet,它的唯一目的是创建一个盒子,每次涂完它都会改变颜色。 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);
}

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. 您的applet不会更改颜色,因为在开始时定义了随机颜色,并且每次它使用开始时声明的相同随机颜色绘制重绘。

i hope this help you 希望对您有帮助

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)... 但这将设置一个无限的重画请求周期,最终将消耗您的CPU周期并使您的PC无法使用(更不用说像疯狂的闪烁一样了)...

A better solution might be to override the getBackgroundColor method... 更好的解决方案可能是重写getBackgroundColor方法...

@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... 然后,您可以使用其他方法来强制小程序重新绘制...

This part of your code only runs once, when you AppletSubClass2 object is instantiated. 实例化AppletSubClass2对象时,这部分代码仅运行一次。

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. 因此,此后每次对repaint()的调用都将使用相同的randomNum1,randomNum2和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(): 然后在repaint()中使用它:

public void paint(Graphics g) {
    setBackground(generateRandomColor());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM