为什么我的屏幕上什么都没有画?

时间:2013-08-19 18:26:12

标签: java eclipse swing draw paint

我正在使用java中的swing进行2D平台游戏,我正在开发游戏框架。我正在测试reapaint()和绘制函数,但它们不起作用,我无能为力。这是我的代码:

Window.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Window extends JFrame {

public Window() {

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(1000, 1000);
    this.setVisible(true);
    this.setTitle("Infiltrator");
    this.setLocationRelativeTo(null);
    this.setContentPane(new Framework());
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() 
    { 
    @Override
    public void run() 
    { new Window(); } 
    }
                                             );

}





}

Panel.java

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public abstract class Panel extends JPanel {

public Panel() {

this.setDoubleBuffered(true);
this.setFocusable(true);

/*
 * 
if(false)
{
    BufferedImage blankCursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
    Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(blankCursorImg, new Point(0, 0), null);
    this.setCursor(blankCursor);
}
*
*/
}

public static void Draw(Graphics2D g2d) {

}

@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;        
super.paintComponent(g2d);        
Framework.Draw(g2d);    
}

}

Framework.java

import java.awt.Color;
import java.awt.Graphics2D;

import javax.swing.JFrame;

public class Framework extends Panel {

private static long beginTime, takenTime, sleepTime;
private static long secInNano = 1000000000L;
private static long secInMilli = 1000000L;
private static long fps = 60L;
private static long fpsTime = secInNano / fps;

public static enum GameState {STARTING, MAINMENU, PLAYING, OVER};
public static GameState gameState;

public Framework ()
{
    super();

    Thread gameThread = new Thread() {
        @Override
        public void run(){
            mainLoop();
        }
    };

    gameState = GameState.STARTING;

    gameThread.start();
}

private void mainLoop() {

    while (gameState != GameState.OVER) {

        beginTime = System.nanoTime();

        switch (gameState) {
            case STARTING:  
                LoadandInit();
                break;
            case MAINMENU: 
                MainMenu.Update();
                break;
            case PLAYING: 

                break;
            default:
                System.out.println("Ok, somebody messed up.");
                break;

        }

        repaint();

        takenTime = System.nanoTime() - beginTime;
        sleepTime = (fpsTime - takenTime) / secInMilli;
        if (sleepTime < 10)
            sleepTime = 10;

        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException ex) { }


    }


}



private static void LoadandInit() {

    gameState = GameState.MAINMENU;
}


public static void Draw(Graphics2D g2d) {

        g2d.setColor(Color.BLACK);
        g2d.drawString("DEARP", 0, 0);
}

}

2 个答案:

答案 0 :(得分:2)

您正在向Window添加Framework实例,但框架不会覆盖paintCompontents。它有一个静态Draw方法,由Panel调用,但你永远不会创建一个面板实例。

您可以将Draw方法转换为paintComponent(),并删除您的Panel类。

此外,您不应该在Framework的构造函数内启动您的线程。这可能导致run方法查看成员变量的错误值。创建一个启动线程的新启动方法。

答案 1 :(得分:-1)

我会推荐Java中杀手游戏编程的第2章。您可以在本书的网站上阅读该章的草稿:

http://fivedots.coe.psu.ac.th/~ad/jg/ch1/index.html