Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: I have a problem with my code I think

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have a problem with my code I think

    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!


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: I have a problem with my code I think

    'Modifiers' are the keywords that go before your member names. Your constructor 'Ejemplo1()' has no modifiers. I'm not familiar with this kind of error because I tend to be a bit anally retentive when it comes to modifiers, specifying as many as I possibly can, but I'd say the JVM is attempting to find a super-class constructor with matching (that is, none at all) modifiers. I'm surprised that wasn't caught at compile time, if that's the cause.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,061
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: I have a problem with my code I think

    Can you post the lines of code in the applet that is creating the Ejemplo1 object?

    What is the relationship between the applet and the Ejemplo1 class?

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with my code I think

    This is the code where I have put the applet, it is a htm archive which the name is: Ejemploprueba.htm


    the code is:


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>

    <head>

    <title></title>

    </head>

    <body>

    <applet code="Ejemplo1.class" height="150" width="300">

    </applet>

    </body>

    </html>


    I expect you can resolve the problem, thanks!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,061
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: I have a problem with my code I think

    Your class MUST extend an applet class.
    public class Ejemplo1 extends Frame

    Your code is not written to be an applet. Go to this site and Find applet:
    http://download.oracle.com/javase/tu...ybigindex.html

    and read about applets.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with my code I think

    Thanks I didn´t realise the program starts with the class frame not applet class, how you should suppose I am still a beginner in program in java and i don't still realise sometimes with this kinds of things Thanks a lot!

Similar Threads

  1. problem in my code
    By dhruvguys in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2011, 05:18 AM
  2. What is the Problem With This Code?
    By olufemi1712 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 18th, 2011, 09:05 AM
  3. problem with code
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 07:06 AM
  4. Problem with the code
    By noFear in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 9th, 2010, 10:28 AM
  5. problem in my code
    By wannabe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2010, 07:53 AM