2014-11-20 73 views
0

我有一个小程序,它的唯一目的是创建一个盒子,每次它被绘时,它都会改变颜色。现在它根本不会改变颜色,只需创建一个随机背景颜色即可开始绘画,但是我需要改变颜色。任何帮助我做错了将不胜感激。Java小程序随着油漆而变化

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); 
} 
+0

1)为什么要编写小程序?如果是由于老师指定它,请将它们转介给[为什么CS教师应该**停止**教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should -stop教学-java的小应用程序/)。 2)为什么使用AWT?看到[这个答案](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978)有很多很好的理由放弃AWT使用组件有利于Swing。 – 2014-11-21 22:41:43

+0

谢谢,但这是由教授 – user3587186 2014-11-21 22:53:55

+0

*所有预定义*“这是由教授预先定义的* *这正是我为什么写的* *”..请参考[为什么CS教师应该停止**教Java小程序](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。“*我没有写这个消息来回读它。 **告诉他们访问链接!** – 2014-11-21 23:00:54

回答

1

你基本上打破了油漆链,没有什么是实际绘画你的背景颜色...

你可以做类似...

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); 
} 

但这将设立重绘请求将最终消耗你的CPU周期,让你的PC无法使用(更不用说闪烁像疯了似的)的无限循环......

一个更好的解决方案可能是覆盖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; 
} 

这将意味着每个这种方法被调用时,它会生成一个随机的颜色。然后,您可以使用其他一些过程强制重新绘制小程序...

+0

谢谢,这个想法绝对有效,但是我有麻烦想办法强制重画。我知道我基本上每次都必须“删除”颜色对象,并在每次调用新对象时创建一个新对象,但我无法弄清楚如何实现。 – user3587186 2014-11-21 23:07:07

+0

在applet上调用'repaint'。你可以使用某种'定时器'或背景'线程' – MadProgrammer 2014-11-22 00:05:12

1

试试这个,对我来说是工作:

setBackground(new Color(rand.nextInt(251), rand.nextInt(251), rand.nextInt(251))); 

小程序不变色,因为定义在开始时随机颜色,每一次画 与申报相同的随机颜色重绘在开始。

我希望这会帮助你

+0

这样做的作用在于它随机更改颜色,但它会自动执行,而无需我最小化/最大化小程序。我认为油漆只有在浏览器重新绘制时才被调用,而不是自动调用? – user3587186 2014-11-20 22:11:13

+0

@ user3587186你认为'setBackground'在做什么?每当你打电话时它会触发一个重绘请求...但你已经打破了油漆链... – MadProgrammer 2014-11-20 23:16:56

0

你的这部分代码,只有当你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); 

所以每次调用重绘()后,将使用randomNum1,randomNum2和randomNum3的值相同。

你可能要的是生成一个随机颜色的方法,在方法:

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); 
} 

然后使用在重绘():

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