940 likes | 1.16k Views
Java and Imaging. Paint. paint() method AWT Coordinate System Color & ColorModel the java.awt.Color class the java.awt.image.ColorModel class. paint() method. Java Peer to draw, inherit Canvas, override paint redraw screen WINDOW_
E N D
Paint • paint() method • AWT Coordinate System • Color & ColorModel • the java.awt.Color class • the java.awt.image.ColorModel class
paint() method Java Peer to draw, inherit Canvas, override paint redraw screen WINDOW_ paint() EXPOSED update() Event repaint() clear component window
The java.awt.Color class • Encapsulate RGB colors • have static final Color object of frequent-used colors public static final Color black; public Color(int red, int green, int blue); public Color brighter(); public Color darker(); public int getRed();
java.awt.image.ColorModel • encapsulate the METHODS of TRANSLATION from PIXEL VALUES • get alpha, red, green, blue value from pixel • implemented by DirectColorModel, IndexColorModel public abstract class ColorModel; public colorModel(int nbits); public abstract getAlpha(int pixelvalue); public static ColorModel getRGBdefault(); // return default ColorModel 0xAARRGGBB
Drawing • The Graphics class • Simple Drawing • Working With Text • Font • FontMetrics
java.awt.Graphics class • contain current graphics context and methods for drawing • graphics context : fgcolor, bgcolor, font, etc. • abstract class - actual java implementation and native library is provided by virtual machine
Simple Drawing public void mouseClicked(MouseEvent e) { pt = e.getPoint(); repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void paint(Graphics g) { g.setColor(Color.blue); g.drawRect(pt.x, pt.y, 10, 10); } } import java.awt.*; import java.awt.event.*; public class DrawingCanvas extends Canvas implements MouseListener { protected Point pt; public DrawingCanvas() { pt = new Point(50, 50); addMouseListener(this); } public Dimension getPreferredSize() { return new Dimension(100, 100); }
java.awt.Font • load Font object from font name public Font(String name, int style, int size); public static Font getFont(String nm);
java.awt.FontMetrics • has methods to get information from Font object public FontMetrics(Font fn); public getAscent(); public getDescent();
Using images • The Image class • Loading images • Loading • Keep Track of Image Loading • With the MediaTracker class • With the ImageObserver interface* • Displaying images
Using images - • Manipulating images* • imagefilter
java.awt.Image class • encapsulate image • programmer can’t know about internal data structure representaing image public abstract Graphics getGraphics(); public abstract int getWidth(); public abstract int getHeight(); public Image createImage(ImageProducer); public Image createImage(int w, int h);
Loading • automatic loading about JPEG and GIF images • background loading • use methods in java.applet.Applet or java.awt.Toolkit • load via filename or url public abstract Image Toolkit.getImage(URL url); public abstract Image Toolkit.getImage(String file) Toolkit Component.getToolkit() or static Toolkit Toolkit.getDefaultToolkit()
java.awt.MediaTracker • keep track of image loading public MediaTracker(Component comp); public addImage(Image img, int id); public boolean checkAll(); public boolean checkID(int id); public boolean isErrorAny(); public void waitForAll();
java.awt.MediaTracker - MediaTracker tracker; tracker = new MediaTracker(this); Toolkit tk = Toolkit.getDefaultToolkit(); for (int i = 1; i <= 10; i++) { images[i-1] = tk.getImage("image" + i + ".gif"); tracker.addImage(images[i-1], i); } try { //Start downloading the images. Wait until they're loaded. tracker.waitForAll(); } catch (InterruptedException e) {} // in paint(); if (!tracker.checkAll()) { g.clearRect(0, 0, d.width, d.height); g.drawString("Please wait...", 0, d.height/2); } //If all images are loaded, draw. else { ...//same code as before...
java.awt.image.ImageObserver interface loading imageUpdate() ImageObserver • image width known height known loading
java.awt.image.ImageObserver public interface ImageObserver { public abstract boolean imageUpdate(image img, int infoflags, int x, int y, int width, int height); } If image knows width and height, or loading pixels, call this methods with infoflags. if return true, no more call this methods Component implements ImageObserver if not completely loaded yet, repaint()
java.awt.image.ImageObserver • public int Image.getWidth(ImageObserver observer) • If the width is not known yet then the ImageObserver will be notified later and -1 will be returned. • public boolean drawImage ( Image img, int x, int y, int width, int height, Color bgColor, ImageObserver observer ) • return immediately. return ture if image is fully initialized.if not, return false, and let img notify observer
Displaying images • public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer); • public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer); • public abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer); • public abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer);
ImageFilter - startProduction() setPixels() ImageProducer ImageConsumer
ImageFilter - ImageFilter ImageProducer ImageConsumer ImageFilter filter = new RotateFilter(angle); ImageProducer producer = new FilteredImageSource(souceImage.getSource(), filter); Image resultImage = createImage(producer);
Improving Animation • Eliminating Flashing • Overrideing update() method • Clipping • Double Buffering • Speed up Image Loading with Clipping
Overriding update() method • default update() • clear all component's Background and call paint() • Component’s member method public void update(Graphics g) { DoActualDrawing(); } public void paint(Graphics g) { update(g); }
Clipping • public abstract void clipRect(int x, int y, int width, int height); • Clipping the Drawing Area public void update(Graphics g) { Graphics g2 = g.create(x, y, w, h); paint(g2); }
Double Buffering • update drawing area one time Dimension offDimension; Image offImage; Graphics offGraphics; public void update(Graphics g) { if( (offGraphics == null) || ( d.width != offDimension.width) || (d.height != offDimension.height) ) { offDimension = d; offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); } DoActualDrawing(offGraphics); drawImage(offGraphics, 0, 0, this); }
Speed up Image Loading with Clipping • when loading images using URLs, most of the time is taken up by initiating HTTP connections. • make an image strip int stripWidth = imageStrip.getWidth(this); int stripHeight = imageStrip.getHeight(this); int imageWidth = stripWidth / numImages; g.clipRect(0, 0, imageWidth, stripHeight); g.drawImage(imageStrip, -imageNumber*imageWidth, 0, this);
New Features of Java1.1 • Enhancement on Graphics Clip • Cropping, Scaling, and Flipping • MemoryImageSource • PixelGrabber
Enhancement on Clipping • New methods of Graphics Shape getClip(); void setClip(Shape clip); void setClip(int x, int y, int w, int h); void getClipBounds();
Enhancement on Clipping Graphics g2 = g.create(); g2.clipRect(10, 10, 100, 100); g2.drawImage(img2, 10, 10, this); g2.dispose(); Shape oldclip = g.getClip(); g.clipRect(10, 10, 100, 100); g.drawImage(img2, 10, 10, this); g.setClip(oldclip);
Java Media APIs • Java2D • Java Advanced Imaging (JAI) • Java Media Framework (JMF) • Java3D
java.awt.image • Image is an abstract class • Image objects are constructed in a platform specific manner • Two types of image objects: • Images created from an image source • Images created by an AWT component • Component.createImage()
Processing Images in AWT ImageProducer PixelGrabber MemoryImageSource MemoryImageSource
JAI models • The producer/consumer model • 基本的 AWT 影像 model • The immediate mode model • 進階的 AWT 影像 model • The pipeline model • The JAI 所使用的影像 model Push model Pull model
BufferedImage java.awt.image.BufferedImage Color Model Raster ColorSpace SampleModel DataBuffer Encapsulates the methods for translating a pixel value to color components (ex. red/green/blue) and an alpha component for rendering
DataBuffer • java.awt.image.DataBuffer • DataBuffer stores pixel data as one or more arrays of primitive data types. • Java2D buffer types: • Byte • Integer • Short • Unsigned short • JAI buffer types: • Double • Float
Sample Model • SampleMode describes how pixels are stored in the DataBuffer. • Packed Models • Single-pixel packed model • Multiple-pixel packed sample model • Component Models • Component sample model
ColorModel • Converts raster pixel samples into color components for displaying • Pixel samples are packed into a short integer (16 bits). Each sample would be 5 bits. • If displayed on a color monitor (0-255) per channel, the resulting image would appear dark. • Color model handles the conversion.
ColorSpaces • Device independent/dependent • Converts between devices • Supports many color spaces including: • CMYK • HSV (HSB) • CIEXYZ • sRGB • ICC Color Profile • Create ColorSpace via factory
Code Example Creating an Image
Alpha Channel • A mixing factor to control the linear interpolation of foreground and background colors. • Varies from 0 to 1 • 0 indicates no coverage (clear) • 1 indicates full coverage • Each sample is multiplied by the alpha channel
Transformations • Definitions: • Transformation is a function that maps an object from one point to another. • Affine transform preserves: • Finiteness • Parallelism • Transformations: • Translation • Scaling • Rotation • Reflection • Shear
Translation y x
Scaling y x
Rotation y x
Reflection y x
Shear y x
Transformations • Transformations can be concatenated: • C = Translate * Scale * Rotate * Shear • Java Classes: • AffineTransform ( java.awt.geom ) • AffineTransformOp ( java.awt.image )