| 1 |
|
| 2 |
import java.awt.BorderLayout; |
| 3 |
import java.awt.image.BufferedImage; |
| 4 |
import java.net.URL; |
| 5 |
import javax.imageio.ImageIO; |
| 6 |
import javax.jnlp.BasicService; |
| 7 |
import javax.jnlp.ServiceManager; |
| 8 |
import javax.swing.Icon; |
| 9 |
import javax.swing.ImageIcon; |
| 10 |
import javax.swing.JApplet; |
| 11 |
import javax.swing.JFrame; |
| 12 |
import javax.swing.JLabel; |
| 13 |
|
| 14 |
/* AppletTest.java |
| 15 |
Copyright (C) 2011 Red Hat, Inc. |
| 16 |
|
| 17 |
This file is part of IcedTea. |
| 18 |
|
| 19 |
IcedTea is free software; you can redistribute it and/or |
| 20 |
modify it under the terms of the GNU General Public License as published by |
| 21 |
the Free Software Foundation, version 2. |
| 22 |
|
| 23 |
IcedTea is distributed in the hope that it will be useful, |
| 24 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 26 |
General Public License for more details. |
| 27 |
|
| 28 |
You should have received a copy of the GNU General Public License |
| 29 |
along with IcedTea; see the file COPYING. If not, write to |
| 30 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 31 |
02110-1301 USA. |
| 32 |
|
| 33 |
Linking this library statically or dynamically with other modules is |
| 34 |
making a combined work based on this library. Thus, the terms and |
| 35 |
conditions of the GNU General Public License cover the whole |
| 36 |
combination. |
| 37 |
|
| 38 |
As a special exception, the copyright holders of this library give you |
| 39 |
permission to link this library with independent modules to produce an |
| 40 |
executable, regardless of the license terms of these independent |
| 41 |
modules, and to copy and distribute the resulting executable under |
| 42 |
terms of your choice, provided that you also meet, for each linked |
| 43 |
independent module, the terms and conditions of the license of that |
| 44 |
module. An independent module is a module which is not derived from |
| 45 |
or based on this library. If you modify this library, you may extend |
| 46 |
this exception to your version of the library, but you are not |
| 47 |
obligated to do so. If you do not wish to do so, delete this |
| 48 |
exception statement from your version. |
| 49 |
*/ |
| 50 |
public class GifarMain extends JApplet { |
| 51 |
|
| 52 |
boolean isApplet = true; |
| 53 |
String defaultPath = "happyNonAnimated.gif"; |
| 54 |
String imageName; |
| 55 |
URL path = null; |
| 56 |
|
| 57 |
private class Killer extends Thread { |
| 58 |
|
| 59 |
public int n = 3000; |
| 60 |
|
| 61 |
@Override |
| 62 |
public void run() { |
| 63 |
try { |
| 64 |
Thread.sleep(n); |
| 65 |
System.out.println("gifar killing himself after " + n + " ms of life"); |
| 66 |
System.exit(0); |
| 67 |
} catch (Exception ex) { |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
private Killer killer; |
| 72 |
|
| 73 |
@Override |
| 74 |
public void init() { |
| 75 |
System.out.println("gifar was initialised"); |
| 76 |
killer = new Killer(); |
| 77 |
this.setLayout(new BorderLayout()); |
| 78 |
String futurePath = null; |
| 79 |
if (isApplet) { |
| 80 |
futurePath = getParameter("image"); |
| 81 |
if ("yes".equals(futurePath)) { |
| 82 |
imageName = defaultPath; |
| 83 |
} else if (futurePath != null) { |
| 84 |
imageName = futurePath; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void start() { |
| 91 |
System.out.println("gifar is starting"); |
| 92 |
String s = "<html>" + System.getProperty("java.vm.version") + "<br>" |
| 93 |
+ System.getProperty("java.vm.vendor") + "<br>" |
| 94 |
+ System.getProperty("java.vm.name") + "<br>" |
| 95 |
+ System.getProperty("java.specification.version") + "<br>" |
| 96 |
+ System.getProperty("java.specification.vendor") + "<br>" |
| 97 |
+ System.getProperty("java.specification.name") + "</html>"; |
| 98 |
JLabel jLabel1 = new JLabel(s); |
| 99 |
this.add(jLabel1, BorderLayout.NORTH); |
| 100 |
System.out.println("Used image: " + imageName); |
| 101 |
if (imageName != null) { |
| 102 |
try { |
| 103 |
path = new URL(getIndependentCodebase(), imageName); |
| 104 |
System.out.println("Loading: "+path.toString()); |
| 105 |
JLabel jLabel2 = new JLabel(loadIcon(path)); |
| 106 |
System.out.println("Image loaded"); |
| 107 |
this.add(jLabel2, BorderLayout.SOUTH); |
| 108 |
} catch (Exception ex) { |
| 109 |
//ex.printStackTrace(); |
| 110 |
throw new RuntimeException(ex); |
| 111 |
} |
| 112 |
} |
| 113 |
killer.start(); |
| 114 |
System.out.println("is applet: " + isApplet); |
| 115 |
System.out.println("gifar was started"); |
| 116 |
} |
| 117 |
|
| 118 |
Icon loadIcon(URL u) { |
| 119 |
try { |
| 120 |
BufferedImage i = ImageIO.read(u.openStream()); |
| 121 |
return new ImageIcon(i); |
| 122 |
} catch (Exception ex) { |
| 123 |
throw new RuntimeException(ex); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
public URL getIndependentCodebase() { |
| 130 |
try { |
| 131 |
URL u1 = getCodeBaseApplet(); |
| 132 |
URL u2 = getCodeBaseJavaws(); |
| 133 |
if (u1 != null) { |
| 134 |
return u1; |
| 135 |
} |
| 136 |
if (u2 != null) { |
| 137 |
return u2; |
| 138 |
} |
| 139 |
return new URL("http://localhost:44321/"); |
| 140 |
} catch (Exception ex) { |
| 141 |
throw new RuntimeException(ex); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
public URL getCodeBaseApplet() { |
| 147 |
try { |
| 148 |
URL codebase = getCodeBase(); |
| 149 |
if (codebase != null) { |
| 150 |
System.out.println("applet codebase: " + codebase.toString()); |
| 151 |
return codebase; |
| 152 |
} else { |
| 153 |
System.out.println("applet codebase: null"); |
| 154 |
} |
| 155 |
} catch (Exception ex) { |
| 156 |
ex.printStackTrace(); |
| 157 |
System.out.println("applet codebase: null"); |
| 158 |
} |
| 159 |
return null; |
| 160 |
} |
| 161 |
|
| 162 |
public URL getCodeBaseJavaws() { |
| 163 |
try { |
| 164 |
BasicService bs = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService"); |
| 165 |
URL codebase = bs.getCodeBase(); |
| 166 |
if (codebase != null) { |
| 167 |
System.out.println("javaws codebase: " + codebase.toString()); |
| 168 |
return codebase; |
| 169 |
} else { |
| 170 |
System.out.println("javaws codebase: null"); |
| 171 |
} |
| 172 |
} catch (Exception ex) { |
| 173 |
ex.printStackTrace(); |
| 174 |
System.out.println("javaws codebase: null"); |
| 175 |
} |
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public void stop() { |
| 181 |
System.out.println("gifar was stopped"); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void destroy() { |
| 186 |
System.out.println("gifar will be destroyed"); |
| 187 |
} |
| 188 |
|
| 189 |
public static void main(String[] args){ |
| 190 |
final JFrame f = new JFrame(); |
| 191 |
f.setLayout(new BorderLayout()); |
| 192 |
f.setSize(250, 200); |
| 193 |
GifarMain gm = new GifarMain(); |
| 194 |
gm.isApplet = false; |
| 195 |
f.add(gm); |
| 196 |
gm.init(); |
| 197 |
if (args.length > 0) { |
| 198 |
if ("yes".equals(args[0])) { |
| 199 |
gm.imageName = gm.defaultPath; |
| 200 |
} else { |
| 201 |
gm.imageName = args[0]; |
| 202 |
} |
| 203 |
} |
| 204 |
gm.start(); |
| 205 |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 206 |
java.awt.EventQueue.invokeLater(new Runnable() { |
| 207 |
public void run() { |
| 208 |
f.setVisible(true); |
| 209 |
} |
| 210 |
}); |
| 211 |
} |
| 212 |
} |