package ght.play.paopaodragon.ui;import ght.play.paopaodragon.core.GameContext;
import ght.play.paopaodragon.module.Module;
import ght.play.paopaodragon.util.GlobalVariable;
import ght.play.paopaodragon.util.Position;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Vector;import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameScreen extends JFrame{

/**
 * 为了去掉警告
 */
private static final long serialVersionUID = 1L;

/*
 * 游戏的屏幕的宽
 */
public final int WIDTH=300;

/*
 * 游戏的屏幕的长
 */
public final int HEIGHT=400;


private final int RESOLUTION=30;


//private GameEngine ge=new GameEngine(0);


public GameScreen(String name){
super(name);



setVisible(true);
setSize(WIDTH,HEIGHT);

getContentPane().add(new GamePanel(),BorderLayout.CENTER);
//setBackground(Color.WHITE);


//setLayout(new BorderLayout());


//setVisible(true);

GlobalVariable.newInstance().
put("CENTER",new Position(150,380));



}

private class GamePanel extends JPanel{


private static final long serialVersionUID = -316237384326279994L;
public void paintComponent(Graphics g) {


super.paintComponent(g);
GlobalVariable.newInstance().put("P",(Graphics2D)g);

//g.fillOval(0,0,100,100);

Vector<Module> allModules=GameContext.i().getModuleContainer().getModules();


for(Module module:allModules){
module.draw();
}

//File file=new File(getClass().getResource("")+"a/ball_black.png");
//ge.renderScreen();
//g.drawImage(new ImageIcon(getClass().getResource("")+"a/ball_black.png").getImage(),40,40,this);
//g.setColor(Color.black);
//g.fillOval((int)(Math.random()*300),0, 50,50);
    }

}



public  void start(){

final GameScreen paopao=this;
//paopao.setLayout(new BorderLayout());
//final  Integer index=new Integer(1);
//paopao.pack();
//final GameEngine ge=new GameEngine(0);
//ge.run();

// paopao.getContentPane().add(new JPanel(){
// /**
//  * 为了删掉警告
//  */
// private static final long serialVersionUID = -5283610764609078407L;
//
// public void paintComponent(Graphics g) {
//
// super.paintComponent(g);
// GlobalVariable.newInstance().put("P",(Graphics2D)g);
//
// //g.fillOval(0,0,100,100);
//
// Vector<Module> allModules=GameContext.i().getModuleContainer().getModules();
//
//
// for(Module module:allModules){
// module.draw();
// }
//
// //File file=new File(getClass().getResource("")+"a/ball_black.png");
// //ge.renderScreen();
// //g.drawImage(new ImageIcon(getClass().getResource("")+"a/ball_black.png").getImage(),40,40,this);
// //g.setColor(Color.black);
// //g.fillOval((int)(Math.random()*300),0, 50,50);
//     }
//
// public void paint(Graphics g){
// g.fillOval(0,0,100,100);
// }
//
//
// },BorderLayout.CENTER);

//GameEngine ge=new GameEngine(0);
//ge.run();


//paopao.setVisible(true);
//ge.register(paopao);
//this.getComponents();


//final GamePanel panel=(GamePanel)getContentPane().getComponent(0);
//panel.repaint();
// final GameScreen s=this;

//panel.paintComponent(panel.getGraphics());

new Thread(new Runnable(){
public void run(){
while(true){

try {

//Thread.sleep(RESOLUTION);
//paopao.paintComponents(paopao.getGraphics());
//paopao.paintComponents(paopao.getGraphics());

while(true){
Thread.sleep(RESOLUTION);
//panel.paintComponent(panel.getGraphics());
update(getGraphics());
}



} catch (Exception e) {

}


}
}).start();


}

public static void main(String[] args){
//GameScreen.start();
}

}程序刚启动的时候图形显示不出来,但只要一调整下窗口的大小,或者最大化,图形就可以显示了。不知道什么问题呀,郁闷死了。请大家赐教.....

解决方案 »

  1.   

    这个问题已经解决了。说出来大家可能不相信,我将eclipse当中的jdk选择成为1.6,并且complier也选择成1.6的话。再次编译运行就可以显示出来了。怪异的很.....
      

  2.   

    楼主,请把JFrame的setVisible(true)放在添加所有组件完成之后,不然显示不出组件是正常现象,这是很多人爱犯的错误,也许JDK1.6做了什么优化吧,但这种规则还是记住的好,你不能把所有希望都放在JDK的改良上。之所以要“调整下窗口的大小,或者最大化,图形就可以显示”,是因为那时候调用了repaint()方法,repaint又调用了paint()方法,paint又调用了paintComponent()方法。
      

  3.   

    建议先把组件添加完以后。最后在setVisible(true);
    我还碰到过一个JDeskTop,有个地方一定要按循序来。不然也有问题
    这种问题很郁闷……