An old Java applet explained

What this is:
This is the explaination (partly) of an old java applet I once wrote.
This is obsolete software it does not run anymore in modern browsers.
But anyways I will explain it here for educational and archive purposes

To run it you will need an old Java version that still supports the old awt 1.02 event handling system.
You then can try it to run with the appletviewer which is delivered with the Sun sdk (system development kit)
See https://www.sun.com (currently owned by Oracle)
(see google for it)

Disclaimer:
Offcourse everything at your risk no liability for me :-)
Freeware


1:   import java.applet.Applet;
2:   import java.awt.*;
3:   import java.awt.image.*;
These are the package equired to run the applet
This old applet doesn't use Swing components
Just the ols awt (abstract windows toolkit) components
(awt is thread save Swing isn't)

5:   public class Panorama extends Applet implements Runnable
6:   {
Defenition of this applet
It is an extension of java.Applet
It inplements the runnable interface
Means that it must have a public method run

7:     public static final String REVISION_DATE="July 29 2001";
8:     
9:     Thread runner;
10:    MediaTracker tracker;
11:    String marker="nhgube EWUZ";            //security
12:    String marker2="ina qra Oretu";         //security
13:    boolean created;
14:    
15:    Image picture;
16:    int x,y,xAdd,width,height,delay;
17:    boolean bounce;
18:    
19:    String loadString="Loading please wait";
20:    
21:    boolean mouseDown=false,mouseDraged=false,mouseIn=false;
22:    Point mouse=new Point(0,0);
23:    int stop=1;
24:    
25:    Object frame;
26:    
27:    boolean sizeVertical;
28:    Image offscreenI;
29:    Graphics offscreenG;
30:    
These are all kinds of class variables.
Most of them are automaticly public accessible because no keyword public or private in front of them
Line 7 is a public final String, final meaning it is defined as a constant. (not changable)
line 9 Thread runner a program thread used do tasks in the background. It should I think normaly ber defined private I think. :-)
line 10 Mediatracker A class used for tracking (to see) whether or not an image has loaded from the Internet.
Line 11 and 12: Not importand (rot 13) I think some text to prevent stealing tracking of the applet
Not very usefull
Line 13 Some boolean flag to indicate for the rest of the applet that all is loaded (created)
line 15 The image normaly a panorama which scrolls on the applet and can be moved with the mouse
line 16 Just some parameters location of the image on the applet delay (determines refresh time) etceteras
line 19 Some string to display on the applet while the panoramic picture is loaded from the Internet
line 21..23 Some paramaters to indicate for the rest of the applet the state of the mouse
line 25 Object frame That is the frame of the applet.
It is needed to set the type of cursor of the mouse (for example when draging).
Line 27 boolean (should I resize the the picture Vertical when it is smaller?)
Line 28..29 Offscreen Images and graphics are used to draw the applet surface offscreen
After it is finached offscreen it is copied to the applet surface
This way drawing speed is increased

31:    public void init()
32:    {
This method is called when after an applet is loaded from the Internet.
An applet is an Java program running on an html page.

33:      super.init();
34:      System.out.println(getAppletInfo());
35:      while (marker!="nhgube EWUZ" || marker2!="ina qra Oretu"){}
36:      
37:      if (getParameter("load_string")!=null) loadString=getParameter("load_string");
38:      
39:      if (getParameter("size_vertical")!=null && getParameter("size_vertical").equalsIgnoreCase("true"))
40:      {
41:        sizeVertical=true;
42:        offscreenI=createImage(size().width,size().height);
43:        offscreenG=offscreenI.getGraphics();
44:      }else sizeVertical=false;
45:      
46:      
47:      //geting the frame of this applet
48:      frame = this;
49:      while (frame != null && !(frame instanceof Frame)) {
50:        frame = ((Component) frame).getParent();
51:      }
52:      
53:      created=false;
line 33 super.init()
The init method here overides the java.Applet.init method.
Actually it is not very usefull here to call this java.Applet.init with super.init
But actually I did this here for some reason don't remember why more than 18 years ago.
Line 34: System.out.println(getAppletInfo());
The public method java.Applet.get AppletInfo is overridden in this applet.
This method ruturns a String
In this case that String is printed on the java applet console
Which can be opened on most browsers
Line 37..44 Reads some parameters set in the html page.
For example <PARAM NAME="size_vertical" VALUE="false">
Parameter name is case sensitive
line 47: //geting the frame of this applet
line 48: frame = this;
line 49: while (frame != null && !(frame instanceof Frame)) {
line 50: frame = ((Component) frame).getParent();
line 51: }
This is interesting
A technique commently used by me
It finds the frame of the applet
This frame I must know to be able to set the mouse pointer
line 53 set this boolean to false indicating not everhing jet created
For example the panoramic pictures is not loaded jet (it still is null)

57:    public void start()
58:    {
59:      if (runner==null){ runner = new Thread(this);runner.start();}
60:    }
This method is called when the applet is shown in the browser html page.
What is does here is check weather the thread runner allready exists.
If it is null then it not has been created yet
runner=new thread(this) this refers yo this applet whuch has the runnable interface.
Meaning that is has a public method run.
In the run methode the runner thread enters to execute some code
We don't want to rerun it from start that is why we check if it not allready is created. (and started after)
The big advantage of java is you can have multiple threads running at the same time

62:    public void stop()
63:    {
64:      if (runner!=null){runner.stop();runner=null;}
65:    }
This methode is called when the user leaves the html page
More exactly when the applet is not vissable anymore to the user.
For example it opens another html page in another or the user closed the window containing the java applet
Note the old Netscape browsers little bit different behave

Will stop today typing probably will continue later if I have some more time
Questions or answers please email me at sales2 at comweb.nl
Note if you want to compile Panorama_explaned.java rename it to Panorama.java

Donation:
If you want to support my work, thanks

  
1:   import java.applet.Applet;
2:   import java.awt.*;
3:   import java.awt.image.*;
4:   
5:   public class Panorama extends Applet implements Runnable
6:   {
7:     public static final String REVISION_DATE="July 29 2001";
8:     
9:     Thread runner;
10:    MediaTracker tracker;
11:    String marker="nhgube EWUZ";            //security
12:    String marker2="ina qra Oretu";         //security
13:    boolean created;
14:    
15:    Image picture;
16:    int x,y,xAdd,width,height,delay;
17:    boolean bounce;
18:    
19:    String loadString="Loading please wait";
20:    
21:    boolean mouseDown=false,mouseDraged=false,mouseIn=false;
22:    Point mouse=new Point(0,0);
23:    int stop=1;
24:    
25:    Object frame;
26:    
27:    boolean sizeVertical;
28:    Image offscreenI;
29:    Graphics offscreenG;
30:    
31:    public void init()
32:    {
33:      super.init();
34:      System.out.println(getAppletInfo());
35:      while (marker!="nhgube EWUZ" || marker2!="ina qra Oretu"){}
36:      
37:      if (getParameter("load_string")!=null) loadString=getParameter("load_string");
38:      
39:      if (getParameter("size_vertical")!=null && getParameter("size_vertical").equalsIgnoreCase("true"))
40:      {
41:        sizeVertical=true;
42:        offscreenI=createImage(size().width,size().height);
43:        offscreenG=offscreenI.getGraphics();
44:      }else sizeVertical=false;
45:      
46:      
47:      //geting the frame of this applet
48:      frame = this;
49:      while (frame != null && !(frame instanceof Frame)) {
50:        frame = ((Component) frame).getParent();
51:      }
52:      
53:      created=false;
54:    }
55:    
56:    
57:    public void start()
58:    {
59:      if (runner==null){ runner = new Thread(this);runner.start();}
60:    }
61:    
62:    public void stop()
63:    {
64:      if (runner!=null){runner.stop();runner=null;}
65:    }
66:    
67:    private boolean create()
68:    {
69:      delay=50;String tempS=getParameter("delay");
70:      if (tempS!=null) delay=Integer.parseInt(tempS);
71:      
72:      xAdd=-1;tempS=getParameter("x_add");
73:      if (tempS!=null) xAdd=Integer.parseInt(tempS);
74:      
75:      bounce=false;tempS=getParameter("bounce");
76:      if (tempS!=null) if (tempS.toLowerCase().equals("true")) bounce=true;
77:      
78:      String filename=getParameter("picture");
79:      
80:      if (filename!=null)
81:      {
82:        MediaTracker tracker;
83:        picture=getImage(getCodeBase(),filename);
84:        tracker=new MediaTracker(this);
85:        tracker.addImage(picture,0);
86:        try
87:        {
88:          tracker.waitForID(0);
89:        }catch (InterruptedException e)
90:        {
91:          System.out.println("IO error loading images");
92:        }
93:        width=picture.getWidth(this);
94:        height=picture.getHeight(this);
95:      }
96:      
97:      return true;
98:    }
99:    
100:   public void run()
101:   {
102:     while (Thread.currentThread() == runner)
103:     {
104:       repaint();
105:       pause(delay);
106:       if (created)
107:       {
108:         if (!mouseDown)
109:         {
110:           x+=(stop*xAdd);
111:           if (!bounce && (x<-width || x>=width)) x=0;
112:           if (bounce && (x>=0 || x+width<=size().width))
113:           {
114:             xAdd*=-1;x+=2*xAdd;
115:           }
116:         }
117:       } else { repaint(200);created=create();}
118:     }
119:   }
120:   
121:   public boolean handleEvent(Event evt)
122:   {
123:     if (!created) return true;
124:     switch (evt.id)
125:     {
126:       case Event.MOUSE_ENTER:
127:       ((Frame)frame).setCursor(Frame.CROSSHAIR_CURSOR);
128:       mouseIn=true;
129:       return true;
130:       case Event.MOUSE_EXIT:
131:       ((Frame)frame).setCursor(Frame.DEFAULT_CURSOR);
132:       mouseIn=false;
133:       return true;
134:       case Event.MOUSE_DOWN:
135:       mouse.x=evt.x;
136:       mouse.y=evt.y;
137:       mouseDown=true;
138:       mouseDraged=false;
139:       return true;
140:       case Event.MOUSE_UP:
141:       if (evt.x>=size().width || evt.x<0 || evt.y>=size().height || evt.y<0) mouseIn=false;
142:       if (mouseIn) ((Frame)frame).setCursor(Frame.CROSSHAIR_CURSOR); else ((Frame)frame).setCursor(Frame.DEFAULT_CURSOR);
143:       mouseDown=false;
144:       if (mouse.x==evt.x && mouse.y==evt.y && !mouseDraged) stop=0; else stop=1;
145:       mouseDraged=false;
146:       return true;
147:       case Event.MOUSE_DRAG:
148:       ((Frame)frame).setCursor(Frame.MOVE_CURSOR);
149:       mouseDraged=true;
150:       x+=(evt.x-mouse.x);
151:       if (x<-width) x+=width;
152:       if (x>=width) x-=width;
153:       if ((evt.x<mouse.x && xAdd>0) || (evt.x>mouse.x && xAdd<0)) xAdd*=-1;
154:       mouse.x=evt.x;
155:       if (!sizeVertical)
156:       {
157:         y+=(evt.y-mouse.y);
158:         if (y+height<=size().height) y=size().height-height;
159:         if (y>0) y=0;
160:         mouse.y=evt.y;
161:       }
162:       return true;
163:     }
164:     return true;
165:   }
166:   
167:   public synchronized void update(Graphics g)
168:   {
169:     if (!created)
170:     {
171:       FontMetrics fm=getFontMetrics(g.getFont());
172:       int x=(int)((size().width-fm.stringWidth(loadString))/2),y=(size().height/2);
173:       g.drawString(loadString,x,y);
174:     }
175:     if (picture!=null)
176:     {
177:       if (sizeVertical && picture.getHeight(this)!=size().height)
178:       {
179:         if (offscreenI!=null)
180:         {
181:           offscreenG.drawImage(picture,x,y,picture.getWidth(this),size().height, this );
182:           if (x<0) offscreenG.drawImage(picture,x+width,y,picture.getWidth(this),size().height,this);
183:           if (x>0) offscreenG.drawImage(picture,x-width,y,picture.getWidth(this),size().height,this);
184:           g.drawImage(offscreenI,0,0,this);
185:         }
186:       } else
187:       {
188:         g.drawImage(picture,x,y,this);
189:         if (x<0) g.drawImage(picture,x+width,y,this);
190:         if (x>0) g.drawImage(picture,x-width,y,this);
191:       }
192:     }
193:   }
194:   
195:   public void paint(Graphics g)
196:   {
197:     update(g);
198:   }
199:   
200:   public String getAppletInfo()
201:   {
202:     return " \nPanorama applet \n(c)1998 rvdb@comweb.nl \nhttp://www.comweb.nl/~rvdb\n"+REVISION_DATE;
203:   }
204:   
205:   private void pause(int time)
206:   {
207:     if (time==0) return;//prevent a NullPointerException
208:     try { Thread.sleep(time); } catch (InterruptedException e) {}
209:   }
210:   
211: }

Output created with perl2html from https://www.comweb.nl
©2019 Comweb Netherlands, sales2@comweb.nl