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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Images works on an applet, but doesn't work when an applet is in a JFrame.

  1. #1
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Thumbs up [SOLVED] Images works on an applet, but doesn't work when an applet is in a JFrame.

    Hi guys,
    i'm new here and i am new in java and using ECLIPSE.

    So, very shortly. The problem is explained in the title.

    In an applet my mini game looks like this: single Applet
    As u can see, there are 4 images, witch i coded in this way (example of background image):
    public URL url;
    public Image bg;
     
    public void init(){
    ...
     
    try{
    		url = getDocumentBase();
    		}  catch(Exception e){			
    		}
    		bg = getImage(url, "images / bg.png"); 
    // i put there spaces cuz this site auto edits image patch.
    }
     
    public void paint(Graphics g){
    		g.fillRect(0, 0, getSize().width, getSize().height); 
    		g.drawImage(bg, 0, 0, this);
                    ...
                    super.paint(g);
    }

    Everything looks just fine in an applet, as u can see in a picture above.

    But when I run it in JFrame, it looks like this: Applet in JFrame (do not pay your attention in red circles)

    0 images are draw, only simple colors that are set up...
    I tried to use 'getCodeBase()' in getImage method instead of 'url', but then an applet w/ JFrame don't even start and errors appear.

    Where is a problem? It looks like an applet in JFrame can't read where the images is, or smth like that...
    My images are placed in src/images folder.

    can't figure out what's wrong...

    Should I use other image drawing methods?
    Last edited by scorpas; May 18th, 2012 at 02:17 PM.


  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: Images works on an applet, but doesn't work when an applet is in a JFrame.

    You've got empty 'catch' clauses in your code. Just never, ever do that. Put a 'e.printStackTrace()' in there, or don't catch the Exceptions (where that's allowed by the signature of the method you're overriding) and you'll be able to answer your own question.

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    scorpas (May 18th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    errors appear.
    Please post the full text of any error messages.

    Applets require the services of an AppletContext normally provided by a browser. Unless you create an appletcontext and set it for the applet, the applet can not use any of the services provide by the appletcontext. See the API doc to see what methods are involved.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  6. #4
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Quote Originally Posted by Sean4u View Post
    Put a 'e.printStackTrace()' in there
    ok, i did that.

    My JFrame works like that: I press run (Window) on Eclipse, JFrame appears, then i click Start button, and applet shows up in the same window.

    So it runs good, but when i press Start console goes red w/ errors
    However i can still play the game, but there are 0 images loaded...

    console looks like that:
    java.lang.NullPointerException
    	at java.applet.Applet.getDocumentBase(Unknown Source)
     
    	at Zaidimas.Main.init(Main.java:123) // try{
    			          //url = getDocumentBase(); - error on this line
    	at Zaidimas.Window.GameStart(Window.java:70) // app.init(); - in init method i use getDocumentBase() method.
    	at Zaidimas.Window$7.actionPerformed(Window.java:205) // GameStart(); - in this i use init() method.
     
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(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.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    From that I can say, that there is something wrong w/ getDocumentBase(); in my init(); method.

    I tried to comment all this:
    try{
    			url = getDocumentBase();
    		}catch(Exception e){
    			e.printStackTrace();
    		}

    And put getCodeBase() instead of 'url', but console still remains red of errors and an applet don't even show up:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at java.applet.Applet.getCodeBase(Unknown Source)
     
    	at Zaidimas.Main.init(Main.java:128) // block = getImage(getCodeBase(), "images/block.png"); - error on this line
    	at Zaidimas.Window.GameStart(Window.java:70) // app.init(); - in this method i use getCodeBase();
    	at Zaidimas.Window$7.actionPerformed(Window.java:205) // GameStart(); - in this - init();
     
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(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.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    So the problem is in init(); method and it looks like Eclipse (or i should say JFrame) don't understand getCodeBase() or getDocumentBase() methods.

    I need to change somehow those methods, to get images on an applet in JFrame.

    Any ideas?

    By the way, if i run only an applet - everything works just fine...
    Last edited by scorpas; May 18th, 2012 at 06:00 AM.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    to get images on an applet in JFrame.
    Did you read post#3 about applets and browsers and appletContext?

    Unless you provide the services of the appletcontext, its methods will not work.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  9. #6
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Quote Originally Posted by Norm View Post
    Did you read post#3 about applets and browsers and appletContext?

    Unless you provide the services of the appletcontext, its methods will not work.
    But i'm creating it not for the web or browsers...
    That just a simple java game, that will be runnable from .jar file.

    Anyway, i don't get it how i cant provide it.

    I need to use this method:
    HTML Code:
    Applet.getAppletContext() 
              Determines this applet's context, which allows the applet to query and affect the environment in which it runs.
    or what?
    where i should use it? in JFrame class or in an applet class?
    Last edited by scorpas; May 18th, 2012 at 06:47 AM.

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    If you are not going to use the applet code in a browser that provides the applet stub and context, then you will need to rewrite the code so it does not extend the applet class or use any of the applet methods.

    It is possible to write code for the applet context and stub and use the setStub() method to provide those services to your applet.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  12. #8
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Where can i find any example how those methods are useing?

  13. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Ask google

    An idea:
    Create a class that implements AppletStub. Create an instance of your applet class and call its setStub() method passing it an instance of your class that implements AppletStub.. Put println statements in all its methods and see which are called by your applet. add code to those methods to do what they need to do.
    Last edited by Norm; May 18th, 2012 at 07:45 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  15. #10
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    damn, im just a rookie in programming, ur language is too good to understand for me ...

    package Zaidimas;
     
    import java.applet.AppletContext;
    import java.applet.AppletStub;
    import java.net.URL;
     
    public class MyStub implements AppletStub {
    	Main applet = new Main();
     
    	@Override
    	public void appletResize(int width, int height) {
    		// TODO Auto-generated method stub
    	}
    	@Override
    	public AppletContext getAppletContext() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	@Override
    	public URL getCodeBase() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	@Override
    	public URL getDocumentBase() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	@Override
    	public String getParameter(String name) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	@Override
    	public boolean isActive() {
    		// TODO Auto-generated method stub
    		return false;
    	}
    	public final void setStub(AppletStub stub){
    	}
    }
    whats next...


    btw, maybe there is another way to create GUI for an applet without losing images loading?
    Last edited by scorpas; May 18th, 2012 at 09:28 AM.

  16. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Create an instance of your applet class:
    yourApplet = new YourApplet();

    Call its setStub() method passing it an instance of your class that implements AppletStub.
    yourApplet.setStub(new MyStub());

    Put println statements in all the MyStub class's methods and see which are called by your applet when it is executed.

    Add code to those methods to do what they need to do.
    Last edited by Norm; May 18th, 2012 at 09:22 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  18. #12
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    first of all, i cant create constructor like that: applet = new YourApplet();
    that's the way how i create it: YourApplet applet = new YourApplet();
    but i got error here: yourApplet.setStub(new MyStub());
    Multiple markers at this line
    -syntax error on token "setStub", = expected after this token.
    -syntax error on token(s), misplaced construct(s)

  19. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Can you post the source code that has the error?
    And the full text of the compiler's error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  21. #14
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    package Zaidimas;
     
    import java.applet.AppletContext;
    import java.applet.AppletStub;
    import java.net.URL;
     
    public class MyStub implements AppletStub {
    	Main applet = new Main();
    	applet.setStub(new MyStub());

    Last line, under setStub red 'waved' line, and when i check on the left what kind of error it is, there are written what i already wrote above about this error.

    compile process is automatic in eclipse, i dont even know where the compiler is

  22. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    The code for setting the AppletStub goes with the applet you are using in post#1. Not in the AppletStub class.
    After you create an instance of your applet class, call its setStub() method.
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  24. #16
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    is there any other ways/methods to draw images?

  25. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Are you trying to draw images or display an existing image? if you want your applet to work without a browser, then you would continue doing what I have suggested. It will take more code that way then writing new code for your app because you have to do everything that the browser would have done .

    If you want to display an image look at the ImageIO class for ways to read an image file.
    If you don't understand my answer, don't ignore it, ask a question.

  26. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  27. #18
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    display existing ones.

    the problem is, that i don't understand ur suggestion.

    my applet is in Main class...

    i tried to create in the Main class getCodeBase() and getDocumentBase() methods.
    again, only in an applet images were draw...

  28. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Those methods are for applets. If you are not going to execute the code as an applet in a browser, you need to get rid of those methods and use methods suitable for applications. Don't extend the applet class.

    Which are you trying to write? An applet for a browser or an app for a PC?

    If you want to read an image from a disk file use the ImageIO class's methods.
    If you don't understand my answer, don't ignore it, ask a question.

  29. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  30. #20
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    an app for a PC (my task is to create something who works on Linux). Runnable from .jar file after double clicks on it.

    as I said, my images are in 'this_project/src/images' folder.

  31. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Get rid of the applet class's methods and use the ImageIO class's read methods to read the image files.
    Will the images be in the jar file or outside of it?
    If inside you should use the read(URL) method and get the URL by using the Class class's getResource() method.
    If outside you could use other versions of the read() method
    If you don't understand my answer, don't ignore it, ask a question.

  32. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  33. #22
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Yeah. Finally, u are genius, man!!!
    THANK YOU!

    It works, but its very laggy
    any ideas how to fix lag?

  34. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    how to fix lag?
    Take out the call to sleep(4000)

    No idea without seeing the code.
    If you don't understand my answer, don't ignore it, ask a question.

  35. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  36. #24
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    sleep change the speed of an applet, but lag still remains.
    i guess it is something with repaint() method.

  37. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    A common mistake is to load the images every time they are displayed instead of doing it one time.
    If you don't understand my answer, don't ignore it, ask a question.

  38. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

Page 1 of 2 12 LastLast

Similar Threads

  1. Trying to make 3 images become random in an Applet. Need help
    By slahsdash in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 9th, 2012, 09:39 PM
  2. Replies: 2
    Last Post: February 24th, 2012, 07:56 PM
  3. Can't get applet to work on Safari..
    By Bertorox in forum Java Applets
    Replies: 10
    Last Post: September 20th, 2011, 10:00 PM
  4. how to convert applet to a JPanel or JFrame
    By flipsyde02 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 29th, 2011, 07:40 AM
  5. [SOLVED] Java Noob, Jframe in Applet.
    By rLLZORS in forum AWT / Java Swing
    Replies: 2
    Last Post: May 5th, 2011, 10:42 AM

Tags for this Thread