001: /* Thinlet GUI toolkit - www.thinlet.com
002: * Copyright (C) 2002-2003 Robert Bajzat (robert.bajzat@thinlet.com) */
003: package thinlet;
004:
005: import java.awt.*;
006: import java.awt.event.*;
007: import java.awt.image.*;
008:
009: /**
010: * <code>FrameLauncher</code> is a double buffered frame
011: * to launch any <i>thinlet</i> component as an application
012: */
013: public class FrameLauncher extends Frame implements WindowListener {
014:
015: private transient Thinlet content;
016: private transient Image doublebuffer;
017:
018: /**
019: * Construct and show a new frame with the specified title, including the
020: * given <i>thinlet</i> component. The frame is centered on the screen, and its
021: * preferred size is specified (excluding the frame's borders). The icon is
022: * the thinlet logo
023: *
024: * @param title the title to be displayed in the frame's border
025: * @param content a <i>thinlet</i> instance
026: * @param width the preferred width of the content
027: * @param height the preferred height of the content
028: */
029: public FrameLauncher(String title, Thinlet content, int width,
030: int height) {
031: super (title);
032: this .content = content;
033: add(content, BorderLayout.CENTER);
034: addWindowListener(this );
035: pack();
036:
037: Insets is = getInsets();
038: width += is.left + is.right;
039: height += is.top + is.bottom;
040: Dimension ss = getToolkit().getScreenSize();
041: width = Math.min(width, ss.width);
042: height = Math.min(height, ss.height);
043: setBounds((ss.width - width) / 2, (ss.height - height) / 2,
044: width, height);
045: setVisible(true);
046: //maximize: setBounds(-is.left, -is.top, ss.width + is.left + is.right, ss.height + is.top + is.bottom);
047:
048: int[] pix = new int[16 * 16];
049: for (int x = 0; x < 16; x++) {
050: int sx = ((x >= 1) && (x <= 9)) ? 1
051: : (((x >= 11) && (x <= 14)) ? 2 : 0);
052: for (int y = 0; y < 16; y++) {
053: int sy = ((y >= 1) && (y <= 9)) ? 1
054: : (((y >= 11) && (y <= 14)) ? 2 : 0);
055: pix[y * 16 + x] = ((sx == 0) || (sy == 0)) ? 0xffffffff
056: : ((sx == 1) ? ((sy == 1) ? (((y == 2)
057: && (x >= 2) && (x <= 8)) ? 0xffffffff
058: : (((y >= 3) && (y <= 8)) ? ((x == 5) ? 0xffffffff
059: : (((x == 4) || (x == 6)) ? 0xffe8bcbd
060: : 0xffb01416))
061: : 0xffb01416))
062: : 0xff377ca4)
063: : ((sy == 1) ? 0xff3a831d : 0xfff2cc9c));
064: }
065: }
066: setIconImage(createImage(new MemoryImageSource(16, 16, pix, 0,
067: 16)));
068: }
069:
070: /**
071: * Call the paint method to redraw this component without painting a
072: * background rectangle
073: */
074: public void update(Graphics g) {
075: paint(g);
076: }
077:
078: /**
079: * Create a double buffer if needed,
080: * the <i>thinlet</i> component paints the content
081: */
082: public void paint(Graphics g) {
083: if (doublebuffer == null) {
084: Dimension d = getSize();
085: doublebuffer = createImage(d.width, d.height);
086: }
087: Graphics dg = doublebuffer.getGraphics();
088: dg.setClip(g.getClipBounds());
089: super .paint(dg);
090: dg.dispose();
091: g.drawImage(doublebuffer, 0, 0, this );
092: }
093:
094: /**
095: * Clear the double buffer image (because the frame has been resized),
096: * the overriden method lays out its components
097: * (centers the <i>thinlet</i> component)
098: */
099: public void doLayout() {
100: if (doublebuffer != null) {
101: doublebuffer.flush();
102: doublebuffer = null;
103: }
104: super .doLayout();
105: }
106:
107: /**
108: * Notify the <i>thinlet</i> component and terminates the Java Virtual Machine,
109: * or redisplay the frame depending on the return value of <i>thinlet</i>'s
110: * <code>destroy</code> method (true by default,
111: * thus terminates the VM if not overriden)
112: */
113: public void windowClosing(WindowEvent e) {
114: if (content.destroy()) {
115: System.exit(0);
116: }
117: setVisible(true);
118: }
119:
120: public void windowOpened(WindowEvent e) {
121: }
122:
123: public void windowClosed(WindowEvent e) {
124: }
125:
126: public void windowIconified(WindowEvent e) {
127: }
128:
129: public void windowDeiconified(WindowEvent e) {
130: }
131:
132: public void windowActivated(WindowEvent e) {
133: }
134:
135: public void windowDeactivated(WindowEvent e) {
136: }
137: }
|