Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

add a graphic to applet

Posted on 1998-08-24
7
Medium Priority
?
246 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

609 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