Let s See That in an Applet

Let's See That in an Applet

The applet conversion of the TemplateGraphicsApplication is quite clear-cut. The applet still has a displayable area that you are able to draw to via its paint method, but we do not need to bother with any resizing, as the applet does not contain any borders to worry about. We are left with a rectangular canvas to which we can draw and add functionality embedded in a web page, just like a picture. Here is the code for TemplateGraphicsApplet.java. (In the previous chapter we discussed how to get applets running in the browser and using AppletViewer.)

Code Listing 9-2: TemplateGraphicsApplet.java

start example
import javax.swing.*; import java.awt.*;   public class TemplateGraphicsApplet extends JApplet {     public void init()     {         getContentPane().setLayout(null);         setSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);     }          public void paint(Graphics g)     {         Graphics2D g2D = (Graphics2D)g;              g2D.setColor(Color.blue);         g2D.fill(getBounds());              g2D.setColor(Color.white);         g2D.fillRect(getWidth()/4, getHeight()/4, getWidth()/2,              getHeight()/2);     }          private static final int DISPLAY_WIDTH = 400;     private static final int DISPLAY_HEIGHT = 400; }
end example

Here is a screen shot of our applet running in Java's AppletViewer program.

click to expand
Figure 9-3:

We outlined how to get an applet running in AppletViewer (and a web browser) in the previous chapter, so refer to that chapter if you have any problems running your applets.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net