Using BufferedImage in java

Joined
Oct 31, 2008
Messages
2
Reaction score
0
I am new to java programming. I have to write a code where I have to take a image from file, create into buffered image and then paint this image on the component. I have given it a try, but it doesn't work. Can someone please tell me as to where am I making a mistake here? There seems to be a big confusion somewhere in my concepts.

Thnx in advance!!

Here is my code

import java.awt.*;
import java.awt.image.*;

public class ImageUtilities extends Frame{



BufferedImage img;
Frame f;
ImageUtilities(String filename){

f = new Frame();

img = getBufferedImage(filename, f);

//f.setVisible(true);
System.out.println("This is "+this);
f.setLayout(null);
f.setSize(400,400);
//repaint();
f.setVisible(true);

}
public static BufferedImage getBufferedImage(String imageFile,
Component c) {
Image image = c.getToolkit().getImage(imageFile);
waitForImage(image, c);
int width = image.getWidth(c);
int height = image.getHeight(c);
System.out.println("Width"+width+"height"+height);
BufferedImage bufferedImage =
new BufferedImage(image.getWidth(c), image.getHeight(c),
BufferedImage.TYPE_INT_ARGB );
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, null);
//c.paint(g2d);
System.out.println("Exitting getBuffimage");
return(bufferedImage);
}


public static boolean waitForImage(Image image, Component c) {
MediaTracker tracker = new MediaTracker(c);
tracker.addImage(image, 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
System.out.println("Exitting waitForImages");
return(!tracker.isErrorAny());
}

public void paint(Graphics g){
System.out.println("In paint");
Graphics2D g2d = (Graphics2D)g;
setBackground(Color.blue);
super.paint(g);
g2d.drawString("hello",20,20);
g2d.drawImage(img, 0 ,0, f); //is this correct???

}

public static void main(String args[]){
new ImageUtilities("xxx.gif");


}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top