📄 beautifulmist.java
字号:
/**程序线程通过使用Timer类控制动画
*Timer类是通过固定频率调用actionPerformed方法
*注意:如果你的编译器是j2sdk1.5.0版本,没有这样的效果
*只能使用j2sdk1.4.x版本。
*/
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.awt.Dimension;
import java.awt.event.*;
public class BeautifulMist extends JFrame implements ActionListener
{
protected static Color color = null;
protected static Dimension dimension = null;
protected static int location_x = 0;
protected static int location_y = 0;
protected static int red = 0;
protected static int green = 0;
protected static int blue = 0;
protected Timer timer = null;
// 构造方法
public BeautifulMist()
{
getContentPane().add(new RectPanel());
// 创建一个Timer对象,该对象以1毫秒的频率调用actionPerformed方法
timer = new Timer(1,this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
// Main 方法
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
BeautifulMist frame = new BeautifulMist();
frame.setTitle("星空晚霞");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
//面板类
class RectPanel extends JPanel
{
public void paintComponent(Graphics g)
{
// 使用随机数产生点的坐标
dimension = getSize();
location_x = (int) (Math.random() * dimension.width);
location_y = (int) (Math.random() * dimension.height);
// 在面板的底部颜色最红
red = (int) (location_y * 255 / dimension.height);
// 在面板的右上角颜色最蓝
blue = (int) (((dimension.height - location_y) * 255 / dimension.height + location_x *255 / dimension.width) / 2);
// 在面板的左上角颜色最绿
green = (int) (((dimension.height - location_y) * 255 / dimension.height + (dimension.width - location_x) * 255 / dimension.width) / 2);
// 设置颜色
g.setColor(new Color(red, green, blue));
// 画点
g.drawLine(location_x,location_y,location_x,location_y);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -