Java的图形是不重新粉刷 [英] Java graphics isn't repainting

查看:230
本文介绍了Java的图形是不重新粉刷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在功能添加,这样当鼠标在一个按钮上,然后它会增加阴影。目前,我只是试图让机械师的工作。我有我的游戏循环调用更新方法,我知道的作品,但在这里它是无论如何

 公共无效updateManager(双三角翼){
    mhandler.updateCoordinates();
    如果(mhandler.getX()> = 144&放大器;&放大器; mhandler.getX()&下; = 444&放大器;&放大器; mhandler.getY()> = 784&放大器;&放大器; mhandler.getY()&下; = 980 ){
        oversp = TRUE;
    }其他{
        oversp = FALSE;
    }
}

mhandler是我叫我MouseHandler类。
然后我有我的渲染方法

 公共无效渲染(){
    重绘();
}

然后我paint方法

 公共无效的paintComponent(图形G){
    Graphics2D的G2D =(Graphics2D的)克;    如果(oversp){
        的System.out.println(结束键);
        g2d.setColor(Color.RED);
        g2d.fillRect(144,784,300,169);
    }其他{
        的System.out.println(不在按钮);
        g2d.setColor(Color.BLACK);
        g2d.fillRect(144,784,300,169);
    }
}

当我有史以来运行该程序只打印出不在按钮两次,甚至当我不断地调用render()在我的游戏循环。我真的不知道为什么它不重新粉刷。任何帮助非常appriciated!

这是我发现我的鼠标坐标

 私人诠释的x,y;公共MouseHandler(){
    X = 0;
    Y = 0;
}公共无效updateCoordinates(){
    PointerInfo一个= MouseInfo.getPointerInfo();
    点b = a.getLocation();
    X =(int)的b.getX();
    Y =(INT)b.getY();
}公众诠释的getX(){
    返回X;
}公众诠释的getY(){
    返回是;
}

游戏循环code

 公共静态无效MenuLoop(){
    长lastLoopTime = System.nanoTime();
    最终诠释TARGET_FPS = 60;
    最终长OPTIMAL_TIME = 10亿/ TARGET_FPS;
    长lastFpsTime = 0;
    INT FPS = 0;
    而(isrunning){
        长今= System.nanoTime();
        长updateLength =现在 - lastLoopTime;
        lastLoopTime =现在;
        双三角翼= updateLength /((双)OPTIMAL_TIME);        lastFpsTime + = updateLength;
        FPS ++;        如果(lastFpsTime> = 1000000000){
            的System.out.println((FPS:+ FPS +));
            lastFpsTime = 0;
            FPS = 0;
        }        menu.render();
        menu.updateManager(增量);
        尝试{
            视频下载((lastLoopTime - System.nanoTime()+ OPTIMAL_TIME)/ 1000000);
        }赶上(例外五){        }
    }}公共静态无效MenuLoop(){
    长lastLoopTime = System.nanoTime();
    最终诠释TARGET_FPS = 60;
    最终长OPTIMAL_TIME = 10亿/ TARGET_FPS;
    长lastFpsTime = 0;
    INT FPS = 0;
    而(isrunning){
        长今= System.nanoTime();
        长updateLength =现在 - lastLoopTime;
        lastLoopTime =现在;
        双三角翼= updateLength /((双)OPTIMAL_TIME);        lastFpsTime + = updateLength;
        FPS ++;        如果(lastFpsTime> = 1000000000){
            的System.out.println((FPS:+ FPS +));
            lastFpsTime = 0;
            FPS = 0;
        }        menu.render();
        menu.updateManager(增量);
        尝试{
            视频下载((lastLoopTime - System.nanoTime()+ OPTIMAL_TIME)/ 1000000);
        }赶上(例外五){        }
    }}


解决方案

事件指派线程包含其中加入了所有AWT事件队列。每当你叫重绘,油漆的事件将在事件指派线程进行排队。

所以,如果你在一个无限循环,而在事件指派线程,这些油漆事件将站在永远在线,等待无限循环结束。这就是为什么的paintComponent 永远不会被调用。

该解决方案将与一个秋千来代替无限循环计时器

 定时器定时器=新定时器(1000 / TARGET_FPS,新的ActionListener(){
    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        // ...
    }
});
timer.start();

I want to add in a feature so that when the mouse goes over a button then it will add a drop shadow. Currently I'm just trying to get the mechanics to work. I have my game loop calling a update method which I know works but here it is anyway

    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 is what I named my MouseHandler class. then i have my render method

    public void render(){
    repaint();
}

and then my paint method

    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);
    }
}

When ever i run the program it only prints out not over button twice even when i am constantly calling render() in my game loop. I really do not know why it is not repainting. any help is very appriciated!

This is how I detect my coordinates of mouse

    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;
}

Game loop code

    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) {

        }
    }

}

解决方案

The Event Dispatch Thread contains a queue to which all AWT events are added. Whenever you call repaint, a paint event will be queued on the Event Dispatch Thread.

So if you're in an infinite loop while on the Event Dispatch Thread, those paint events will stand in line forever, waiting for the infinite loop to end. That's why paintComponent is never called.

The solution would be to replace the infinite loop with a Swing timer.

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

这篇关于Java的图形是不重新粉刷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