📄 circletest.java
字号:
/**
程序功能:在框架上动态的画出水波
*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.Timer.*;
public class CircleTest
{
public static void main(String[] args)
{
CircleFrame frame=new CircleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
// 主框架类
class CircleFrame extends JFrame
{
public CircleFrame()
{
setTitle("ButtonFrame");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
Container content=getContentPane();
CirclePanel panel=new CirclePanel();
content.add(panel);
}
public static final int DEFAULT_WIDTH=400;
public static final int DEFAULT_HEIGHT=400;
}
//主画板类
class CirclePanel extends JPanel
{
public CirclePanel()
{
JButton closeButton=new JButton("quite");
add(closeButton);
closeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
);
}
public void paintComponent(Graphics g)
{
//Timer计时器
ActionListener listener=new ActionPerformed();
Timer t=new Timer(1000,listener);
t.start();
}
//内部类实现Timer计时器接口,动态画出水波
private class ActionPerformed implements ActionListener
{
int topx=175;
int topy=175;
int r=50;
public void actionPerformed(ActionEvent event)
{
Graphics g=getGraphics(); //用getGraphics()方法获得g对象
Graphics2D g2=(Graphics2D)g;
//画水波
Ellipse2D circle=new Ellipse2D.Double(topx,topy,r,r);
g2.draw(circle);
topx=topx-10;
topy=topy-10;
r=r+20;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -