Link to home
Start Free TrialLog in
Avatar of alanlee081398
alanlee081398

asked on

add a graphic to applet

My code for my java applet is:

import java.awt.*;
import java.applet.*;


public class gui extends Applet
{
      //searchb is the search button
      Button searchb;      
      //this is where the keyword is typed into
      TextField entry;
      //displays 'enter keyword' beside the entry box
      Label prompt;
      //the string that holds the keyword
      //this is the results box
      List textbox = new List(4, false);
      List status_box = new List(3, false);
      //this is the info button, displays author and supervisor
      Button info;
      
      
      
      
      public void init()
      {
            //sets the layout of the applet
            setLayout(new BorderLayout());
            Panel panel1 = new Panel();
            Panel panel2 = new Panel();
            Panel panel3 = new Panel();
            Panel panel4 = new Panel();
            Panel panel5 = new Panel();
            
            panel2.setLayout(new GridLayout(3,1,20,15));
            //panel5.setLayout(new GridLayout(2,1));
            //adds the various components to the layout
            prompt = new Label("Enter keyword");
            panel1.add(prompt);
            entry = new TextField(10);
            panel1.add(entry);
            searchb = new Button("Search");
            panel1.add(searchb);
            
            
            
            panel2.add(textbox);
            panel2.add(status_box);
            
            info = new Button("?");
            panel1.add(info);
            //panel5.add(status_box);
            
            add("North", panel1);
            add("Center", panel2);
            add("East", panel3);
            add("West", panel4);
            add("South", panel5);
      }
}


As you prob notice, it's pretty bland.. I'm trying to add a graphic at the 'south' layout (or panel5). How can i add a GIF file there?
Avatar of aziz061097
aziz061097

//To get the image use either this or the next line
//Image img = Toolkit.getDefaultToolkit().getImage("Logo.gif");


Image img = getImage(getDocumentBase(),"Logo.gif");
Graphics g = SouthPanel.getGraphics();
g.drawImage(img, 0, 0, 615, 430, this);       


You can write the above code in the maybe somewhere in the constructor of the panel (I am assuming that you have subclassed Panel , which is a good way to do such programs) and then add the panel to the layout.
Avatar of alanlee081398

ASKER

Thanks Aziz..
However, i still can't get it to work.. the code compiles but I get a null.exception statement in Netscape when I try to run it. My code is now:

import java.net.URL;
import java.awt.*;
import java.applet.*;


public class gui extends Applet
{
      //searchb is the search button
      Button searchb;      
      //this is where the keyword is typed into
      TextField entry;
      //displays 'enter keyword' beside the entry box
      Label prompt;
      //the string that holds the keyword
      //this is the results box
      List textbox = new List(4, false);
      List status_box = new List(3, false);
      //this is the info button, displays author and supervisor
      Button info;
      
      
      
      public void init()
      {
            
            
            
            //To get the image use either this or the next line
    //Image img = Toolkit.getDefaultToolkit().getImage("Logo.gif");


   
            
            
            
            //sets the layout of the applet
            setLayout(new BorderLayout());
            Panel panel1 = new Panel();
            Panel panel2 = new Panel();
            Panel panel3 = new Panel();
            Panel panel4 = new Panel();
            Panel panel5 = new Panel();
            
            panel2.setLayout(new GridLayout(3,1,20,15));
            //panel5.setLayout(new GridLayout(2,1));
            //adds the various components to the layout
            prompt = new Label("Enter keyword");
            panel1.add(prompt);
            entry = new TextField(10);
            panel1.add(entry);
            searchb = new Button("Search");
            panel1.add(searchb);
            
            Image img = getImage(getDocumentBase(),"logo.gif");
   
            Graphics g = panel5.getGraphics();
    g.drawImage(img, 0, 0, this);

            
            panel2.add(textbox);
            panel2.add(status_box);
            
            info = new Button("?");
            panel1.add(info);
            //panel5.add();
            
            add("North", panel1);
            add("Center", panel2);
            add("East", panel3);
            add("West", panel4);
            add("South", panel5);
            
      }
      
      
}


couple of things.
1. You're running into the null pointer exception because panel 5 haven't been
    added to a container yet. Move the getImage lines below the add statements
    and you'll avoid the null pointer exception.

2. You really want to use another way to load the image. The current way might
    run into some security violations. Try:

try {
       URL url = new URL(getDocumentBase()+"logo.gif");
       img = getImage(url);
} catch (MalformedURLException e) {System.out.println("bad url");}

This will bypass the security error. Remember to include java.awt.net.*;
I note the above comments and I've tried to add the graphic. Apparently the graphic is read into my applet but it is not displayed anywhere! HELP! I don't know what is giong wrong, the applet compiles ok but no pic anywhere. Someone suggested that I add a canvas into the panel that I want to use.... is that a feasible idea and if so how do I do it?
Please could someone try to run and compile my prog and show that the graphic does indeed appear! Thanks a lot!

Alan
Assuming that you have loaded the image the way I have suggested...

In your applet's paint method, try to draw the image and see if you'll
come up with anything. comment out the panels for now and concentrate
on whether or not you can get an image to show.

So in your paint method, add this:

g.drawImage(img, 10, 10, this);

and see if anything shows up.
ASKER CERTIFIED SOLUTION
Avatar of milen_n
milen_n

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great! Thanks so much.. my applet is complete now! :)