import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Draw extends JFrame {
  int width, height, radius;
 int X, Y, moveX, moveY;
 Graphics g;
 Image buffer;  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();  //Construct the frame
  public Draw() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    width = getWidth();
    height = getHeight();
    X = 30;
    Y = 50;
    moveX = 1;
    moveY = 1;
    radius = 20;    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
  }  protected void paintComponent(Graphics g) {
    buffer = createImage(width, height);
    g = buffer.getGraphics();    if (X >= (width - radius)) {
      X = width - radius;
      moveX = -moveX;
    }    if (X <= 0) {
      X = 0;
      moveX = -moveX;
    }    if (Y >= (height - radius)) {
      Y = height - radius;
      moveY = -moveY;
    }    if (Y <= 0) {
      Y = 0;
      moveY = -moveY;
    }    X += moveX;
    Y += moveY;
    g.setColor(Color.blue);
    g.drawOval(X, Y, radius, radius);
    g.drawImage(buffer, 0, 0, null);  }  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
}画面无任何显示,如何解决?

解决方案 »

  1.   

    我是隔壁贴的,支持一下 UnAgain() ( ) 信誉:98  2006-7-29 22:00:59  得分: 0  
    关于双缓冲,你可以参考一下这篇文章--“AWT和Swing中的绘画”(http://blog.csdn.net/UnAgain/archive/2006/05/13/727474.aspx)
      

  2.   

    swing里有个属性,并不用自己实现双缓冲,你查一下API我记不清楚了。而且设置后,系统会根据实际效率自动选择双缓存还是翻页模式frame.createBufferStrategy(2);    //生成缓存的数量,有可能使用翻页技术所以至少2
    BufferStrategy staategy=frame.getBufferStrategy();
    Graphics g=stategy.getDrawGraphics();
    draw(g);
    g.dispose();
    startegy.show;
    大致是这样个过程,你再看看API上怎么说的。(需要JDK1.4 UP)
      

  3.   

    而且你的代码有问题
     g.drawImage(buffer, 0, 0, null);
    这里的g是哪个?如果实现双缓存的话,这里应该调用Pane的g,而你这里似乎是buffer的呀
      

  4.   

    g = buffer.getGraphics(); 这句把原来的g参数给覆盖了, 换个名字。
      

  5.   

    多谢诸位解答不过,我依然不甚明了我想在JFrame里画图,不知是否可行?我将代码修改如下,依然无任何显示```
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class draw extends JFrame {
      int width, height, radius;
     int X, Y, moveX, moveY;
     Graphics g,bufferg;
     Image buffer;  JPanel contentPane;
      BorderLayout borderLayout1 = new BorderLayout();  //Construct the frame
      public draw() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception  {
        width = getWidth();
        height = getHeight();
        X = 30;
        Y = 50;
        moveX = 1;
        moveY = 1;
        radius = 20;    contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(borderLayout1);
        this.setSize(new Dimension(400, 300));
        this.setTitle("-----Draw Test-----");
        g = this.getGraphics();
      }  protected void paintComponent(Graphics g) {
        g.drawOval(100,100,20,20);
        buffer = createImage(width, height);
        bufferg = buffer.getGraphics();    if (X >= (width - radius)) {
          X = width - radius;
          moveX = -moveX;
        }    if (X <= 0) {
          X = 0;
          moveX = -moveX;
        }    if (Y >= (height - radius)) {
          Y = height - radius;
          moveY = -moveY;
        }    if (Y <= 0) {
          Y = 0;
          moveY = -moveY;
        }    X += moveX;
        Y += moveY;
        bufferg.setColor(Color.blue);
        bufferg.drawOval(X, Y, radius, radius);
        g.drawImage(buffer, 0, 0, this);  }  //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
    }
    还请诸位多指教!