Java 使用随机颜色更新Applet
我试图制作一个背景随时间缓慢变化的JFrame。 这是我的密码:Java 使用随机颜色更新Applet,java,swing,applet,Java,Swing,Applet,我试图制作一个背景随时间缓慢变化的JFrame。 这是我的密码: public class RainbowWindow extends Applet implements ActionListener { private static final long serialVersionUID = 1L; public void paint(Graphics g) { Timer timer = new Timer(1000, this); tim
public class RainbowWindow extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
Timer timer = new Timer(1000, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
Color color = new Color(((int) Math.random()), ((int) Math.random()), ((int) Math.random()), 255);
setBackground(color);
}
}
但我得到的只是一个黑屏
绘制
中创建计时器
,每次需要绘制组件时都会调用绘制,并且通常会快速连续地调用绘制。有关如何在Swing中进行绘制的更多详细信息,请参见ndJFrame
和Applet
)的paint
,它们不是双缓冲的,而且往往会导致无休止的问题,最好从某种组件开始,并将其添加到您想要的任何容器中JFrame
!=<代码>小程序Math.random
返回介于0
和1
之间的值,Color
需要介于0
-255
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
Color color = new Color(red, green, blue, 255);
setBackground(color);
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
绘制
中创建计时器
,每次需要绘制组件时都会调用绘制,并且通常会快速连续地调用绘制。有关如何在Swing中进行绘制的更多详细信息,请参见ndJFrame
和Applet
)的paint
,它们不是双缓冲的,而且往往会导致无休止的问题,最好从某种组件开始,并将其添加到您想要的任何容器中JFrame
!=<代码>小程序Math.random
返回介于0
和1
之间的值,Color
需要介于0
-255
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
Color color = new Color(red, green, blue, 255);
setBackground(color);
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
您应该扩展
JApplet
(而不是Applet
),并添加一个组件来设置颜色,并且您不应该在paint
方法中启动Timer
,将浮点值强制转换为int
不会在随机颜色生成中给您任何值。大概
public class RainbowWindow extends JApplet implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel = new JPanel();
@Override
public void actionPerformed(ActionEvent e) {
Color color = new Color(((int) (Math.random() * 255)),
((int) (Math.random() * 255)),
((int) (Math.random() * 255)), 255);
panel.setBackground(color);
}
public RainbowWindow() {
Timer timer = new Timer(1000, this);
timer.start();
add(panel);
setVisible(true);
}
}
我测试了它,它每秒将背景颜色更改为一种新的随机颜色。您应该扩展
JApplet
(而不是Applet
),并添加一个组件来设置颜色,并且您不应该在绘制方法中启动计时器,将浮点值转换为int
不会在随机颜色生成中提供任何值。大概
public class RainbowWindow extends JApplet implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel = new JPanel();
@Override
public void actionPerformed(ActionEvent e) {
Color color = new Color(((int) (Math.random() * 255)),
((int) (Math.random() * 255)),
((int) (Math.random() * 255)), 255);
panel.setBackground(color);
}
public RainbowWindow() {
Timer timer = new Timer(1000, this);
timer.start();
add(panel);
setVisible(true);
}
}
我对它进行了测试,它每秒将背景颜色更改为一种新的随机颜色。1。及;2.不要在绘制
中创建计时器
,每次需要绘制组件时都会调用绘制,并且通常是快速连续的;3. <代码>JFrame
!=<代码>小程序
4Math.random
返回一个介于0和1之间的值,Color
需要一个介于0-255I之间的值。我更改了'Color Color=new Color(((int)Math.random()),((int)Math.random(),((int)Math.random()),255);'到“Color Color=new Color(((int)Math.random()*256),((int)Math.random()*256),((int)Math.random()*256),255);”(int)Math.random()*255
将通过截断浮点值(0.1
变为0
),将(int)(Math.random()*255)
的结果从Math.random()
转换为int
,记住,它是0-255
而不是0-256
1。及;2.不要在绘制
中创建计时器
,每次需要绘制组件时都会调用绘制,并且通常是快速连续的;3. <代码>JFrame!=<代码>小程序4Math.random
返回一个介于0和1之间的值,Color
需要一个介于0-255I之间的值。我更改了'Color Color=new Color(((int)Math.random()),((int)Math.random(),((int)Math.random()),255);'到“Color Color=new Color(((int)Math.random()*256),((int)Math.random()*256),((int)Math.random()*256),255);”(int)Math.random()*255
将通过截断浮点值(0.1
变成0
),将(int)(Math.random()*255)
的结果从Math.random()
转换为int
,记住,它是0-255
而不是0-256
小程序终于正式发布了,死了:还有。虽然OP在示例代码中使用了Applet
,但他们确实说了JFrame
。。。所以,这并不令人困惑:丘疹终于正式消失了:而且。虽然OP在示例代码中使用了Applet
,但他们确实说了JFrame
。。。所以,这并不令人困惑:P