[ Back | Previous | Next ]

How to add a Frame Icon to a frame?

Package:
java.awt.*
Product:
JDK
Release:
1.1.x
Related Links:
Color
Cursor
FileDialog
Font
FontMetrics
Frame
general
Image
LayoutManager
Menu
ScrollPane
Comment:
An icon is a graphics. This graphics can we connect to a Frame and will be visible at the top-left of the frame.
We will use two objects for this; 

1) MediaTracker to view the loading of the icon image, 

2) Toolkit to load the iconimage 

We have to following example in which all the icon image stuff is 
residing in the importer() method: 

      //  PacManApp.java
      //  
      import java.awt.*;

      public class PacManApp {
              Image icon; MediaTracker tracker; GUI     gui;   
              PlayField playfield;

          /** default constructor */
              public PacManApp( ) {
                      playfield = new PlayField( this, 30, 30); 
                      // make a new GUI enclosed in the PacManApp
                      gui = this. new GUI(); 
                      gui.setTitle( "Pac-Man" ); // title of frame
                      gui.setSize( 500, 550);
                      // make importer load all images in the PacManApp
                      importer();         
                      gui.setVisible( true );  
              } 

 
/** Loading all images @param gui - gui reference to the * Toolkit for loading the images */ public void importer() { Toolkit tk = gui.getToolkit(); tracker = new MediaTracker( gui ); icon = tk.getImage( "images/icon.gif"); // load application icon gui.setIconImage( icon ); }
// // MAIN METHOD // /** main method to enter the game */ public static void main( String [] arg ) { PacManApp app = new PacManApp(); } // // ENCLOSED GUI OBJECT // /** Application GUI */ class GUI extends Frame { // App.GUI is enclosed in the PacManApp Label info; // accessible for status info public GUI () { setLayout( new BorderLayout () ); setBackground( Color.black ); } } } /class
1