Solved

add a graphic to applet

Posted on 1998-08-24
7
244 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:alanlee081398
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:aziz061097
ID: 1222465
//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.
0
 

Author Comment

by:alanlee081398
ID: 1222466
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);
            
      }
      
      
}


0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222467
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.*;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alanlee081398
ID: 1222468
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
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222469
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.
0
 

Accepted Solution

by:
milen_n earned 100 total points
ID: 1222470
Try this. It works fine for me.
1. Modify the panel5 creation line:
    Panel panel5 = new GifPanel(getImage(getCodeBase(), "image.gif"));
2. Define the GifPanel class:
class GifPanel extends Panel
{
      Image m_Background;

      public GifPanel(Image background)
      {
            m_Background = background;
// you may skip this if you don't want the image to be loaded in advance
            MediaTracker m = new MediaTracker(this);
            m.addImage(m_Background, 1);
            try
            {
                  m.waitForID(1);
            }
            catch(InterruptedException e)
            {
            }
      }

      public void paint(Graphics g)
      {
            Dimension d = getSize();
            g.drawImage(m_Background, 0, 0, d.width, d.height, this);
      }

      public Dimension preferredSize()
      {
            return minimumSize();
      }

      public Dimension minimumSize()
      {
            // The size of the image if you don't want to redo the layout
            return new Dimension(100, 100);
      }
}

If you are using JDK 1.0 browser, thogh you will have to use Canvas. The Panel will not work.
0
 

Author Comment

by:alanlee081398
ID: 1222471
Great! Thanks so much.. my applet is complete now! :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question