I don't know what happend with my code
It is:
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
public class Ejemplo1 extends Frame implements KeyListener {
private static final long serialVersionUID = 1L;
private String imageURL = "http://dreamers.com/byrne/46.jpg";
private ImageIcon imagen;
private int x;
private int y;
public static void main (String[] args) {
Ejemplo1 ejemplo = new Ejemplo1();
ejemplo.setVisible (true);
}
Ejemplo1() {
super ("Ejemplo1");
setSize (100, 257);
try {
imagen = new ImageIcon (new URL (imageURL));
} catch ( MalformedURLException e) {
e.printStackTrace ();
}
x = 100;
y = 257;
addKeyListener (this);
addWindowListener (new WindowAdapter(){
@Override
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void keyPressed (KeyEvent e) {
boolean cambio = true;
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
y--;
break;
case KeyEvent.VK_DOWN:
y++;
break;
case KeyEvent.VK_LEFT:
x--;
break;
case KeyEvent.VK_RIGHT:
x++;
break;
default:
cambio = false;
}
if(cambio)
repaint();
}
@Override
public void paint (Graphics g) {
if (imagen!=null) {
g.drawImage (imagen.getImage(), x, y, imagen.getImageObserver());
} else {
g.drawString ("Can't load image " + imageURL, x, y);
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}
My program is compilated well with the "javac"
but when i try to show it with a applet it show me this kind of message where it should be to show the image:
Java Plug-in 1.6.0_26
Usar versión JRE 1.6.0_26-b03 Java HotSpot(TM) Client VM
Directorio local del usuario = C:\Users\Jose
----------------------------------------------------
c: borrar ventana de consola
f: finalizar objetos en la cola de finalización
g: liberación de recursos
h: presentar este mensaje de ayuda
l: volcar lista del cargador de clases
m: imprimir sintaxis de memoria
o: activar registro
q: ocultar consola
r: recargar configuración de norma
s: volcar propiedades del sistema y de despliegue
t: volcar lista de subprocesos
v: volcar pila de subprocesos
x: borrar antememoria del cargador de clases
0-5: establecer nivel de rastreo en <n>
----------------------------------------------------
java.lang.reflect.InvocationTargetException
at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Un known Source)
at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.RuntimeException: java.lang.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class Ejemplo1 with modifiers ""
at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class Ejemplo1 with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
... 15 more
Excepción: java.lang.reflect.InvocationTargetException
you know what happen? the applet only shows a message where it should be to show the image, simple like this: "Error, do click to show the error" and i did click and it show me the code of the exceptions before
Thanks for your Help!