Solved

add a graphic to applet

Posted on 1998-08-24
7
241 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
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 49
servlet example 11 42
login jsp example 24 51
configure dependency in POM for new database 3 27
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

825 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