Results 1 to 9 of 9
  1. #1
    Vampire5 is offline Member
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Question Draw Image on JPanel in Applet

    I have a JPanel in an Applet, and I want to draw images from the JPanel. My source-code looks like this (sort of)

    gnp/GNP.java
    gnp/panels/Game.java
    assets/gfx/bg.jpg

    I want to access the bg.jpg from the Game class (which is a JPanel that I use in the GNP class). How would I access this file? I don't want to put down the whole entire path to the file, just what you see above. I've tried using the getCodeBase function from the Applet and giving the URL to the JPanel, but then it gives me a permissions error as follows:

    java.security.AccessControlException: access denied ("java.io.FilePermission" "file:/home/nicolas/Documents/Programs/Java/GNP2/bin/assets/gfx/bg.jpg" "read")
    at java.security.AccessControlContext.checkPermission (AccessControlContext.java:372)
    at java.security.AccessController.checkPermission(Acc essController.java:559)
    at java.lang.SecurityManager.checkPermission(Security Manager.java:549)
    at net.sourceforge.jnlp.runtime.JNLPSecurityManager.c heckPermission(JNLPSecurityManager.java:284)
    at java.lang.SecurityManager.checkRead(SecurityManage r.java:888)
    at java.io.File.canRead(File.java:760)
    at javax.imageio.ImageIO.read(ImageIO.java:1300)

    Would it be easier if I put the assets directory in the gnp directory? and even if so, how would I access the files?

    If I didn't put enough info, just ask.

    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Draw Image on JPanel in Applet

    Post moved to Applet section.
    java.security.AccessControlException: access denied ("java.io.FilePermission" ...
    An applet needs permission to read files from a user's disk.
    Some ways to give permission:
    sign the jar file
    add permissions to the user's .java.policy file

    There are some ideas here: https://blogs.oracle.com/java-platfo...ments_for_rias

    Would it be easier if I put the assets directory in the gnp directory?
    Can all the resources be placed in the jar file? Then there wouldn't be a need to read from a disk.
    Last edited by Norm; 06-12-2014 at 01:15 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Vampire5 is offline Member
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Draw Image on JPanel in Applet

    Quote Originally Posted by Norm View Post
    Can all the resources be placed in the jar file? Then there wouldn't be a need to read from a disk.
    Yes, these are just images that I would like to use in my game (JPG and PNG). If I moved the assets directory into the gnp so it would look like this

    gnp/assets/gfx/bg.jpg
    gnp/GNP.java
    gnp/panels/Game.java

    How would I direct to the files using the JPanel (which is the Game class).

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Draw Image on JPanel in Applet

    Put the image files in the jar file, use the getResourceAsStream method to get a stream from the image and read that stream into an image with the ImageIO class's method.

    Do a Search here on the forum for sample code.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Vampire5 is offline Member
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Draw Image on JPanel in Applet

    Alright, I'm getting some progress, but I still don't get a good image. I tried doing this:
    Java Code:
    try {
    			bg = ImageIO.read(this.getClass().getResource("/assets/gfx/bg.jpg"));
    			bluePaddle = ImageIO.read(this.getClass().getResource("/assets/gfx/Paddle_blue.png"));
    			redPaddle = ImageIO.read(this.getClass().getResource("/assets/gfx/Paddle_red.png"));
    			greenPaddle = ImageIO.read(this.getClass().getResource("/assets/gfx/Paddle_green.png"));
    			purplePaddle = ImageIO.read(this.getClass().getResource("/assets/gfx/Paddle_purple.png"));
    			balli = ImageIO.read(this.getClass().getResource("/assets/gfx/Ball_orange.png"));
    		} catch(IOException ioe) {
    			ioe.printStackTrace();
    		}
    But that just ended with this:
    Draw Image on  JPanel in Applet-screenshot-2014-06-11-21-27-44.png
    It's progress, I have a feeling that little dot is one of the images. Funny thing is that I don't get any errors. By the way, the images are not as small as they appear in the photo, they're quite bigger.

    Then, when I change getResource to getResourceAsStream it does the same thing, but without the error.
    Last edited by Vampire5; 06-12-2014 at 04:42 AM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Draw Image on JPanel in Applet

    Please make a small, complete program that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Draw Image on JPanel in Applet

    Where exactly have you put the images in the jar file?
    And did you get an error?
    Your last post wasn't too clear on that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Vampire5 is offline Member
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Draw Image on JPanel in Applet

    Alright, I've solved the problem, Norm's suggestion helped a lot. I made a small program and I defined the BufferedImages like this:

    Java Code:
    BufferedImage img = ImageIO.read(this.getClass().getResource("assets/gfx/Ball.png"));
    Of course, of this the new structure of my code looks like this:

    gnp/GNP.java (the main class which is a JApplet)
    gnp/Game.java (the Panel from which I am calling to the image file)
    gnp/assets/gfx/Ball.png (the image file)

    Also, there were times when I didn't get errors when running, that's because I forgot to put super.paintComponent(g) at the beginning of my paintComponent function in my JPanel.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Draw Image on JPanel in Applet

    Just so you know:
    "assets/gfx/Ball.png"
    That path is relative to the class you are calling getResource on.
    In your case one of the classes in the gnp package, so the full path from the top of the jar file is "gnp/assets/gfx/Ball.png".
    The reason this path didn't work:
    "/assets/gfx/bg.jpg"
    is because that starting "/" is telling the getResource() method that the path is from the top of the jar file, not relative to the class file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  2. Draw on JPanel?
    By PhQ in forum New To Java
    Replies: 2
    Last Post: 07-16-2010, 11:19 PM
  3. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 05:01 AM
  4. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •