简体   繁体   中英

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. 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. 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. In Eclipse, that works fine, but in the browser, nothing happens.

Here's the relevant code (All of which is inside the main class called 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. In Eclipse the output is:
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
basic: Starting applet
basic: completed perf rollup
basic: Applet made visible
basic: Applet started
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(). 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.

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()):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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