Solved

add a graphic to applet

Posted on 1998-08-24
7
239 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
7 Comments
 
LVL 2

Expert Comment

by:aziz061097
Comment Utility
//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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:alanlee081398
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great! Thanks so much.. my applet is complete now! :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now