import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HE extends JFrame
{
public HE()
{
showJPanel panel =new showJPanel();
//setLayout(new BordeLaout());
add(panel);
}
public static void main(String[] args)
{
HE frame = new HE();
frame.setTitle("showJFrame");
frame.setSize(310,410);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//面板类
class showJPanel extends JPanel
{
private int currentX = 1 ,currentY = 1,rX = 10,rY = 10;
private int cr = 255,cg = 0,cb = 0;
int count = 0;
public showJPanel()
{
addMouseMotionListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
currentX = e.getX();
currentY = e.getY();
double L = (currentX-rX)*(currentX-rX)+(currentY-rY)*(currentY-rY);
if(Math.sqrt(L)<10)
{
rX = (int)(Math.random()*300)+10; //随机显示圆
rY = (int)(Math.random()*400)+10;
cr = (int)(Math.random()*255); //随机确定颜色
cg = (int)(Math.random()*255);
cb = (int)(Math.random()*255);
repaint();
}
L = 0;
}
});
}
//覆盖父类方法
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(rX,rY ,10,10);
Color c = new Color(cr,cg,cb);
g.setColor(c);
g.fillOval(rX,rY,10,10);
}
}