KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > FrameLauncher


1 /* Thinlet GUI toolkit - www.thinlet.com
2  * Copyright (C) 2002-2003 Robert Bajzat (robert.bajzat@thinlet.com) */

3 package thinlet;
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.awt.image.*;
8
9 /**
10  * <code>FrameLauncher</code> is a double buffered frame
11  * to launch any <i>thinlet</i> component as an application
12  */

13 public class FrameLauncher extends Frame implements WindowListener {
14     
15     private transient Thinlet content;
16     private transient Image doublebuffer;
17     
18     /**
19      * Construct and show a new frame with the specified title, including the
20      * given <i>thinlet</i> component. The frame is centered on the screen, and its
21      * preferred size is specified (excluding the frame's borders). The icon is
22      * the thinlet logo
23      *
24      * @param title the title to be displayed in the frame's border
25      * @param content a <i>thinlet</i> instance
26      * @param width the preferred width of the content
27      * @param height the preferred height of the content
28      */

29     public FrameLauncher(String JavaDoc title, Thinlet content, int width, int height) {
30         super(title);
31         this.content = content;
32         add(content, BorderLayout.CENTER);
33         addWindowListener(this);
34         pack();
35         
36         Insets is = getInsets();
37         width += is.left + is.right;
38         height += is.top + is.bottom;
39         Dimension ss = getToolkit().getScreenSize();
40         width = Math.min(width, ss.width);
41         height = Math.min(height, ss.height);
42         setBounds((ss.width - width) / 2, (ss.height - height) / 2, width, height);
43         setVisible(true);
44         //maximize: setBounds(-is.left, -is.top, ss.width + is.left + is.right, ss.height + is.top + is.bottom);
45

46         int[] pix = new int[16 * 16];
47         for (int x = 0; x < 16; x++) {
48             int sx = ((x >= 1) && (x <= 9)) ? 1 : (((x >= 11) && (x <= 14)) ? 2 : 0);
49             for (int y = 0; y < 16; y++) {
50                 int sy = ((y >= 1) && (y <= 9)) ? 1 : (((y >= 11) && (y <= 14)) ? 2 : 0);
51                 pix[y * 16 + x] = ((sx == 0) || (sy == 0)) ? 0xffffffff :
52                     ((sx == 1) ? ((sy == 1) ? (((y == 2) && (x >= 2) && (x <= 8)) ? 0xffffffff :
53                         (((y >= 3) && (y <= 8)) ? ((x == 5) ? 0xffffffff : (((x == 4) || (x == 6)) ?
54                             0xffe8bcbd : 0xffb01416)) : 0xffb01416)) : 0xff377ca4) :
55                         ((sy == 1) ? 0xff3a831d : 0xfff2cc9c));
56             }
57         }
58         setIconImage(createImage(new MemoryImageSource(16, 16, pix, 0, 16)));
59     }
60         
61     /**
62      * Call the paint method to redraw this component without painting a
63      * background rectangle
64      */

65     public void update(Graphics g) {
66         paint(g);
67     }
68
69     /**
70      * Create a double buffer if needed,
71      * the <i>thinlet</i> component paints the content
72      */

73     public void paint(Graphics g) {
74         if (doublebuffer == null) {
75             Dimension d = getSize();
76             doublebuffer = createImage(d.width, d.height);
77         }
78         Graphics dg = doublebuffer.getGraphics();
79         dg.setClip(g.getClipBounds());
80         super.paint(dg);
81         dg.dispose();
82         g.drawImage(doublebuffer, 0, 0, this);
83     }
84     
85     /**
86      * Clear the double buffer image (because the frame has been resized),
87      * the overriden method lays out its components
88      * (centers the <i>thinlet</i> component)
89      */

90     public void doLayout() {
91         if (doublebuffer != null) {
92             doublebuffer.flush();
93             doublebuffer = null;
94         }
95         super.doLayout();
96     }
97
98     /**
99      * Notify the <i>thinlet</i> component and terminates the Java Virtual Machine,
100      * or redisplay the frame depending on the return value of <i>thinlet</i>'s
101      * <code>destroy</code> method (true by default,
102      * thus terminates the VM if not overriden)
103      */

104     public void windowClosing(WindowEvent e) {
105         if (content.destroy()) {
106             System.exit(0);
107         }
108         setVisible(true);
109     }
110     public void windowOpened(WindowEvent e) {}
111     public void windowClosed(WindowEvent e) {}
112     public void windowIconified(WindowEvent e) {}
113     public void windowDeiconified(WindowEvent e) {}
114     public void windowActivated(WindowEvent e) {}
115     public void windowDeactivated(WindowEvent e) {}
116 }
Popular Tags