90 likes | 218 Views
Images. Creating Image objects Create an in-memory drawing surface Downloaded from GIF or JPEG files Create your own ImageProducer. Creating a Drawing Surface. Use the Component class method: createImage () public void paint(Graphics g) { Dimention dim = getSize();
E N D
Images • Creating Image objects • Create an in-memory drawing surface • Downloaded from GIF or JPEG files • Create your own ImageProducer
Creating a Drawing Surface • Use the Component class method: createImage() public void paint(Graphics g) { Dimention dim = getSize(); Image img = createImage(dim.width, dim.height); } Convas c = new Canvas(); Image img = c.createImage(w, h);
Drawing on the surface • You must obtain a Graphics object attached to the in-memory Image object • Use the Image objects getGraphics() method Image img = creatImage(w, h); Graphics gImg = img.getGraphics(); gImg.drawLine(10, 20); gImg.dispose();
Displaying an Image • Use the Graphics method drawImage() to transfer an image onto the Graphics’ surface boolean drawImage(Image imgObj, int left, int top, ImageObserver imgOb) Image img = createImage(w, h); // draw on the surfzce g.drawImage(img, x, y, null); g.drawImage(img, x, y, this);
Double-Buffered Graphics • Good for animation • Apply the operations to in-memory Image • Draw Image to the screen
Double Buffering Image buffer = creatImage(w, h); Graphics bg= buffer.getGraphics bg.drawLine(…) screen.drawImage(buffer, 0, 0, null);
Loading Images • All Java VMs can support GIF and JPEG graphic files • Done in applications by the toolkit Tookit def = Toolkit.getDefaultToolkit(); Image img = def.getImage(“MyFile.dat”); Image img = def.getImage(url)
Downloading Process • Images must be loaded to be drawn • With URLs this involves downloading acros the Internet • Get updates about a downloading process • Through an ImageObserver interface
ImageObserver • The Component class implements ImageObserver • The component implementation simple calls repaint() whenever more data is available