Results 1 to 9 of 9
  1. #1
    BangJava is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default Background Image of Applet

    I have an Applet i need to set backgroound image for it on button click. Like thr will be a browse button for getting all images of the local PC and set it as background image. I tried doing it but it gives Access Denied and when i modified the policy files it works for the first browse but after that it doesnt work. Can any one tell me reason for it and how i can improve it?

    Regards,
    Pranaw

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Sounds like you are trying to read files from the local system.
    Its better to use URLs to access the image file. Use the getCodeBase() method to get the URL to location of the image file and then use that to read the file. Don't remember which class and method , you'll have to read the API doc to find it. Perhaps if you search on getCodeBase() you'll find some code.
    it works for the first browse but after that it doesnt work
    Which browse does it work in and which one does it not work in?

  3. #3
    BangJava is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    Actually when i choose image for 1st time, background gets set by the image but after that it takes long time to update with second time choosed image.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    you'll have to show your code that reads the image and displays it.

  5. #5
    BangJava is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JPanel;

    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.swing.*;
    import java.io.File;

    import java.awt.*;

    import javax.imageio.ImageIO;

    public class BackgroundImage extends JApplet implements ActionListener {

    JPanel panel;
    JButton button = new JButton("Browse");
    JFileChooser filechooser;
    File Image_path=null;
    BufferedImage img = null;
    URL imageSrc ;
    ImageIcon img1;
    public BackgroundImage() {

    Container c = getContentPane();
    filechooser = new JFileChooser();
    filechooser.setFileSelectionMode(JFileChooser.FILE S_ONLY);
    button.addActionListener(this);
    System.out.println("inside constructor");
    panel = new JPanel() {
    public void paintComponent(Graphics g) {
    //ImageIcon img = new ImageIcon("new1.jpg");
    System.out.println("inside JPanel");
    g.drawImage(img, 0, 0, null);

    super.paintComponent(g);
    }
    };
    panel.setOpaque(false);
    panel.add(button);
    c.add(panel);
    }

    public void actionPerformed(ActionEvent e)
    {

    filechooser.showOpenDialog(this);
    Image_path=filechooser.getSelectedFile().getAbsolu teFile();
    //System.out.print(filechooser.getSelectedFile().get AbsoluteFile());
    //System.out.print("\n");

    try{


    img=ImageIO.read(Image_path);
    //System.out.print(img);
    }
    catch(IOException e1){
    e1.printStackTrace();
    //System.out.print("Error");
    }

    }

    private BackgroundImage objBackgroundImage;

    public void init() {
    try{
    objBackgroundImage=new BackgroundImage();

    System.out
    .println("------------------Execution In Process------------------");
    }
    catch(java.security.AccessControlException a ){
    a.printStackTrace();
    }
    }

    }

    this works fine in eclipse but have problem when embeded in html page. After modifying Java.policy file i am geting it working for the first execution only....

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Do you get any messages in the browser's java console?
    geting it working for the first execution only
    what is "the first execution"? What do you mean by execution?
    The browser loads an HTML page with an applet and the applet does its thing. Then what?

  7. #7
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    g.drawImage(img, 0, 0, null);
    you might want to set a size and a place to draw. Null here is telling it to paint to nothing I think. It's the Image observer. try setting it to the JPanel you are painting to.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    you might want to set a size and a place to draw.
    The place is set: [0, 0]. The size is the unscaled size of the image.

    Null here is telling it to paint to nothing I think.
    Read the API and you won't have to use your (creative) imagination.

    It's the Image observer. try setting it to the JPanel you are painting to.
    Irrelevant for a non-animated image loaded on the EDT via ImageIO#read(...).

    db

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Applet Image location
    By MarkWilson in forum NetBeans
    Replies: 3
    Last Post: 06-30-2008, 07:37 AM
  2. Center background image
    By Floetic in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 05:50 PM
  3. Replies: 2
    Last Post: 03-24-2008, 05:29 AM
  4. Background image in java
    By toby in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 07:15 AM
  5. Why this image background is black ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:24 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
  •