簡體   English   中英

Repaint()無法正常工作,我也不知道為什么

[英]Repaint() isn't working and I don't know why

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. 您對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. 您不應該將圖像加載到paint內部(對Toolkit.getImage的調用)。 這樣做意味着您要每幀加載每個文件。 您應該一次將資源加載到類或實例字段中。

  4. 通常,我們不覆蓋Swing程序中的public void paint(Graphics) ,而是覆蓋protected void paintComponent(Graphics)

另見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM