|
-
Oct 4th, 2004, 08:48 PM
#1
Thread Starter
Dazed Member
jpg not showing in canvas
I have a class that adds the following class to a container. Then shows the JFrame that contains the container. ie container.add(new MovieImage()); When the JFrame is made visible the image does not show. The image only appears when the JFrame is either resized or iconified then deiconified. Any suggestions as to how i could go about getting the image to appear the first time the JFrame is drawn? Thanks.
Code:
class DrinkImage extends Canvas{
public DrinkImage(){
setSize(25,50);
}
public void paint(Graphics g){
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage("C:\\Beer.jpg");
g.drawImage(image,10,10,null);
}
}
Last edited by Dilenger4; Oct 28th, 2004 at 06:05 PM.
-
Oct 5th, 2004, 02:46 PM
#2
Frenzied Member
Have you tried the repaint() method? It would have to be something along that line since it only shows after its resized. Not sure though...
-
Oct 5th, 2004, 03:06 PM
#3
Thread Starter
Dazed Member
Yes i tried the repaint() method with not much luck though.
-
Oct 5th, 2004, 03:56 PM
#4
Frenzied Member
I had this same problem in VB.NET one time and the only way I could get it to work is make a bitmap and set the image equal to the bitmap. Im not sure if there is even a way to make a bitmap in java, but thats the way I got it fixed in another language. Again not sure if that will work though...
-
Oct 5th, 2004, 04:23 PM
#5
Thread Starter
Dazed Member
The canvas is added to the container c. Now when the JFrame rv is made visible the paint method should be fired off to draw the image. It's only fired off when everything needs to be drawn a second time.
Code:
// constructor
c.add(new DrinkImage());
rv.setSize(400,500);
rv.setVisible(true);
}
}
class DrinkImage extends Canvas{
public DrinkImage(){
setSize(25,50);
}
public void paint(Graphics g){
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage("C:\\Beer.jpg");
g.drawImage(image,10,10,null);
}
}
Last edited by Dilenger4; Oct 5th, 2004 at 04:29 PM.
-
Oct 7th, 2004, 06:52 AM
#6
Addicted Member
i've messed around with similair things, and generally i still had to call super.paint(g) and then after these do my extra bits of painting.....
-
Oct 7th, 2004, 02:37 PM
#7
Thread Starter
Dazed Member
The way ive always understood it was that paint() is automatically invoked by the awt thread when the size of the component has changed or been uncovered. This sounds correct becasue if i change the size of my JFrame that holds the canvas the image appears, also when the JFrame is iconified then deiconified the image is there so paint is being called. Now i should be able to call repaint() in my application but from what i understand repaint() calls update() which eventually calls paint(); Calling repaint() is not working for me at all so is there another alternative?
-
Oct 16th, 2004, 06:18 PM
#8
Thread Starter
Dazed Member
I was thinking about not displaying the JFrame until the image is drawn but i don't know if this would work and i know nothing about using an ImageObserver.
-
Oct 26th, 2004, 11:42 AM
#9
Banned
I don't know if this is any help at all.
why don't u use media tracker for loading the image.. here is a
sample code..
Code:
MediaTracker tracker = new MediaTracker(this);
Image img = Toolkit.getDefaultToolkit().getImage(<full path of mage and file name [I used getcanonical path or file]>);
tracker.addImage(img, 0);
try {
tracker.waitForAll();
}
catch (InterruptedException ex) {
}
after that u can use img for drawing... worked for me.. although I applied the image to a Jpanel... worth a try!
-
Oct 26th, 2004, 11:23 PM
#10
Thread Starter
Dazed Member
Works great! Thanks.
Code:
class MovieImage extends Canvas{
private MediaTracker mt;
public MovieImage(){
mt = new MediaTracker(this);
setSize(25,50);
}
public void paint(Graphics g){
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage("C:\\Beer.jpg");
mt.addImage(image, 0);
try {
mt.waitForAll();
}catch (InterruptedException ex){}
g.drawImage(image,10,10,null);
}
}
-
Oct 27th, 2004, 01:49 AM
#11
Banned
Originally posted by Dilenger4
Works great! Thanks.
Code:
class MovieImage extends Canvas{
private MediaTracker mt;
public MovieImage(){
mt = new MediaTracker(this);
setSize(25,50);
}
public void paint(Graphics g){
Toolkit tk = Toolkit.getDefaultToolkit();
Image image = tk.getImage("C:\\Beer.jpg");
mt.addImage(image, 0);
try {
mt.waitForAll();
}catch (InterruptedException ex){}
g.drawImage(image,10,10,null);
}
}
glad 2 be of any help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|