Wednesday, September 30, 2009

Drawing HTML with JOGL Part 2: JComponent

Ok, so now that we are able to use a BufferedImage and create a texture from it, as I showed in the last part, lets move on to showing HTML. Java provides basic HTML rendering, complete with stylesheets so that you can render a majority of webpages, provided they don't require javascript. You'll have to work out all the nuances of using HTML in java yourself.

So on to the code you'll need a JEditorPane or JTextPane, and a container JPanel.
private final JEditorPane editor = new JEditorPane();

private final JPanel container = new JPanel(new BorderLayout());

You can set them to load a particular URL and make sure the editable is set to false.
// setup our HTML render capable component
try {
//get the resource using the classloader which requires
//the file to be in a directory on the classpath.
} catch (IOException e1) {
Or you can hard code the HTML yourself but make sure you've set the editors setContentType to "text/html" or the html will not be recognized.

Now here is the tricky part, as in the last example the texture will be created from the BufferedImage but now you will have to draw the swing component and update the texture data every frame in case you wish to change the contents of the editor pane dynamically.
// paint the html component to a buffered image.
Graphics g = image.getGraphics();

// rerender the editor to capture any changes
SwingUtilities.paintComponent(g, editor, container, 0, 0,
image.getWidth(), image.getHeight());

//need to update the texture itself
texture.updateImage(new TextureData(GL.GL_RGBA,
image.getType(), false, image));

Here is the example in full.
This example uses JOGL version 1.1.1a and should work with the NetBeans plugin you can obtain the version from JOGL's website go to the "Archived Builds" page from the navigation menu on the left. Then find the jsr-231-1.1.1a folder

1 comment:

Post a Comment