Sign in to follow this  
Ph4tM4N

(Hopefully) Simple issue with an Applet

Recommended Posts

[font=arial, verdana, tahoma, sans-serif][size=2]



So I'm trying to take a game I made and package it into an applet, which, I gather, is a fairly simple process. The programming I did is all in native java using AWT components (the game is drawn to a Canvas object), so I just extracted an interface from my GameWindow class and called it AppletWindow, and made it contain a canvas, and function exactly as the GameWindow class. What I did runs fine in Eclipse, but does not function when embedded in an html file (penny for every time eclipse runs something that wont run outside eclipse...). I'm going to edit this post with thorough details, but while I tinker with it a while I thought I'd make a quick post in case this is some simple derp I'm not aware of regarding applets.[/size][/font]

What happens is when you load the html file (currently on my desktop, and the jar is sitting next to it) the html file loads properly, and has the applet embedded in it, with the little java cup and the swirley animation around it, and an empty loading bar, which never progresses. I would assume this has something to do with the applet not starting correctly, but when I activated the java console to take a look at some of my debug output, it appears the game launches and enters the game loop as one would expect a properly functioning program to behave.

Is this possibly a symptom of the canvas not properly being displayed in the applet window? It clearly works just fine in eclipse, so I can't see anything obvious about what's going wrong here. I've tried opening the html file in chrome, firefox, and IE8 to no avail.

Thanks for any help anyone can offer! I'll update with some pics and source of the issue shortly.

EDIT: relevant code follows. Screenshots of applet not working and working in eclipse as well.

[code]public class AppletWindow extends Applet {
private GameWindowCallback callback;
private int width;
private int height;
private BufferStrategy strategy;
private Graphics2D g;
private Canvas drawArea;

public void startRendering() {
System.out.println("Starting rendering");
if(strategy == null){
setPreferredSize(new Dimension(width, height));
this.setSize(new Dimension(width, height));
this.setIgnoreRepaint(true);
this.setVisible(true);
drawArea = new Canvas();
drawArea.setBounds(0, 0, width, height);
add(drawArea);
this.validate();
drawArea.setIgnoreRepaint(true);

drawArea.createBufferStrategy(2);
strategy = drawArea.getBufferStrategy();

mouse = new Mouse();
keyboard = new Keyboard();
drawArea.addMouseListener(mouse);
drawArea.addMouseMotionListener(mouse);
drawArea.addKeyListener(keyboard);
drawArea.requestFocus();
}

if (callback != null)
callback.initialize();
gameLoop();
}
private void gameLoop() {
System.out.println("Game Loop");
while (gameRunning) {
if(lastRender + 10 > System.currentTimeMillis())
continue;
g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, width, height);

if (callback != null)
callback.frameRendering();

g.dispose();
strategy.show();
}
}
public void init() {
game.graphics.ResourceManager.setWindow(this);
new TestApplet(this);
}
public void start() {
gameRunning = true;
}
public void stop() {
gameRunning = false;
}[/code]

Anything present in TestApplet shouldn't change anything. It's just a simple applet that only renders a single entity (the smug face) and moves it to wherever the mouse is if the mouse button is held down, and, as you can see in the screenshots, indicates that the entity has been moved.

While the game was running in chrome, I looked in the java console once again, and it is running. Observing a few stack traces yielded a lot of stuff to dig through, but probably the only relevant parts are that the program spends most of its time in line 125 (buried about 10 function calls deep, line 125 is in gameLoop, "strategy.show();") though I did manage to catch it twice on line 116 ("g.fillRect(0,0,width,height);".

Here are two pictures. The first is of the game running in the ecclipse applet loader as expected. In the console you can see the rendering begin, and the game loop start, and some relocation messages.
[attachment=4153:smallapplettest.jpg]


In the second, you can see the html file loaded(appletTest.html) and displaying properly, but the applet not displaying properly. Note the proper debug text in the console. A stack trace a few times verified that the game is indeed running (each one yielded a different state in a call, things are actually going on, it's not frozen somewhere).
[attachment=4154:smallrunning.jpg]


Oh, and here's the contents of the html file, if that matters to anyone:
[code]<Html>
<Head>
<Title>Applet Test</Title>
</Head>

<Body bgcolor="lightgray">
ROOBY ROOOOOO<br>
Applet Test follows:<br>
<br>
<Applet Code="game.engine.AppletWindow.class" archive="testappplet.jar" width=400 Height=400>
</Applet>
</Body>
</Html>[/code]

Yes, testappplet.jar has 3 p's in it. I typo'd it and decided to be lazy and just reproduce it in the filename rather than open and edit the html file. I could have even done it just now, but I didn't.

So I have no idea how the applet could possibly be running but not displaying properly, I'm certain it has something to do with how I'm adding the canvas to the applet not properly leading to it being displayed. I've tried the internet, but noone seems to be very helpful on this subject (can't find anyone else with the same problem, and most articles on applets come from the early 12th century).

Sorry for the massive wall of text, I'm just trying to be thorough about providing information about the problem. I really just hope someone comes in and says "right after add(drawArea); add this line of code XXX" and that fixes it, but I'm sure there's some deeper problem I've gone and screwed myself with.

Thanks again,
Skoob

Share this post


Link to post
Share on other sites
isn't there a draw function of some sort that should be getting called or paint or repaint or something? I'm not much for java so I wouldn't completely know.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this