1Spring 2003 © University of Stirling IT82: Multimedia 1 Spring 2003 © University of Stirling IT82: Multimedia 2 Overview • Graphics in Java: – Colour – Images – Java2D – Fonts • The bigger picture of Java Graphics: – Java Advanced Imaging (JAI) API – Java3D • The bigger picture of Java multimedia Spring 2003 © University of Stirling IT82: Multimedia 3 Java2D • Java 2D extends the basic AWT graphics facilities • New in Java 2 (with JDK version 1.2) • A brief overview: – Draw lines of any thickness – Fill shapes with gradients and textures – Move, rotate, scale, and shear text and graphics – Composite overlapping text and graphics – Filters, enabling simple image operations, e.g. • blurring • sharpening 2Spring 2003 © University of Stirling IT82: Multimedia 4 Spring 2003 © University of Stirling IT82: Multimedia 5 Colour Models • In Java2D, the java.awt.color package provides colours • All the common colour spaces are provided, including – standard RGB – greyscale colour models – CMY, CMYK, – HSV – CIE XYZ – CIE Lab, – and more! • Typical usage: – (alpha,red,green,blue) is stored as an int in the RGB colour model Spring 2003 © University of Stirling IT82: Multimedia 6 Image Types • Image – in java.awt – abstract superclass of all classes for representing images • ImageIcon – class provided by Swing, for using as icons – implements Icon • BufferedImage – direct subclass of Image – in java.awt.image – pixel data available for manipulation 3Spring 2003 © University of Stirling IT82: Multimedia 7 Buffered Images bufferedIm.getRGB(15,10) 15 10 Spring 2003 © University of Stirling IT82: Multimedia 8 What is a graphics context? public void paint(Graphics g) { g.setColor(Color.red); g.drawRect(100,100,60,40); } A graphics context (an instance of the Graphics class) holds contextual information for doing drawing operations: Spring 2003 © University of Stirling IT82: Multimedia 9 Graphics2D Paint on a component (use paintComponent in Swing): public void paint(Graphics g) { g.setColor(Color.red); g.drawRect(100,100,60,40); } …or create a graphics context for drawing on an image: Graphics2D g2 = bufferedIm.createGraphics(); g2.setColor(Color.red); g2.drawRect(100,100,60,40); A Graphics2D object holds state information 4Spring 2003 © University of Stirling IT82: Multimedia 10 Graphics2D State • Stroke setStroke • Paint setPaint • Composite setComposite • Transform setTransform • Clip setClip • Font setFont • RenderingHints setRenderingHints Spring 2003 © University of Stirling IT82: Multimedia 11 Shapes and Painting g2.drawRect(100,100,60,40); Spring 2003 © University of Stirling IT82: Multimedia 12 Stroking public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.magenta); BasicStroke stroke = new BasicStroke(2.0f); g2d.setStroke(stroke); g2d.draw(new Rectangle2D.Double(40,40,60,40)); BasicStroke wideStroke = new BasicStroke(8.0f); g2d.setStroke(wideStroke); g2d.draw(new RoundRectangle2D.Double(40,120,60,40, 10, 10)); } 5Spring 2003 © University of Stirling IT82: Multimedia 13 Transformations (affine) • Rotation • Translating (moving) • Reflection • Shearing (stretching) • Scaling Spring 2003 © University of Stirling IT82: Multimedia 14 Example: Rotation Spring 2003 © University of Stirling IT82: Multimedia 15 public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; AffineTransform at = new AffineTransform(); g2d.setTransform(at); g2d.setColor(Color.red); g2d.fillOval(200,200,50,30); at.rotate(Math.PI/3.0,180.0,180.0); g2d.setTransform(at); g2d.setColor(Color.green); g2d.fillOval(200,200,50,30); at.rotate(Math.PI/3.0,180.0,180.0); g2d.setTransform(at); g2d.setColor(Color.blue); g2d.fillOval(200,200,50,30); at.rotate(Math.PI/3.0,180.0,180.0); g2d.setTransform(at); g2d.setColor(Color.magenta); g2d.fillOval(200,200,50,30); at.rotate(Math.PI/3.0,180.0,180.0); g2d.setTransform(at); g2d.setColor(Color.orange); g2d.fillOval(200,200,50,30); at.rotate(Math.PI/3.0,180.0,180.0); g2d.setTransform(at); g2d.setColor(Color.cyan); g2d.fillOval(200,200,50,30); } } import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Trans extends Frame { public Trans() { this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } }); } public static void main(String args[]) { System.out.println("Starting App"); Trans f = new Trans(); f.setSize(400,300); f.show(); } 6Spring 2003 © University of Stirling IT82: Multimedia 16 Compositing Rules Various! 8 different rules altogether Spring 2003 © University of Stirling IT82: Multimedia 17 Compositing Rule: XOR Mode Spring 2003 © University of Stirling IT82: Multimedia 18 import java.awt.*; import java.awt.event.*; public class XORdemo extends Frame { public XORdemo() { this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } }); } public static void main(String args[]) { System.out.println("Starting App"); XORdemo f = new XORdemo(); f.setSize(200,120); f.show(); } public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.black); g2d.fillRect(0,0,200,120); g2d.setColor(Color.red); g2d.fillOval(20,40,40,40); g2d.setColor(Color.green); g2d.fillOval(80,40,40,40); g2d.setColor(Color.blue); g2d.fillOval(140,40,40,40); g2d.setXORMode(Color.black); g2d.setColor(Color.white); g2d.fillRect(10,50,180,20); } } 7Spring 2003 © University of Stirling IT82: Multimedia 19 Rendering Hints • Rendering hints are not guaranteed to be obeyed • Various available, including – dithering – anti-aliasing Spring 2003 © University of Stirling IT82: Multimedia 20 The Java2D Graphics Pipeline Graphic Primitive Transformation Clipping Rasterizer Painting Compositing Rule Spring 2003 © University of Stirling IT82: Multimedia 21 Java Advanced Imaging (JAI) • Extends features provided in the Java2D API, in the area of image input, manipulation and output • Greater range of file formats (for input/output) – GIF (Compuserve’s Graphics Interchange Format) – JPEG (a way of compressing images from the Joint Photographics Experts Group) – PNG (Portable Network Graphics) – TIF (Tag Image File Format) – BMP (Microsoft’s bitmap format) – PNM (Portably aNy Map format - includes PBM,PGM,PPM) – FPX (FlashPix) 8Spring 2003 © University of Stirling IT82: Multimedia 22 JAI • Greater range of colour models • Conversion methods between different colour spaces are also provided • ColorSpaces provided include – RGB, non-colour-corrected RGB, – Grayscale, CMY/CMYK – CIE XYZ, Lab – Lab, HSV, – a luminance (g)/chrominance (colour) standard for Kodak PhotoCD images Spring 2003 © University of Stirling IT82: Multimedia 23 JAI • More ways to model images than just BufferedImage , e.g. – Tiled Images – Image Sequences – Multi-resolution Images • Methods for extracting data from images, e.g. – finding the frequency of occurrence of colours in images – finding the “edges” (or outlines) in an image – other statistical operations on the pixel data Spring 2003 © University of Stirling IT82: Multimedia 24 JAI • Many, many methods for image manipulation, e.g. • Combining (compositing) two images in various ways • Image enhancement, e.g. – Sharpening – Blurring • Transformations - not just geometric, but also... 9Spring 2003 © University of Stirling IT82: Multimedia 25 – Perspective – Warping Spring 2003 © University of Stirling IT82: Multimedia 26 Java3D Spring 2003 © University of Stirling IT82: Multimedia 27 Java Media APIs • Java2D • Java Advanced Imaging (JAI) • Java3D • Java Media Framework – audio, video, time-based media • Java Sound – audio, midi • Java Speech – speech technology including digital recognition, speech synthesisers – only 3rd party implementations 10 Spring 2003 © University of Stirling IT82: Multimedia 28 End of Lecture