日期:2014-05-20  浏览次数:20572 次

一个奇怪的问题,为什么我画不出图片来?
Java code

import java.awt.*;
import java.awt.event.*;



public class TankClient extends Frame{

    private static int GAME_WIDTH = 800;
    private static int GAME_HEIGHT = 600;
    
    public static void main(String[] args) {
        new TankClient().launchFrame();
    }
    

    Tank myTank = new Tank(50,50);  
    
    
    public void launchFrame(){
        Frame f = new Frame();
                  f.setBounds(222, 120, GAME_WIDTH, GAME_HEIGHT);
        f.setBackground(Color.GREEN);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }    
        });
        f.setResizable(false);
        f.setVisible(true);        
    }
    
    public void paint(Graphics g) {
        g.setColor(Color.GREEN);
        g.fillOval(30, 30, 50, 50);    
}

}




------解决方案--------------------
在launchFrame方法里调用一下repain方法试试
------解决方案--------------------
因为你显示的frame和你画图的frame不是同一个frame
你应该这样写
Java code

import java.awt.*;
import java.awt.event.*;



public class TankClient extends Frame{

    private static int GAME_WIDTH = 800;
    private static int GAME_HEIGHT = 600;
    
    public static void main(String[] args) {
        new TankClient();
    }
    

    //Tank myTank = new Tank(50,50);  
    
    
    public TankClient(){
        //Frame f = new Frame();
        setBounds(222, 120, GAME_WIDTH, GAME_HEIGHT);
        setBackground(Color.GREEN);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }    
        });
        setResizable(false);
        setVisible(true);        
    }
    
    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.fillOval(30, 30, 50, 50);    
}

}