简体   繁体   English

Java repaint()在浏览器中不起作用

[英]Java repaint() Doesn't Work In Browser

I've made a game in Java which works without any problem when I run it in Eclipse. 我用Java制作了一个游戏,当我在Eclipse中运行它时,它没有任何问题。 Everything looks great and it is effectively done (at least until I come up with something else to do with it). 一切看起来都很不错,并且可以有效完成(至少直到我提出其他相关要求为止)。 So I've been trying to put it on my website, but whenever I run the game in browser I simply get a white screen, though checking the Java console shows no errors. 因此,我一直试图将其放置在我的网站上,但是每当我在浏览器中运行游戏时,我都会得到一个白屏,尽管检查Java控制台不会显示任何错误。 I've managed to narrow the problem down to the painting of the screen. 我设法将问题缩小到屏幕上的绘画。 I have a timer which runs the game and makes stuff happen. 我有一个计时器,可以运行游戏并使事情发生。 At the end of it, it calls the repaint() method. 最后,它调用repaint()方法。 In Eclipse, that works fine, but in the browser, nothing happens. 在Eclipse中,这很好用,但是在浏览器中,什么也没有发生。

Here's the relevant code (All of which is inside the main class called FinalProject): 这是相关的代码(所有代码都在名为FinalProject的主类中):

public class FinalProject extends JApplet implements ActionListener,
                          KeyListener, MouseListener, MouseMotionListener {
    public void init(){
        //...initialize program

        System.out.println("game started");
    }

    /**
     * A method called every so often by a timer to control the game world.
     * Mainly calls other functions to control objects directly, but this 
     * is used as the only timer, which also calls repaint() at it's end.
     */
    private void runGame(){
        //...Run game and do important stuff

        //This Draws The Screen
        System.out.println("about to paint");
        repaint();
    }

    public void paint(Graphics g){
        System.out.println("painting");

        //...paint screen
    }

    public void update(Graphics gr){
        System.out.println("updating");
        paint(gr);
    }
}

runGame() is called by a timer. runGame()由计时器调用。 In Eclipse the output is: 在Eclipse中,输出为:
game started 游戏开始
painting 绘画
painting 绘画
about to paint 即将油漆
painting 绘画
about to paint 即将油漆
painting 绘画
about to paint 即将油漆
painting 绘画
... ...

When doing this in a browser (Running offline directly on my machine. All browsers have the same problem as well), the console shows: 在浏览器中执行此操作时(直接在我的计算机上脱机运行。所有浏览器也存在相同的问题),控制台显示:
...(loading stuff) ...(正在加载东西)
game started 游戏开始
basic: Applet initialized 基本:Applet初始化
basic: Starting applet 基本:启动小程序
basic: completed perf rollup 基本:完成性能汇总
basic: Applet made visible 基本:使小程序可见
basic: Applet started 基本:Applet开始
basic: Told clients applet is started 基本:已启动告诉客户小程序
about to paint 即将油漆
about to paint 即将油漆
about to paint 即将油漆
... ...

I don't know what else to try at this point. 我目前不知道还能尝试什么。 Despite my efforts I still don't fully understand exactly what repaint() does, all I know is that it ultimately calls update() and paint(). 尽管我付出了很多努力,但我仍然不太完全了解repaint()的作用,我所知道的是它最终将调用update()和paint()。 Except that doesn't seem to be happening in the browser. 除非浏览器似乎没有发生这种情况。 I'm using Windows 7 64x with Java Version 7 Update 5. Thanks in advance for any help. 我将Windows 7 64x与Java Version 7 Update 5一起使用。在此先感谢您的帮助。

Turns out, the problem was in removing the menu bar. 原来,问题出在删除菜单栏。 I had found some code a while ago which would remove the menu bar from the program and it worked without any problems. 不久前我发现了一些代码,该代码将从程序中删除菜单栏,并且可以正常工作。 However it seems that it prevented it from repainting when placed in a browser. 但是,当放置在浏览器中时,似乎阻止了重新绘制。 I have no idea why repainting broke because of removing the menu bar, but apparently it does. 我不知道为什么重新粉刷由于删除菜单栏而中断,但显然可以。

The code I had used (in init()): 我使用过的代码(在init()中):

Frame[] frames = Frame.getFrames();
for (Frame frame : frames){
    frame.setMenuBar(null);
    frame.pack();
}

This code did remove the menu bar as desired, but also exploded the program whenever it was put online. 此代码确实删除了所需的菜单栏,但每当程序联机时也会分解该程序。 Removing this fixed the issue. 删除此问题已解决。 Fortunately, the menu bars don't show up online anyways, so you aren't losing much by removing this bit of code. 幸运的是,菜单栏无论如何都不会在线显示,因此删除这些代码不会给您带来太多损失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM