Home : Course Map : Chapter 12 : Java :
The Graphics Toolkit
JavaTech
Course Map
Chapter 12

Printing
  Demo 1
Cursor Icons
  Demo 2
MouseButtons
  Demo 3
PopupMenu
  Demo 4
Keystrokes
  Demo 5
Audio
  Demo 6
Timing & Speed
  Demo 7
Toolkit
  Demo 8
AppletContext
  Demo 9
Exercises

    Supplements
Java Beans
More APIs
Java & Browsers
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The AWT - Abstract Window Toolkit - does in fact provide a toolkit with all sorts of tools and resources regarding the display system.

The Component class provides a getToolkit() method to give a reference to the toolkit. The toolkit for the system will be a subclass of the abstract Toolkit class created for your host. Toolkit uses native peer code to access the system GUI.

There is also the static method

   java.awt.Toolkit.getDefaultToolkit()

in the abstract Toolkit class that returns a reference to the toolkit.

Toolkit provides all sorts of useful services:

  • Obtain images for applications

  • Printing

  • Info about screen size and resolution:

      java.awt.Toolkit.getScreenSize ()
      java.awt.Toolkit.getScreenResolution ()


  • Beep the keyboard

      java.awt.Toolkit.beep()

  • System colors (use index constants in SystemColor class to relate index to a given window component):

      java.awt.Toolkit.loadSystemColors (int[] systemColors)

For example, to create a frame that is sized to a particular proportion to the screen's size and in a particular location is illustrated by the following method:

  /** Put frame at center of screen. **/
  void centerFrame (JFrame f) {
    // Need the toolkit to get info on system.
    Toolkit tk = Toolkit.getDefaultToolkit ();

    // Get the screen dimensions.
    Dimension screen = tk.getScreenSize ();

    // Make the frame 1/4th size of screen.
    int fw =  (int) (screen.getWidth ()/4);
    int fh =  (int) (screen.getWidth ()/4);
    f.setSize (fw,fh);

    // And place it in center of screen.
    int lx =  (int) (screen.getWidth ()  * 3/8);
    int ly =  (int) (screen.getHeight () * 3/8);
    f.setLocation (lx,ly);
  } // centerFrame

The program FrameCenterApplet shown below is identical to FrameApplet example in Chapter 7: Java: Fames except that it uses the above method to place the frame at the center of the screen.

FrameCenterApplet
   
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
  * Same as FrameApplet except that it centers the frame in
  * monitor display area.
  *
  * This program demonstrates the basics of creating a frame
  * user interface with a menubar. It also shows how to
  * add a menubar and dropdown menus to the applet, which wasn't
  * possible in the basic AWT heavyweight component.
 **/
public class FrameCenterApplet extends JApplet
       implements ActionListener
{
  JFrame fFrame;
  JMenuItem fMenuClose ;
  JMenuItem fMenuOpen;

  /** Build an applet interface with a menubar. A
    * a drop down menu includes Open/Close items
    * for opening and closing an instance of ParticleFrame.
   **/
  public void init () {
    JMenuBar mb = new JMenuBar ();
    JMenu m = new JMenu ("File");
    fMenuOpen= new JMenuItem ("Open");
    m.add (fMenuOpen);
    fMenuOpen.addActionListener (this);

    fMenuClose = new JMenuItem ("Close");
    m.add (fMenuClose);
    fMenuClose.addActionListener (this);
    mb.add (m);

    setJMenuBar (mb);

    fFrame = new ParticleCenterFrame (this);
    centerFrame (fFrame);
    fFrame.setVisible (true);
    fMenuOpen.setEnabled (false);
    fMenuClose.setEnabled (true);

  } // init

  /** Get the menu events here. Open an instance of ParticleFrame
    * or close the one currently displayed.
   **/
  public void actionPerformed (ActionEvent e) {
    String command = e.getActionCommand ();
    if (command.equals ("Close")) {
        close ();
    } else { // Open
        if (fFrame == null) {
            fFrame = new ParticleCenterFrame (this);
            centerFrame (fFrame);
            fFrame.setVisible (true);
            fMenuOpen.setEnabled (false);
            fMenuClose.setEnabled (true);
        }
    }
  } // actionPerformed

  /** Put frame at center of screen. **/
  void centerFrame (JFrame f) {
    // Need the toolkit to get info on system.
    Toolkit tk = Toolkit.getDefaultToolkit ();

    // Get the screen dimensions.
    Dimension screen = tk.getScreenSize ();

    // Make the frame 1/4th size of screen.
    int fw =  (int) (screen.getWidth ()/4);
    int fh =  (int) (screen.getWidth ()/4);
    f.setSize (fw,fh);

    // And place it in center of screen.
    int lx =  (int) (screen.getWidth ()  * 3/8);
    int ly =  (int) (screen.getHeight () * 3/8);
    f.setLocation (lx,ly);
  } // centerFrame

  /** Close the frame. **/
  void close () {
    fFrame.dispose ();
    fFrame = null;
    fMenuOpen.setEnabled (true);
    fMenuClose.setEnabled (false);
  } // close

} // class FrameApplet


