/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. package pak; 

2. import java.awt.*; 

3. import java.awt.event.*; 

4. import java.applet.*; 

5.  

6. public class AppletOrApp 

7.     extends Applet 

8.

9.   Label lbl ; 

10.   boolean isStandAlone = false

11.  

12.   public void 

13.   paint ( Graphics gr ) 

14.   { 

15.     int x, y ; 

16.     for ( int i=0; i < 200; i++ ) 

17.     { 

18.       x = (int)( Math.random() * 300 ); 

19.       y = (int)( Math.random() * 300 ); 

20.       gr.drawLine ( 150,150, x, y ); 

21.     } 

22.   } 

23.   public void 

24.   init () 

25.   { 

26.     setBackground ( Color.pink ); 

27.     lbl = new Label ( "I'm running inside a browser or viewer" ) ; 

28.     if ( isStandAlone ) 

29.       lbl.setText ( "I'm running standalone" ) ; 

30.     lbl.setBackground ( Color.yellow ) ; 

31.     add ( lbl ) ; 

32.     validate () ; 

33.   } 

34.  

35.   public static void 

36.   main ( String[] args ) 

37.   { 

38.     AppletOrApp ap = new AppletOrApp(); 

39.     ap.isStandAlone = true

40.  

41.     OwnFrame frame = new OwnFrame () ; 

42.     frame.setTitle ( "Applet running standalone" ) ; 

43.     frame.setSize ( 305, 330 ) ; 

44.  

45.     frame.add ( ap, BorderLayout.CENTER ) ; 

46.  

47.     ap.init(); 

48.  

49.     Dimension scr = 

50.       Toolkit.getDefaultToolkit().getScreenSize(); 

51.     int frw, frh ; 

52.     frw = frame.getSize().width ; 

53.     frh = frame.getSize().height ; 

54.     frame.setLocation 

55.     ((scr.width - frw ) / 2

56.     (scr.height - frh ) / 2); 

57.  

58.     frame.setVisible(true); 

59.   } 

60.

61. ////////////////////////// 

62. class OwnFrame 

63.     extends Frame 

64.

65.   OwnFrame ()  { 

66.     addWindowListener( new WindowAdapter() { 

67.           public void windowClosing ( WindowEvent we ) { 

68.              dispose (); 

69.              System.exit(0); 

70.           } 

71.         } ); 

72.   } 

73.

74. /*======================== 

75.  go to the directory where the pak directory is 

76. "C:\Program Files\Java\jdk1.5.0_06\bin\java.exe"  pak.AppletOrApp 

77. "C:\Program Files\Java\jdk1.5.0_06\bin\java.exe"  pak.AppletOrApp uno due 

78. ====== 

79. "C:\Program Files\Java\jdk1.5.0_06\bin\jar.exe" 

80.                 -cvfm standalone.jar manifest.txt pak 

81. ================= 

82. standalone.jar : 

83.  META-INF 

84.    MANIFEST.MF 

85.     Manifest-Version: 1.0    (from manifest.txt) 

86.     Main-Class: pak.AppletOrApp    (from manifest.txt) 

87.  pak 

88.    AppletOrApp.class 

89.    OwnFrame.class 

90.    OwnFrame$1.class 

91.  

92.  */