📄 beautifulgrid.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 BeautifulGrid extends JFrame implements ActionListener
{
protected static Point[] point = new Point[2];
protected static Point[] span = new Point[2];
protected static Color color = null;
protected static int red = 0;
protected static int green = 0;
protected static int blue = 0;
protected Timer timer = null;
// 构造方法
public BeautifulGrid()
{
point[0] = new Point(50,50);
point[1] = new Point(550,400);
span[0] = new Point(3,5);
span[1] = new Point(-5,-3);
getContentPane().add(new RectPanel());
// 创建一个Timer对象,该对象以50毫秒的频率调用actionPerformed方法
timer = new Timer(50,this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
//面板类
class RectPanel extends JPanel
{
public void paintComponent(Graphics g)
{
green = point[1].x%255;
blue = point[1].y%255;
g.setColor(new Color(255, green, blue));
g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y);
for(int i=0; i<2; i++)
{
point[i].x += span[i].x;
point[i].y += span[i].y;
if(point[i].x <= 5 || point[i].x >= 630) span[i].x = -span[i].x;
if(point[i].y <= 5 || point[i].y >= 490) span[i].y = -span[i].y;
}
}
}
//坐标点类
class Point
{
protected int x =0;
protected int y =0;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
// Main 方法
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
BeautifulGrid frame = new BeautifulGrid();
frame.setTitle("顶尖设计师杰作--多彩的网格世界");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640,500);
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -