Results 1 to 5 of 5

Thread: Applet Icon

  1. #1
    arimakidd is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Question Applet Icon

    Here is a simple class to just highlight the use of Frame Object. I am however at the same time interested in getting an image icon, or title icon or applet icon, what ever is the proper name. I thought of using the setIconImage method but on checking it, I realized that an image object is required. This is where I got stomped as the image class has all sort of methods and apparently all kinds of other methods have to be used. I am hoping this is not the case. The image I intend to use is in the .ico format and is placed in the same folder as the class I am using. Here is the code thus far that works.
    Java Code:
    //FrameDemo
    //Demonstrates a very simple frame
    //Danon Knox, 05/00
    
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class FrameDemo extends Applet implements ActionListener
    {
    	Frame frmHello = new Frame("Hello There!");
    	Button btnShowFrame = new Button("Show Frame");
    	Button btnHideFrame = new Button("Hide Frame");
       [COLOR="Red"] //this is where i failed to instantiate the image object
        //Image favicon = getIconImage("favicon.ico");[/COLOR]
    
    
    	public void init()
    	{
    		add(btnShowFrame);
    		add(btnHideFrame);
    
    		btnShowFrame.addActionListener(this);
    		btnHideFrame.addActionListener(this);
    
    		frmHello.setFont(new Font("SansSerif", Font.BOLD, 30));
    		frmHello.add(new Label("Hello there!"), BorderLayout.CENTER);
    
    		[COLOR="red"]// this was my failed attempt
    		//frmHello.setIconImage(favicon);[/COLOR]
    	}//end init
    
    
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == btnShowFrame)
    		{
    			frmHello.setVisible(true);
    			frmHello.setSize(100,100);
    			frmHello.pack();
    
    		}
    		else
    		{
    			frmHello.setVisible(false);
    			frmHello.dispose();
    		}
    	}
    }
    I have highlighted my attempt to put the icon image in the frame. This is a very simple class that simply hides and shows a frame based on the click of a button.

  2. #2
    arimakidd is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Question New Problem

    I have addressed some issues in the previous code. I will highlight the code of importance.
    Before
    Java Code:
    //this is where i failed to instantiate the image object
    Image favicon = getIconImage("favicon.ico");
    .
    .
    .
    // this was my failed attempt
    frmHello.setIconImage(favicon);
    After
    Java Code:
      //this is where i failed to instantiate the image object
    Image favicon = Toolkit.getDefaultToolkit().getImage("favicon.gif");
    .
    .
    .
    // this was my failed attempt
    frmHello.setIconImage(favicon);
    Now when run the applet with these adjustments I get the following error:
    Java Code:
    java.security.AccessControlException: access denied (java.io.FilePermission favicon.gif read)
    What do I do?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Applets are NOT allowed to read files from the local disk. The code should be written to get the image from the Applet's codebase. There is sample code on the forum showing how to do that.
    Or you can give the applet permission by changing the .java.policy file or signing the applet. Again there are examples on the forum on how to do that.

  4. #4
    bugmenot is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Applets are NOT allowed to read files from the local disk. The code should be written to get the image from the Applet's codebase. There is sample code on the forum showing how to do that.
    Or you can give the applet permission by changing the .java.policy file or signing the applet. Again there are examples on the forum on how to do that.
    Norm,

    Could you please point me in the direction of the sample code on the forum showing how to write code "to get the image from the Applet's codebase"? I have searched in vain for the info.

    Thanks!

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    getResourceAsStream() - it's in most Applet threads
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Yes No Icon MessageBox in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:57 PM
  2. Example of an icon that changes form
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:18 AM
  3. icon
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-16-2008, 08:34 PM
  4. How to set an Icon in a Label?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 01:38 PM
  5. To add an icon to my project
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2007, 04:14 PM

Posting Permissions

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