/*
 * ClassDemol.java
 *
 * Created on 2007年4月5日, 下午3:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */package hhh;import java.awt.Graphics;/**
 *
 * @author Administrator
 */
public class ClassDemol {
    
    /** Creates a new instance of ClassDemol */
    public ClassDemol() {
    }    private int x;    private int y;    private int width;    private int height;    public void init() {
        width = 60;
        height = 60;
    }    public void SetPosition(int xPos, int yPos) {
        x=xPos;
        y=yPos;
    }    public void paint(Graphics g) {
        SetPosition(20,20);
        g.drawRect(x,y,width,height);
        g.drawString("1的位置"+x,20,100);
        g.drawString("1的位置"+y,20,120);
                               
        g.drawRect(x,y,width,height);
        g.drawString("2的位置"+x,170,100);
        g.drawString("2的位置"+y,170,120);
        
    }
    
    
}
/*
 * Main.java
 *
 * Created on 2007年4月2日, 上午8:19
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */package hhh;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*; /**
 *
 * @author Administrator
 */
public class Main extends Frame{
    
    /** Creates a new instance of Main */
    public Main() {
        super("drawOval Example"); 
        setSize(200, 200); 
        show(); 
    }
        /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Main m = new Main();
        ClassDemol c = new ClassDemol();
        c.init();
        c.paint(m.getGraphics());    }
    
}
程序可以运行,但是矩形出不来,只出来窗体。
请大家告诉我,谢谢!

解决方案 »

  1.   

    我的感觉:是不是由于调用c.paint()的时候,其实窗体并没有显示完整,那么等到窗体完全显示出来之后又做了自动刷新,导致画上去的内容丢失了?试着override Frame.paint()方法,将你的图形绘画过程加到里面看看。
      

  2.   

    CORE JAVA 的图形编程那一章里面有,椭圆。矩形都有。JAVA有直接的函数。不用这么麻烦的。
      

  3.   

    override Frame里面的paint()方法是王道啊
      

  4.   

    import java.awt.Graphics;import java.awt.Frame;
    import java.awt.Panel;
     class ClassDemo extends Panel {
        
        
        public ClassDemo() {
        }    private int x;    private int y;    private int width;    private int height;    public void init() {
            width = 60;
            height = 60;
        }    public void SetPosition(int xPos, int yPos) {
            x=xPos;
            y=yPos;
        }    public void paint(Graphics g) {
            SetPosition(20,20);
            g.drawRect(x,y,width,height);
            g.drawString("1µÄλÖÃ"+x,20,100);
            g.drawString("1µÄλÖÃ"+y,20,120);
                                   
            g.drawRect(x,y,width,height);
            g.drawString("2µÄλÖÃ"+x,170,100);
            g.drawString("2µÄλÖÃ"+y,170,120);
            
        }
        
        
    }
      public class Main extends Frame{
        
        public Main() {
            super("drawOval Example"); 
            setSize(200, 200); 
            show(); 
        }
            public static void main(String[] args) {
            Main m = new Main();        ClassDemo c = new ClassDemo();
            c.init();
           m.add(c);
        
        }
        
    }帮你改了下,