HeavyLight.java

Download HeavyLight.java

 1: /*
 2: <APPLET CODE="HeavyLight" WIDTH="200" HEIGHT="85">
 3: </APPLET>
 4: */
 5: 
 6: // HeavyLight Applet showing the difference between heavyweight and
 7: // lightweight components.
 8: // Adopted from "Graphic Java 1.2: Mastering the JFC", 3rd Edition,
 9: // Volume 1, (C) 1999 by Sun Microsystems, Inc.  Published by Prentice-Hall.
10: // Written by Wayne Pollock, Tampa Florida USA, March 2000.
11: // Updated 2004.
12: 
13: import java.applet.*;
14: import java.awt.*;
15: import java.awt.event.*;
16: 
17: public class HeavyLight extends Applet
18: {
19:    private Image dining, paper;
20: 
21:    public void init ()
22:    {
23:       dining = getImage( getClass().getResource( "Dining.gif" ) );
24:       paper  = getImage( getClass().getResource( "paper.gif" ) );
25:       MediaTracker mt = new MediaTracker( this );
26:       try
27:       {
28:          mt.addImage( dining, 0 );
29:          mt.addImage( paper, 1 );
30:          mt.waitForAll();
31:       }
32:       catch( InterruptedException e )  {  e.printStackTrace();  }
33:       if ( mt.isErrorAny() )
34:          dining = paper = null;
35: 
36:       setLayout( new FlowLayout( FlowLayout.CENTER, 40, 18 ) );
37:       add( new Heavyweight( dining ) );
38:       add( new Lightweight( dining ) );
39:       setBackground( Color.white );
40:       setVisible( true );
41:    }
42: 
43:    public void paint ( Graphics g )
44:    {
45:       if ( paper == null )
46:       {
47:          g.drawString( "Error loading images!", 30, 45 );
48:          return;
49:       }
50:       wallPaper( g, paper );  // paint the background first...
51:       super.paint( g );       // ...before painting components!
52:    }
53: 
54:    public void wallPaper ( Graphics  g, Image image )
55:    {
56:       Dimension myArea = getSize();
57:       int tileWidth = image.getWidth( this );
58:       int tileHeight = image.getHeight( this );
59: 
60:       for ( int row = 0; row < myArea.width; row += tileWidth )
61:          for ( int col = 0; col < myArea.height; col += tileHeight )
62:             g.drawImage( image, row, col, this );
63:    }
64: }
65: 
66: 
67: class Lightweight extends Component // Component has no peer, so is lightweight
68: {
69:    private Image image;
70:    private Dimension mySize;
71: 
72:    public Lightweight ( Image im )
73:    {
74:       image = im;
75:       mySize = new Dimension( image.getWidth( this ), image.getHeight( this ) );
76:    }
77: 
78:    public void paint ( Graphics g )  {  g.drawImage( image, 0, 0, this );  }
79: 
80:    public Dimension getPreferredSize ()  {  return mySize;  }
81: }
82: 
83: 
84: class Heavyweight extends Panel  // Panel has a peer, so is heavyweight.
85: {
86:    private Image image;
87:    private Dimension mySize;
88: 
89:    public Heavyweight ( Image im )
90:    {
91:       image = im;
92:       mySize = new Dimension( image.getWidth( this ), image.getHeight( this ) );
93:    }
94: 
95:    public void paint ( Graphics g )  {  g.drawImage( image, 0, 0, this );  }
96: 
97:    public Dimension getPreferredSize ()  {  return mySize;  }
98: }