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.....*^^*
thnx for reply.....*^^*
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just extend Panel instead of JPanel.. I used JPanel because my application was using Swing.
You can also extend component or canvas.
CJ
You can also extend component or canvas.
CJ
ASKER
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.jp g");
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
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(
MediaTracker mt = new MediaTracker(this);
mt.addImage(img,0);
try {
mt.waitForID(0);
} catch(InterruptedException
} // 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
ASKER
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
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.jp g");
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
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(
MediaTracker mt = new MediaTracker(this);
mt.addImage(img,0);
try {
mt.waitForID(0);
} catch(InterruptedException
} // 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
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
ASKER
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.jp g");
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?
here r my code:import java.awt.*;
public class DisplayImage extends Frame{
Image img;
DisplayImage(String title){
super(title);
this.img = Toolkit.getDefaultToolkit(
MediaTracker mt = new MediaTracker(this);
mt.addImage(img,0);
try {
mt.waitForID(0);
} catch(InterruptedException
} // 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
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
I haven't had a chance to look at it yet.
CJ
ASKER
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