[Thinlet-development] thinlet/src/java/thinlet AppletLauncher.java,1.2,1.3 FrameLauncher.java,1.2,1.
Brought to you by:
bajzat
From: <ba...@us...> - 2003-10-13 11:46:21
|
Update of /cvsroot/thinlet/thinlet/src/java/thinlet In directory sc8-pr-cvs1:/tmp/cvs-serv25763 Modified Files: AppletLauncher.java FrameLauncher.java Log Message: applet parameter in constructor and docs Index: AppletLauncher.java =================================================================== RCS file: /cvsroot/thinlet/thinlet/src/java/thinlet/AppletLauncher.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- AppletLauncher.java 6 Jul 2003 22:48:05 -0000 1.2 +++ AppletLauncher.java 13 Oct 2003 11:46:17 -0000 1.3 @@ -1,88 +1,116 @@ -package thinlet; - -import java.applet.*; -import java.awt.*; - -/** - * Useful utility class to start Thinlet application in an applet window. - */ -public class AppletLauncher extends Applet implements Runnable { - - private transient Thinlet content; - private transient Image doublebuffer; - - /** - * - */ - public void init() { - setBackground(Color.white); setForeground(Color.darkGray); - setLayout(new BorderLayout()); - add(new Label("Loading...", Label.CENTER), BorderLayout.CENTER); - new Thread(this).start(); - } - - /** - * - */ - public void run() { - try { - content = (Thinlet) Class.forName(getParameter("class")).newInstance(); - removeAll(); - add(content, BorderLayout.CENTER); - } catch (Throwable exc) { - removeAll(); - add(new Label(exc.getMessage()), BorderLayout.CENTER); - } - doLayout(); repaint(); - } - - /** - * - */ - public void doLayout() { - super.doLayout(); - if (doublebuffer != null) { - doublebuffer.flush(); - doublebuffer = null; - } - } - - /** - * - */ - public void stop() { - if (doublebuffer != null) { - doublebuffer.flush(); - doublebuffer = null; - } - } - - /** - * - */ - public void update(Graphics g) { - paint(g); - } - - /** - * - */ - public void paint(Graphics g) { - if (doublebuffer == null) { - Dimension d = getSize(); - doublebuffer = createImage(d.width, d.height); - } - Graphics dg = doublebuffer.getGraphics(); - dg.setClip(g.getClipBounds()); - super.paint(dg); - dg.dispose(); - g.drawImage(doublebuffer, 0, 0, this); - } - - /** - * - */ - public void destroy() { - content.destroy(); - } -} +/* Thinlet GUI toolkit - www.thinlet.com + * Copyright (C) 2002-2003 Robert Bajzat (rob...@th...) */ +package thinlet; + +import java.applet.*; +import java.awt.*; + +/** + * <code>AppletLauncher</code> is a double buffered applet + * to launch any <i>thinlet</i> component + */ +public class AppletLauncher extends Applet implements Runnable { + + private transient Thinlet content; + private transient Image doublebuffer; + + /** + * Applet instance is created by the browser or applet viewer + */ + public AppletLauncher() { + super(); // for javadoc + } + + /** + * Called by the browser to inform this applet that it has been loaded into + * the system, it displays the <i>Loading...</i> label and starts the loader thread + */ + public void init() { + setBackground(Color.white); setForeground(Color.darkGray); + setLayout(new BorderLayout()); + add(new Label("Loading...", Label.CENTER), BorderLayout.CENTER); + new Thread(this).start(); + } + + /** + * Create a new <i>thinlet</i> instance of the class given as + * <code>class</code> applet parameter, and show it or the + * message of the thrown exception. First try a contructor with + * an applet parameter (thus you get this applet instance e.g. for + * the parameters of the applet HTML tag), then the empty constructor + */ + public void run() { + try { + Class thinletclass = Class.forName(getParameter("class")); + try { + content = (Thinlet) thinletclass.getConstructor(new Class[] { + Applet.class }).newInstance(new Object[] { this }); + } catch (NoSuchMethodException nsme) { + content = (Thinlet) thinletclass.newInstance(); + } + removeAll(); + add(content, BorderLayout.CENTER); + } catch (Throwable exc) { + removeAll(); + add(new Label(exc.getClass().getName() + " " + + exc.getMessage(), Label.CENTER), BorderLayout.CENTER); + } + doLayout(); repaint(); + } + + /** + * Clear the double buffer image, the overriden method lays out its + * components (centers the <i>thinlet</i> component) + */ + public void doLayout() { + super.doLayout(); + if (doublebuffer != null) { + doublebuffer.flush(); + doublebuffer = null; + } + } + + /** + * Called by the browser to inform this applet that it should stop its execution, + * it clears the double buffer image + */ + public void stop() { + if (doublebuffer != null) { + doublebuffer.flush(); + doublebuffer = null; + } + } + + /** + * Call the paint method to redraw this component without painting a + * background rectangle + */ + public void update(Graphics g) { + paint(g); + } + + /** + * Create a double buffer if needed, + * the <i>thinlet</i> component paints the content + */ + public void paint(Graphics g) { + if (doublebuffer == null) { + Dimension d = getSize(); + doublebuffer = createImage(d.width, d.height); + } + Graphics dg = doublebuffer.getGraphics(); + dg.setClip(g.getClipBounds()); + super.paint(dg); + dg.dispose(); + g.drawImage(doublebuffer, 0, 0, this); + } + + /** + * Called by the browser to inform this applet that it is being reclaimed, + * it calls the <i>thinlet</i> component's <code>destroy</code> method + * (its return value is irrelevant) + */ + public void destroy() { + content.destroy(); + } +} \ No newline at end of file Index: FrameLauncher.java =================================================================== RCS file: /cvsroot/thinlet/thinlet/src/java/thinlet/FrameLauncher.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- FrameLauncher.java 6 Jul 2003 22:48:05 -0000 1.2 +++ FrameLauncher.java 13 Oct 2003 11:46:17 -0000 1.3 @@ -1,104 +1,116 @@ -package thinlet; - -import java.awt.*; -import java.awt.event.*; -import java.awt.image.*; - -/** - * Useful utility class to start a Thinlet application in AWT Frame. - */ -public class FrameLauncher extends Frame implements WindowListener { - - private transient Thinlet content; - private transient Image doublebuffer; - - /** - * @param title - * @param content - * @param width - * @param height - */ - public FrameLauncher(String title, Thinlet content, int width, int height) { - super(title); - this.content = content; - add(content, BorderLayout.CENTER); - addWindowListener(this); - pack(); - - Insets is = getInsets(); - width += is.left + is.right; - height += is.top + is.bottom; - Dimension ss = getToolkit().getScreenSize(); - width = Math.min(width, ss.width); - height = Math.min(height, ss.height); - setBounds((ss.width - width) / 2, (ss.height - height) / 2, width, height); - setVisible(true); - //maximize: setBounds(-is.left, -is.top, ss.width + is.left + is.right, ss.height + is.top + is.bottom); - - int[] pix = new int[16 * 16]; - for (int x = 0; x < 16; x++) { - int sx = ((x >= 1) && (x <= 9)) ? 1 : (((x >= 11) && (x <= 14)) ? 2 : 0); - for (int y = 0; y < 16; y++) { - int sy = ((y >= 1) && (y <= 9)) ? 1 : (((y >= 11) && (y <= 14)) ? 2 : 0); - pix[y * 16 + x] = ((sx == 0) || (sy == 0)) ? 0xffffffff : - ((sx == 1) ? ((sy == 1) ? (((y == 2) && (x >= 2) && (x <= 8)) ? 0xffffffff : - (((y >= 3) && (y <= 8)) ? ((x == 5) ? 0xffffffff : (((x == 4) || (x == 6)) ? - 0xffe8bcbd : 0xffb01416)) : 0xffb01416)) : 0xff377ca4) : - ((sy == 1) ? 0xff3a831d : 0xfff2cc9c)); - } - } - setIconImage(createImage(new MemoryImageSource(16, 16, pix, 0, 16))); - } - - /** - * - */ - public void update(Graphics g) { - paint(g); - } - - /** - * - */ - public void paint(Graphics g) { - if (doublebuffer == null) { - Dimension d = getSize(); - doublebuffer = createImage(d.width, d.height); - } - Graphics dg = doublebuffer.getGraphics(); - dg.setClip(g.getClipBounds()); - super.paint(dg); - dg.dispose(); - g.drawImage(doublebuffer, 0, 0, this); - } - - /** - * - */ - public void doLayout() { - if (doublebuffer != null) { - doublebuffer.flush(); - doublebuffer = null; - } - super.doLayout(); - } - - /** - * This method calls Thinlet.destroy(), and exits only if that method - * returns true. This provides a way for programmers to perform - * cleanup tasks on exit, or disallow exiting altogether. - */ - public void windowClosing(WindowEvent e) { - if (content.destroy()) { - System.exit(0); - } - setVisible(true); - } - - public void windowOpened(WindowEvent e) {} - public void windowClosed(WindowEvent e) {} - public void windowIconified(WindowEvent e) {} - public void windowDeiconified(WindowEvent e) {} - public void windowActivated(WindowEvent e) {} - public void windowDeactivated(WindowEvent e) {} -} +/* Thinlet GUI toolkit - www.thinlet.com + * Copyright (C) 2002-2003 Robert Bajzat (rob...@th...) */ +package thinlet; + +import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; + +/** + * <code>FrameLauncher</code> is a double buffered frame + * to launch any <i>thinlet</i> component as an application + */ +public class FrameLauncher extends Frame implements WindowListener { + + private transient Thinlet content; + private transient Image doublebuffer; + + /** + * Construct and show a new frame with the specified title, including the + * given <i>thinlet</i> component. The frame is centered on the screen, and its + * preferred size is specified (excluding the frame's borders). The icon is + * the thinlet logo + * + * @param title the title to be displayed in the frame's border + * @param content a <i>thinlet</i> instance + * @param width the preferred width of the content + * @param height the preferred height of the content + */ + public FrameLauncher(String title, Thinlet content, int width, int height) { + super(title); + this.content = content; + add(content, BorderLayout.CENTER); + addWindowListener(this); + pack(); + + Insets is = getInsets(); + width += is.left + is.right; + height += is.top + is.bottom; + Dimension ss = getToolkit().getScreenSize(); + width = Math.min(width, ss.width); + height = Math.min(height, ss.height); + setBounds((ss.width - width) / 2, (ss.height - height) / 2, width, height); + setVisible(true); + //maximize: setBounds(-is.left, -is.top, ss.width + is.left + is.right, ss.height + is.top + is.bottom); + + int[] pix = new int[16 * 16]; + for (int x = 0; x < 16; x++) { + int sx = ((x >= 1) && (x <= 9)) ? 1 : (((x >= 11) && (x <= 14)) ? 2 : 0); + for (int y = 0; y < 16; y++) { + int sy = ((y >= 1) && (y <= 9)) ? 1 : (((y >= 11) && (y <= 14)) ? 2 : 0); + pix[y * 16 + x] = ((sx == 0) || (sy == 0)) ? 0xffffffff : + ((sx == 1) ? ((sy == 1) ? (((y == 2) && (x >= 2) && (x <= 8)) ? 0xffffffff : + (((y >= 3) && (y <= 8)) ? ((x == 5) ? 0xffffffff : (((x == 4) || (x == 6)) ? + 0xffe8bcbd : 0xffb01416)) : 0xffb01416)) : 0xff377ca4) : + ((sy == 1) ? 0xff3a831d : 0xfff2cc9c)); + } + } + setIconImage(createImage(new MemoryImageSource(16, 16, pix, 0, 16))); + } + + /** + * Call the paint method to redraw this component without painting a + * background rectangle + */ + public void update(Graphics g) { + paint(g); + } + + /** + * Create a double buffer if needed, + * the <i>thinlet</i> component paints the content + */ + public void paint(Graphics g) { + if (doublebuffer == null) { + Dimension d = getSize(); + doublebuffer = createImage(d.width, d.height); + } + Graphics dg = doublebuffer.getGraphics(); + dg.setClip(g.getClipBounds()); + super.paint(dg); + dg.dispose(); + g.drawImage(doublebuffer, 0, 0, this); + } + + /** + * Clear the double buffer image (because the frame has been resized), + * the overriden method lays out its components + * (centers the <i>thinlet</i> component) + */ + public void doLayout() { + if (doublebuffer != null) { + doublebuffer.flush(); + doublebuffer = null; + } + super.doLayout(); + } + + /** + * Notify the <i>thinlet</i> component and terminates the Java Virtual Machine, + * or redisplay the frame depending on the return value of <i>thinlet</i>'s + * <code>destroy</code> method (true by default, + * thus terminates the VM if not overriden) + */ + public void windowClosing(WindowEvent e) { + if (content.destroy()) { + System.exit(0); + } + setVisible(true); + } + public void windowOpened(WindowEvent e) {} + public void windowClosed(WindowEvent e) {} + public void windowIconified(WindowEvent e) {} + public void windowDeiconified(WindowEvent e) {} + public void windowActivated(WindowEvent e) {} + public void windowDeactivated(WindowEvent e) {} +} \ No newline at end of file |