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");
}
}
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");
}
}