/** A JFrame subclass that displays a menu bar
  * and a JComboBox.
 **/
class ParticleCenterFrame extends JFrame
                    implements ActionListener, ItemListener
{
  JLabel fLabelA;
  JLabel fLabelB;

  FrameCenterApplet fApplet;

  ParticleCenterFrame (FrameCenterApplet applet) {
    super ("Frame Test");

    fApplet = applet;
    Container content_pane = getContentPane ();

    content_pane.setLayout (new GridLayout (1,3));

    JPanel choice_panel = new JPanel ();
    choice_panel.add (new JLabel ("Quark", JLabel.RIGHT) );

    JComboBox c = new JComboBox ();
    c.addItem ("Up");
    c.addItem ("Down");
    c.addItem ("Strange");
    c.addItem ("Charm");
    c.addItem ("Top");
    c.addItem ("Bottom");
    c.addItemListener (this);
    choice_panel.add (c);

    content_pane.add (choice_panel);


    fLabelA =new JLabel ("Quark: Up");
    content_pane.add (fLabelA);
    fLabelB =new JLabel ("Lepton: Electron");
    content_pane.add (fLabelB);

    // Use the helper method makeMenuItem
    // for making the menu items and registering
    // their listener.
    JMenu m = new JMenu ("Lepton");
    m.add (makeMenuItem ("electron"));
    m.add (makeMenuItem ("muon"));
    m.add (makeMenuItem ("tau"));

    JMenu sm = new JMenu ("Neutrino");
    sm.add (makeMenuItem ("e Neutrino"));
    sm.add (makeMenuItem ("mu Neutrino"));
    sm.add (makeMenuItem ("tau Neutrino"));

    m.add (sm);
    m.add (makeMenuItem ("Quit"));
    JMenuBar mb = new JMenuBar ();
    mb.add (m);

    setJMenuBar (mb);
    setSize (200,200);
    pack ();
    setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
  } // ctor

  /** Get the combobox item events here. **/
  public void itemStateChanged (ItemEvent e) {
    String command = e.getItem ().toString ();
    if (command.equals ("Quit") )
        dispose ();
    else
        fLabelA.setText ("Quark: " + command);
  } // itemStateChanged

  /** Get the menu events here. **/
  public void actionPerformed (ActionEvent e) {
    String command = e.getActionCommand ();
    if (command.equals ("Quit")) {
        fApplet.close ();
    } else {
        fLabelB.setText ("Lepton: " + command);
    }
  } // actionPerformed

  /** This "helper method" makes a menu item and then
    * registers this applet as a listener to it.
   **/
  private JMenuItem makeMenuItem (String name) {
    JMenuItem m = new JMenuItem (name);
    m.addActionListener (this);
    return m;
  } // makeMenuItem

} // class ParticleFrame

An application whose main() method puts a frame at center:
PlaceFrameApp.java

 

References & Web Resources

 

Latest update: Dec.6, 2004

              Tech
Tech APIs
Exercises

           Physics
Math/Science APIs
JAIDA Program
  Demo 1
Demo 1 Discussion
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.