Link to home
Start Free TrialLog in
Avatar of bhuey_ling
bhuey_ling

asked on

how to put image on a frame?

i have a frame and dialog box and i would like to put image on this frame and dialog. How to do it?.......as i know in applet, we can use getcodeBase(), or URL something........but this is not support by frame and dialog ....

thnx for reply.....*^^*
ASKER CERTIFIED SOLUTION
Avatar of cheekycj
cheekycj
Flag of United States of America image

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
Avatar of bhuey_ling
bhuey_ling

ASKER

hello

it got a error that have no JPanel class in the DisplayImage...is JPanel is another file that u create ur own or i need to import others utility header......i just import

import java.net.*;
import java.applet.*;

thnx for reply

bhl

Just extend Panel instead of JPanel.. I used JPanel because my application was using Swing.

You can also extend component or canvas.

CJ
hello....

following r my code:

import java.awt.*;

public class DisplayImage extends Panel {
  Image img;
   
  DisplayImage(String img){
// img string is the location of the file    
    this.img = Toolkit.getDefaultToolkit().getImage("think.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(img,0);
    try {
        mt.waitForID(0);
    } catch(InterruptedException ie){ System.out.println("IO error in loading Image"); }
  } // Constructor
   
   
  public void paint(Graphics g){
    g.drawImage(imgFile, 0, 0, imgMap.getWidth(this), imgMap.getHeight(this), this);
  } // method paint()
}


there r 3 eeror:

undefined imgFile, imgMap

is the imgFile means i should put the filename or varible img....and wat the imgMap means?

thnx for reply

hlb
hello....

there r another errors when i write


DisplayImage myImg= new DisplayImage("think.jpg");
 Frame.add(myImg);

error:

Can't make static reference to method java.awt.Componen
d(java.awt.Component) in class java.awt.Container.
        Frame.add(myImg);
                 ^
..\DisplayImage.java:11: Incompatible type for method. Can't convert java.lan
ring to java.awt.Image.
    mt.addImage(img,0);
                ^
thnx for reply

hlb
in the line:
g.drawImage(imgFile, 0, 0, imgMap.getWidth(this), imgMap.getHeight(this), this);
 
replace imgMap with img

imgFile is also supposed to img

here is the code again:
===============================
import java.awt.*;

public class DisplayImage extends Panel {
  Image img;
   
  DisplayImage(){
    this.img = Toolkit.getDefaultToolkit().getImage("think.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(img,0);
    try {
        mt.waitForID(0);
    } catch(InterruptedException ie){ System.out.println("IO error in loading Image"); }
  } // Constructor
   
   
  public void paint(Graphics g){
    g.drawImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
  } // method paint()
}

===============================

It should work now.

CJ

for the static error:

You may have to add a panel to your frame and then add the image on to that.

Like

static Panel test1=new Panel(args...);
frame.add(test1);
DisplayImage myImg= new DisplayImage();
test1.add(myImg);


OR I put frame there as a generic term.

Whateveryou your app name is that is what goes there.

public myApp extends Frame {

}
Then you would use
myApp.add(myImg);

for the second one..hmm. could you try recompiling after these fixes and let me know if it still pops up.

CJ
hello......i change it to extends frame now....i have "think.jpg" at same dir with the class file

here r my code:import java.awt.*;

public class DisplayImage extends Frame{
  Image img;
   
  DisplayImage(String title){
    super(title);      
    this.img = Toolkit.getDefaultToolkit().getImage("think.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(img,0);
    try {
        mt.waitForID(0);
    } catch(InterruptedException ie){ System.out.println("IO error in loading Image"); }
  } // Constructor
     
  public boolean handleEvent(Event event){
    switch(event.id){      
      case Event.WINDOW_DESTROY:
        hide();
        removeAll();
        dispose();
      break;
    }  
     return true;  
  }  
  public void paint(Graphics g){
    g.drawImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
  } // method paint()
}

i call it out in my main program by:


if(label.equals("Draw Graph")){
  graph.resize(635,400);
  graph.start(inputX, outputY,l,false);
  graph.show();
  DisplayImage f=new DisplayImage("Think About It");
      f.show();
}

there r a frame pop out but no pic ..why?
Did you have any luck??

I haven't had a chance to look at it yet.

CJ
Did you have any luck??

I haven't had a chance to look at it yet.

CJ