重绘()不起作用,我不知道为什么

时间:2015-04-15 17:34:21

标签: java image swing repaint

public static void main(String[] args){
    Core game = new Core();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setTitle("Multiply");
    game.frame.setIconImage(testIcon);
    game.frame.add(game);
    game.frame.pack();
    game.frame.setLocationRelativeTo(null);
    game.frame.setResizable(false);
    game.frame.setVisible(true);

    t1.start();

    new Core().run();
}

public Core(){
    KeyListener listener = new MyKeyListener();
    addKeyListener(listener);
    setFocusable(true);

    Dimension size = new Dimension(width, height);  //variable that sets the size of the window to 1280x720p
    setPreferredSize(size);                         //Sets the size of the window to "size" 

    frame = new JFrame();                           //makes frame into the JFrame window
}


public void run(){
    running = true;
    loop();
}

public void render(){
        repaint();
        System.out.println("runnin");
}

private synchronized void loop(){   //Running loop, while the game is open this will always be running
    long lastLoopTime = System.nanoTime();
    final int TARGET_FPS = 60;
    final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
    int fps = 0;
    long lastFpsTime = 0;

    while(running == true){
        long now = System.nanoTime();
        long updateLength = now - lastLoopTime;
        lastLoopTime = now;
        double delta = updateLength / ((double)OPTIMAL_TIME);

        lastFpsTime += updateLength;
        fps++;

        if (lastFpsTime >= 1000000000)
          {
             System.out.println("(FPS: "+fps+")");
             lastFpsTime = 0;
             fps = 0;
          }

        enemyAI();
        render();

        try {
            Thread.sleep( (lastLoopTime-System.nanoTime() + OPTIMAL_TIME)/1000000 );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void enemyAI(){
    if(menu != true){
        enemyX++;
    }
}

@Override
public synchronized void paint(Graphics g) {        // Draws top first to bottom last 
    super.paint(g);
    String imagesString = "C:/Users/BigBertha/Desktop/Multiply_Game_Engine/";
    Image background = Toolkit.getDefaultToolkit().getImage(imagesString+"Images/Green_Background.png");
    Image character = Toolkit.getDefaultToolkit().getImage(imagesString+"Images/Character.png");
    Image enemy = Toolkit.getDefaultToolkit().getImage(imagesString+"Images/Enemy.png");
    Image inDevText = Toolkit.getDefaultToolkit().getImage(imagesString+"Images/InDevText.png");

    Image menuScreen = Toolkit.getDefaultToolkit().getImage(imagesString+"Images/MainMenu.png");
    Image menuArrow = Toolkit.getDefaultToolkit().getImage(imagesString+"Images/MainMenuArrow.png");
    //Image loadingScreen = Toolkit.getDefaultToolkit().getImage(imagesString+"Images/LoadingScreen.png");

    if(menu){
        g.drawImage(menuScreen, 0, 0, 1290, 730, this);

        if(menuAPos1){ //play
            g.drawImage(menuArrow, menuArrowX1, menuArrowY1, 125, 100, this);
            loading = true;
        }
        else if(menuAPos2){
            g.drawImage(menuArrow, menuArrowX2, menuArrowY2, 125, 100, this);
        }
        else if(menuAPos3){
            g.drawImage(menuArrow, menuArrowX3, menuArrowY3, 125, 100, this);
        }
            g.drawImage(inDevText, width - 95, height - 20, 100, 40, this); //removed eventually
    }
else if(running){
        g.drawImage(background, 0, 0, 1290, 730, this); //draws the black background
        g.drawImage(inDevText, width - 95, height - 20, 100, 40, this); //removed eventually
        g.drawImage(character, charX, charY, charSizeX, charSizeY, this); //draws the character
        g.drawImage(enemy, enemyX, enemyY, enemySizeX, enemySizeY, this);
}
}

我需要loop()方法才能运行repaint(),但在调用它时它什么都不做。我尝试了许多不同的变体来尝试让它发挥作用,但没有任何效果。我一直在考虑使用JOGL,但我需要为学校完成至少相当数量的游戏原型。

1 个答案:

答案 0 :(得分:4)

  1. 您对synchronized的使用可能导致冻结。由于run方法永远不会释放监视器,因此无法输入paint。您可以尝试Thread.sleep(long)(确实会释放监视器)而不是wait(long)(不会释放监视器)。

    try {
        wait( (lastLoopTime-System.nanoTime() + OPTIMAL_TIME)
                    / 1000000 );
    } ...
    

    但是你应该考虑是否有必要同步整个run方法。如果游戏循环不经常等待,它可能会导致帧速率问题。

  2. 如果你开始使用game,你不清楚你在哪里开始线程......(这也可能导致问题'什么都不做'你看到了。)

    Core game = new Core();
    ...
    
    t1.start();
    
    new Core().run();
    

    new Core().run()创建第二个不相关的实例,并直接在主线程上调用其run方法。你应该做一些像new Thread(core).run()这样的事情。 (这是t1是什么?)

  3. 您不应该在paintToolkit.getImage的调用)中加载图片。这样做意味着您每帧都要加载每个文件。您应该将资源一次加载到类或实例字段中。

  4. 我们通常不会覆盖Swing程序中的public void paint(Graphics),我们会覆盖protected void paintComponent(Graphics)

  5. 另见