恩,估计你是通过在JPanel上getGraphics来得到Graphics对象然后进行画图的吧
建议你扩展JPanel类,直接重写paint(Graphics g)方法就能够解决这个问题了

解决方案 »

  1.   

    只是在JPanel绘图的,主类继承了jpanel。是直接重写paint函数的,paint函数如下:
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2D=(Graphics2D)g;
        g2D.setColor(Color.red);
        g2D.setStroke(new BasicStroke(1.2f));
        g2D.setFont(new Font("宋体",0,15));
        g2D.drawString("My draw program!",0,-10);
        for(int i=0;i<trace.size()-1;i++){
          g2D.drawLine((int)((Point2D.Double)trace.elementAt(i)).getX(),
                       -(int)((Point2D.Double)trace.elementAt(i)).getY(),
                       (int)((Point2D.Double)trace.elementAt(i+1)).getX(),
                       -(int)((Point2D.Double)trace.elementAt(i+1)).getY());
        }
        g2D.setStroke(new BasicStroke(1.0f));
        g2D.setColor(Color.blue);
        g2D.drawLine(0,0,30,-220);
      }
      

  2.   

    在适当的时候使用repaint重画,如何?
      

  3.   

    不知道这个帖子对你有没有帮助
    http://expert.csdn.net/Expert/topic/1682/1682278.xml?temp=.8472864
      

  4.   

    你必须在弹对话框之后显示的调repaint()
      

  5.   

    你应该覆写paintComponent方法,而不是paint方法public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2D=(Graphics2D)g;
        g2D.setColor(Color.red);
        g2D.setStroke(new BasicStroke(1.2f));
        g2D.setFont(new Font("宋体",0,15));
        g2D.drawString("My draw program!",0,-10);
        for(int i=0;i<trace.size()-1;i++){
          g2D.drawLine((int)((Point2D.Double)trace.elementAt(i)).getX(),
                       -(int)((Point2D.Double)trace.elementAt(i)).getY(),
                       (int)((Point2D.Double)trace.elementAt(i+1)).getX(),
                       -(int)((Point2D.Double)trace.elementAt(i+1)).getY());
        }
        g2D.setStroke(new BasicStroke(1.0f));
        g2D.setColor(Color.blue);
        g2D.drawLine(0,0,30,-220);
      }