簡體   English   中英

Java圖形沒有重新繪制

[英]Java graphics isn't repainting

我想添加一個功能,以便當鼠標移過一個按鈕時,它會添加一個投影。 目前我只是想讓機制起作用。 我的游戲循環調用了一個我知道有效的更新方法,但無論如何它在這里

    public void updateManager(double delta){
    mhandler.updateCoordinates();
    if(mhandler.getX() >= 144 && mhandler.getX() <= 444 && mhandler.getY() >= 784 && mhandler.getY() <= 980){
        oversp = true;
    }else{
        oversp = false;
    }
}

mhandler就是我命名的MouseHandler類。 然后我有我的渲染方法

    public void render(){
    repaint();
}

然后我的油漆方法

    public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    if(oversp){
        System.out.println("Is over button");
        g2d.setColor(Color.RED);
        g2d.fillRect(144, 784, 300, 169);
    }else{
        System.out.println("Not over button");
        g2d.setColor(Color.BLACK);
        g2d.fillRect(144, 784, 300, 169);
    }
}

當我運行該程序時,即使我在游戲循環中不斷調用render(),它也只打印出不超過按鈕兩次。 我真的不知道為什么不重新粉刷。 任何幫助都很貼切!

這就是我檢測鼠標坐標的方法

    private int x,y;

public MouseHandler(){
    x = 0;
    y = 0;
}

public void updateCoordinates(){
    PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    x = (int) b.getX();
    y = (int) b.getY();
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

游戲循環代碼

    public static void MenuLoop() {
    long lastLoopTime = System.nanoTime();
    final int TARGET_FPS = 60;
    final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
    long lastFpsTime = 0;
    int fps = 0;
    while (isrunning) {
        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;
        }

        menu.render();
        menu.updateManager(delta);


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

        }
    }

}public static void MenuLoop() {
    long lastLoopTime = System.nanoTime();
    final int TARGET_FPS = 60;
    final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
    long lastFpsTime = 0;
    int fps = 0;
    while (isrunning) {
        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;
        }

        menu.render();
        menu.updateManager(delta);


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

        }
    }

}

Event Dispatch Thread包含一個添加了所有AWT事件的隊列。 每當您調用repaint ,paint事件將在Event Dispatch Thread上排隊。

因此,如果您在事件調度線程中處於無限循環中,那些繪制事件將永遠排在隊列中,等待無限循環結束。 這就是為什么paintComponent永遠不會被調用的原因。

解決方案是用Swing計時器替換無限循環。

Timer timer = new Timer(1000 / TARGET_FPS, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //...
    }
});
timer.start();

暫無
暫無

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

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