我画图基本每次都闪过一下就没有了,幸运的时候能看见图形然后只要我的dos窗口覆盖有图形的地方移开后图形就不见了。
以下是测试代码:import javax.swing.*;
import java.awt.*;
public class testGraphics extends JFrame{
private JPanel jpanel;
    public testGraphics(){
     super("testGraphics");
     setSize(400,400);
     jpanel=new JPanel();
    // jpanel.setBackground(Color.WHITE);
     this.setContentPane(jpanel);
    }
    public void drawGraphics(){
     Graphics g=jpanel.getGraphics();
     int gWidth=jpanel.getWidth();
     int gHeight=jpanel.getHeight();
     log("绘图区长:"+gWidth+"绘图区宽:"+gHeight);
     log("会至直线,三维矩形,弧线,椭圆等几何图形");
     g.drawLine(0,0,50,50);
     g.drawLine(0,100,50,50);
     g.drawLine(0,100,50,150);
     g.drawRect(60,30,100,30);
     g.draw3DRect(60,160,100,30,true);
     g.drawRoundRect(60,200,100,40,10,10);
     g.drawArc(200,50,10,8,0,45);
     g.drawOval(200,150,80,80);
     log("写字符串");
     g.drawString("this is a test",200,250);
     log("设置图形颜色为红色");
     Color c=Color.RED;
     g.setColor(c);
     log("填充椭圆");
     g.fillOval(200,100,100,40);
     g.setColor(Color.PINK);
     g.fill3DRect(60,80,100,30,true);
     log("设置绘图颜色为蓝色");
     g.setColor(Color.BLUE);
     log("填充弧");
     g.fillArc(200,200,100,80,0,45);
     log("设置绘图字体");
     Font f=new Font("Monospaced",Font.ITALIC,60);
     g.setFont(f);
     g.drawString("hello",10,250);
     //g.drawOval(300,50,20,20);
    }
    public void log(String s){
     System.out.println(s);
    }
    public static void main(String args[]){
     testGraphics fg=new testGraphics();
     fg.show();
     fg.drawGraphics();
    }
}不知道这是怎么回事呀???
解答者全分

解决方案 »

  1.   

    复写paint让系统自动调度而不是自己写额外的手工调用的函数
      

  2.   


    ...................    public void paint(Graphics g){
         drawGraphics();
        }
        public static void main(String args[]){
         testGraphics fg=new testGraphics();
         fg.show();
         //fg.drawGraphics();
         fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         fg.setVisible(true);
        }
      

  3.   

    /*
     * 创建日期 2005-8-22
     *//**
     * @author Administrator
     * TODO
     */
    import javax.swing.*;
    import java.awt.*;
    public class testGraphics extends JFrame{
    private JPanel jpanel;
        public testGraphics(){
         super("testGraphics");
         setSize(400,400);
         jpanel=new JPanel();
        // jpanel.setBackground(Color.WHITE);
         this.setContentPane(jpanel);
        }    public void paint(Graphics g){   //注意        super.paint(g);
         int gWidth=jpanel.getWidth();
         int gHeight=jpanel.getHeight();
         log("绘图区长:"+gWidth+"绘图区宽:"+gHeight);
         log("会至直线,三维矩形,弧线,椭圆等几何图形");
         g.drawLine(0,0,50,50);
         g.drawLine(0,100,50,50);
         g.drawLine(0,100,50,150);
         g.drawRect(60,30,100,30);
         g.draw3DRect(60,160,100,30,true);
         g.drawRoundRect(60,200,100,40,10,10);
         g.drawArc(200,50,10,8,0,45);
         g.drawOval(200,150,80,80);
         log("写字符串");
         g.drawString("this is a test",200,250);
         log("设置图形颜色为红色");
         Color c=Color.RED;
         g.setColor(c);
         log("填充椭圆");
         g.fillOval(200,100,100,40);
         g.setColor(Color.PINK);
         g.fill3DRect(60,80,100,30,true);
         log("设置绘图颜色为蓝色");
         g.setColor(Color.BLUE);
         log("填充弧");
         g.fillArc(200,200,100,80,0,45);
         log("设置绘图字体");
         Font f=new Font("Monospaced",Font.ITALIC,60);
         g.setFont(f);
         g.drawString("hello",10,250);
        }
        
        public void log(String s){
         System.out.println(s);
        }
        public static void main(String args[]){
         testGraphics fg=new testGraphics();
         fg.repaint();//注意
         fg.show();
        }
    }
      

  4.   

    最后main函数应该这样写,其他的就和楼上的一样!
    public static void main(String args[]){
         testGraphics fg=new testGraphics();
         fg.repaint();
         fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         fg.setVisible(true);
        }
    show已经是被淘汰的方法!