|
|
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
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; }
Here is a screen shot of our applet running in Java's AppletViewer program.
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.
|
|