我的程序定义了一个panel,每次在鼠标移进这个panel的时候,鼠标上就回显示一个小球,移出的时候小球消失。但是我的程序运行结果什么都不显示,我觉得我的思路没问题,但是在实现的时候可能有问题,请大家看一下程序,多多指教,谢谢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
/*
 * When the mouse move to the panel,there will be a 
 * ball appear  at the same time.when it moves out of the panel
 * the ball disappear.
 */
public class MousewithBall {
   public static void main(String[] args){
  JFrame frame=new DrawBallFrame();
  frame.setSize(300,200);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  
   }
}class DrawBallFrame extends JFrame{
public DrawBallFrame(){
  panel=new DrawBallPanel();
  add(panel);
}

private DrawBallPanel panel;
}class DrawBallPanel extends JPanel{
private ArrayList<Point> points;
public DrawBallPanel(){
   points=new ArrayList<Point>();
   //gets tje bounds of the panel
   r= this.getBounds();
   //add the listener
   addMouseMotionListener(new MouseMotionHandler());
}

public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2=(Graphics2D)g;
for(Point r:points){
  double x=r.getX();
  double y=r.getY();
  //draw the ball
  g2.fillOval((int)x,(int)y,15,15);
}
}

public Point returnPoint(int x,int y){
return new Point(x,y);

public void add(Point p){
points.add(p);
}
private Rectangle r;
private class MouseMotionHandler extends MouseMotionAdapter{
public void mouseMoved(MouseEvent e){
   int x=e.getX();
   int y=e.getY();
   if(r.contains(x,y))
   {returnPoint(x,y);
   repaint();}
}

}
}

解决方案 »

  1.   

    private Rectangle r;
    这个没有初始化值
      

  2.   

    在下面这个构造函数中,r已经初始化了啊,请问楼上的朋友为什么说我没有给r初始化呢?
    public DrawBallPanel(){
       points=new ArrayList<Point>();
       //gets tje bounds of the panel
       r= this.getBounds();
       //add the listener
       addMouseMotionListener(new MouseMotionHandler());
    }
      

  3.   

    JFrame frame=new DrawBallFrame();
      frame.setSize(300,200);
    ---------------------------------------
    你是在下边给设的大小,new的时候还是0
      

  4.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;/*
     * When the mouse move to the panel,there will be a 
     * ball appear  at the same time.when it moves out of the panel
     * the ball disappear.
     */
    public class MousewithBall {
    public static void main(String[] args) {
    JFrame frame = new DrawBallFrame(300,200);
    // frame.setSize(300, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class DrawBallFrame extends JFrame {
    public DrawBallPanel panel;
    public DrawBallFrame(int x,int y) {
    panel = new DrawBallPanel(x,y);
    add(panel);
    }
    }class DrawBallPanel extends JPanel {
    private ArrayList<java.awt.Point> points;
    public Rectangle r;

    public DrawBallPanel(int x,int y) {
    points = new ArrayList<java.awt.Point>();
    // gets tje bounds of the panel
    r = new Rectangle(x,y);
    // add the listener
    addMouseMotionListener(new MouseMotionHandler());
    } public void paintComponent(Graphics g) {
    super.paintComponents(g);
    Graphics2D g2 = (Graphics2D) g;
    for (java.awt.Point r : points) {
    double x = r.getX();
    double y = r.getY();
    // draw the ball
    g2.fillOval((int) x, (int) y, 15, 15);
    }
    System.out.println("paintComponent  "+points);
    } public java.awt.Point returnPoint(int x, int y) {
    System.out.println(x+":  "+y);return new java.awt.Point(x, y); }
    public void add(java.awt.Point p) { points.add(p);}
    private class MouseMotionHandler extends MouseMotionAdapter {
    public void mouseMoved(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    if (r.contains(x,y)) {
    points.clear();
    points.add(e.getPoint());
    repaint();
      }
    // }
    } }
    }
      

  5.   

    points.clear()这句好象没有起到作用,因为在屏幕上鼠标移动的时候是一条大大的粗线
      

  6.   

    points.clear()只是把先前得到的点从arraylist中清除,但是在repaint()的时候,并不能把先前已经画到屏幕上的点清除掉,所以才出现了一连串的黑点以至于形成了粗线。那么请问,怎样才能把先前的这些小球清除掉?为什么repaint()方法没有用呢?谢谢指教