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.....*^^*
bhuey_lingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cheekycjCommented:
here is some code that I used:
public class DisplayImage extends JPanel {
  Image img;
 
  DisplayImage(String img){
// img string is the location of the file    
    this.img = Toolkit.getDefaultToolkit().getImage(img);
    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()
}

Then in your frame you need to just do the following:
DisplayImage myImg= new DisplayImage("filename.jpg");
JFrame.add(myImg);

CJ
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhuey_lingAuthor Commented:
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

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

You can also extend component or canvas.

CJ
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

bhuey_lingAuthor Commented:
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
0
bhuey_lingAuthor Commented:
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
0
cheekycjCommented:
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

0
cheekycjCommented:
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
0
bhuey_lingAuthor Commented:
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?
0
cheekycjCommented:
Did you have any luck??

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

CJ
0
cheekycjCommented:
Did you have any luck??

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

CJ
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.