public class ChessBoard extends JPanel implements MouseListener
{      
        // 在坐标(x,y)画一个半径为r的以color颜色填充的圆
        public void PutCircle(int x, int y, int r, Color color)
        {
            Graphics g = getGraphics();
            g.setColor(color);
            g.fillOval(x, y, r, r);
        }             
        public void mouseClicked(MouseEvent evt) 
        {       
            if(evt.getButton() == MouseEvent.BUTTON1)
            {                              
                 PutCircle(300, 300, 30, Color.white);                                               
            }
     
        }// End of mouseClicked()
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
    }// End of ChessBoard Class 
}// End of Main Class在程序窗口单击鼠标时 执行PutCircle(int x, int y, int r, Color color)函数 编译可以通过 但是执行到
g.setColor(color);
g.fillOval(x, y, r, r);
这两句时产生异常:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at RunFrame$ChessBoard.PutCircle(RunFrame.java:303)
        at RunFrame$ChessBoard.mouseClicked(RunFrame.java:342)
        at java.awt.Component.processMouseEvent(Component.java:5491)
        at java.awt.Component.processEvent(Component.java:5253)
        at java.awt.Container.processEvent(Container.java:1966)
        at java.awt.Window.processEvent(Window.java:1161)
        at java.awt.Component.dispatchEventImpl(Component.java:3955)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Window.dispatchEventImpl(Window.java:1774)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
怎么调啊

解决方案 »

  1.   

    Graphics g = getGraphics();似乎应该是这里没有取得Graphics对象……
    我也一直没有正确得到过Graphics对象,但是如果覆盖JPanel的paint(Graphics g)方法就可以实现楼主的目的
      

  2.   

    我在paint(Graphics g)里实现了一个界面 想再用一个函数来向屏幕上绘制图形 这个方法应该可一 跟踪了一下发现g的值为null 不知道为什么
      

  3.   

    查到了一份说明:
    Creates a graphics context for this component. This method will return null if this component is currently not displayable. 可是我已经做过JPanel.SetVisabel();了
      

  4.   

    这个程序为什么就可以
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import javax.swing.*;public class test003 extends Frame implements MouseListener
    {
    static int x1,y1,x2,y2;
    static int status = 0;
    public static void main(String args[])
    {
    test003 frm = new test003();
    frm.setTitle("交互式绘图");
    frm.setBounds(410,410,250,200);
    frm.addMouseListener(frm);
    //frm.setVisible(true);
    }public void mousePressed(MouseEvent e)
    {
    x1 = e.getX();
    y1 = e.getY();
    Graphics g = getGraphics();
    status++;
    if(status == 1)
    {
    g.drawLine(x1,y1,x1,y1);
    x2=x1;y2=y1;
    }
    else if(status == 2)
    {
    g.drawLine(x1,y1,x2,y2);
    status = 0;
    }
    }public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    }
      

  5.   

    第二次得到Graphics 对象应该是因为传进来的参数是鼠标事件,getGraphics()方法得到鼠标的坐标为当前图形;源程序里没得到Graphics 对象是因为传进来的参数只是int型参数,与图形无关
      

  6.   

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    这句话已经说明问题了,具体问题自己仔细查找一下,可以单步调一下