KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > AppletUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5
6 import java.awt.Dimension JavaDoc;
7 import java.awt.Toolkit JavaDoc;
8
9 import javax.swing.JApplet JavaDoc;
10 import javax.swing.JFrame JavaDoc;
11 import javax.swing.JPanel JavaDoc;
12
13 /**
14  * Misc applet utils.
15  */

16 public class AppletUtil {
17
18     /**
19      * Creates a title string from the class name:
20      */

21     private static String JavaDoc title(Object JavaDoc o) {
22         String JavaDoc t = o.getClass().toString();
23         if (t.indexOf("class") != -1) {
24             t = t.substring(6);
25         }
26         return t;
27     }
28
29
30     /**
31      * Runs a frame.
32      */

33     public static void run(JFrame JavaDoc frame, int width, int height) {
34         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35         frame.setSize(width, height);
36         frame.setVisible(true);
37     }
38     
39     
40     /**
41      * Runs an applet.
42      */

43     public static void run(JApplet JavaDoc applet, int width, int height) {
44         JFrame JavaDoc frame = new JFrame JavaDoc(title(applet));
45         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
46         frame.getContentPane().add(applet);
47         frame.setSize(width, height);
48         applet.init();
49         applet.start();
50         frame.setVisible(true);
51     }
52
53     /**
54      * Runs a pannel.
55      */

56     public static void run(JPanel JavaDoc panel, int width, int height) {
57         JFrame JavaDoc frame = new JFrame JavaDoc(title(panel));
58         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
59         frame.getContentPane().add(panel);
60         frame.setSize(width, height);
61         frame.setVisible(true);
62     }
63
64
65     /**
66      * Center frame window.
67      */

68     public static void center(JFrame JavaDoc frame) {
69         Dimension JavaDoc frameSize = frame.getSize();
70         Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
71         frame.setLocation((screenSize.width - frameSize.width) >> 1, (screenSize.height - frameSize.height) >> 1);
72     }
73 }
74
Popular Tags