Java - profthinh ď jhtp5_12 tài liệu, giáo án, bài giảng , luận văn, luận án, đồ án, bài tập lớn về tất cả các lĩnh vực...
Chapter 12 - Graphics and Java 2D Outline 12.1 Introduction 12.2 Graphics Contexts and Graphics Objects 12.3 Color Control 12.4 Font Control 12.5 Drawing Lines, Rectangles and Ovals 12.6 Drawing Arcs 12.7 Drawing Polygons and Polylines 12.8 Java2D API 12.9 (Optional Case Study) Thinking About Objects: Designing Interfaces with the UML 2003 Prentice Hall, Inc All rights reserved 12.1 Introduction • Java’s graphics capabilities – Drawing 2D shapes – Controlling colors – Controlling fonts • Java 2D API – More sophisticated graphics capabilities • Drawing custom 2D shapes • Filling shapes with colors and patterns 2003 Prentice Hall, Inc All rights reserved Fig 12.1 Classes and interfaces used in this chapter from Java’s original graphics capabilities and from the Java2D API [Note: Class Object appears here because it is the superclass of the Java class hierarchy.] Object Color Component Font FontMetrics Graphics Polygon Classes and interfaces from the Java2D API that appear in package java.awt Graphics2D interface java.awt.Paint BasicStroke GradientPaint interface java.awt.Shape TexturePaint interface java.awt.Stroke Classes from the Java2D API that appear in package java.awt.geom GeneralPath Line2D RectangularShape Arc2D Ellipse2D Rectangle2D RoundRectangle2D 2003 Prentice Hall, Inc All rights reserved 12.1 Introduction • Java’s coordinate system – Scheme for identifying all points on screen – Upper-left corner has coordinates (0,0) – Coordinate point composed of x-coordinate and y-coordinate 2003 Prentice Hall, Inc All rights reserved Fig 12.2 Java coordinate system Units are measured in pixels +x (0, 0) (x , y ) +y Y a xis 2003 Prentice Hall, Inc All rights reserved X a xis 12.2 Graphics Contexts and Graphics Objects • Graphics context – Enables drawing on screen – Graphics object manages graphics context • Controls how information is drawn – Class Graphics is abstract • Cannot be instantiated • Contributes to Java’s portability – Class Component method paint takes Graphics object public void paint( Graphics g ) – Called through method repaint 2003 Prentice Hall, Inc All rights reserved 12.3 Color Control • Class Color – Defines methods and constants for manipulating colors – Colors are created from red, green and blue components • RGB values 2003 Prentice Hall, Inc All rights reserved Fig 12.3 Color constants and their RGB values Color constant public final static Color ORANGE Color RGB value public final static Color PINK orange pink 255, 200, 255, 175, 175 public final static Color CYAN cyan 0, 255, 255 public final static Color MAGENTA magenta 255, 0, 255 public final static Color YELLOW yellow 255, 255, public final static Color BLACK black 0, 0, public final static Color WHITE white 255, 255, 255 public final static Color GRAY gray 128, 128, 128 public final static Color LIGHT_GRAY light gray 192, 192, 192 public final static Color DARK_GRAY dark gray 64, 64, 64 public final static Color RED red 255, 0, public final static Color GREEN green 0, 255, public final static Color BLUE blue 0, 0, 255 2003 Prentice Hall, Inc All rights reserved Fig 12.4 Color methods and color-related Graphics methods Method Description Color constructors and methods public Color( int r, int g, int b ) Creates a color based on red, green and blue components expressed as integers from to 255 public Color( float r, float g, float b ) Creates a color based on red, green and blue components expressed as floating-point values from 0.0 to 1.0 public int getRed() Returns a value between and 255 representing the red content public int getGreen() Returns a value between and 255 representing the green content public int getBlue() Returns a value between and 255 representing the blue content Graphics methods for manipulating Colors public Color getColor() Returns a Color object representing the current color for the graphics context public void setColor( Color c ) Sets the current color for drawing with the graphics context 2003 Prentice Hall, Inc All rights reserved 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Outline // Fig 12.5: ShowColors.java // Demonstrating Colors import java.awt.*; import javax.swing.*; ShowColors.java public class ShowColors extends JFrame { // constructor sets window's title bar string and dimensions public ShowColors() { super( "Using colors" ); setSize( 400, 130 ); setVisible( true ); Line 18 Line 24 Paint window when application begins execution Line 25 Line 26 } // draw rectangles and Strings in different colors public void paint( Graphics g ) { // call superclass's paint method super.paint( g ); // set new drawing color using integers g.setColor( new Color( 255, 0, ) ); g.fillRect( 25, 25, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(), 130, 40 ); Method setColor sets color’s RGB value Method fillRect creates filled rectangle at specified coordinates using current RGB value Method drawString draws colored text at specified coordinates 2003 Prentice Hall, Inc All rights reserved 47 12.8 Java2D API • Java 2D API – Provides advanced 2D graphics capabilities • java.awt • java.awt.image • java.awt.color • java.awt.font • java.awt.geom • java.awt.print • java.awt.image.renderable – Uses class java.awt.Graphics2D • Extends class java.awt.Graphics 2003 Prentice Hall, Inc All rights reserved 48 12.8 Java2D API • Java 2D shapes – Package java.awt.geom • Ellipse2D.Double • Rectangle2D.Double • RoundRectangle2D.Double • Arc3D.Double • Lines2D.Double 2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig 12.22: Shapes.java // Demonstrating some Java2D shapes import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; Outline Shapes.java public class Shapes extends JFrame { // set window's title bar String and dimensions public Shapes() { super( "Drawing 2D shapes" ); setSize( 425, 160 ); setVisible( true ); } // draw shapes with Java2D API public void paint( Graphics g ) { super.paint( g ); // call superclass's paint method Graphics2D g2d = ( Graphics2D ) g; // cast g to Graphics2D 2003 Prentice Hall, Inc All rights reserved 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // draw 2D ellipse filled with a blue-yellow gradient g2d.setPaint( new GradientPaint( 5, 30, Color.BLUE, 35, 100, Color.YELLOW, true ) ); g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) ); // draw 2D rectangle in red g2d.setPaint( Color.RED ); g2d.setStroke( new BasicStroke( 10.0f ) ); g2d.draw( new Rectangle2D.Double( 80, 30, 65, 100 ) ); // draw 2D rounded rectangle with a buffered background BufferedImage buffImage = new BufferedImage( 10, 10, BufferedImage.TYPE_INT_RGB ); Graphics2D gg = buffImage.createGraphics(); gg.setColor( Color.YELLOW ); // draw in yellow gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle gg.setColor( Color.BLACK ); // draw in black gg.drawRect( 1, 1, 6, ); // draw a rectangle gg.setColor( Color.BLUE ); // draw in blue gg.fillRect( 1, 1, 3, ); // draw a filled rectangle gg.setColor( Color.RED ); // draw in red gg.fillRect( 4, 4, 3, ); // draw a filled rectangle Outline Use GradientPaint to fill shape with gradient Shapes.java Fill ellipse with gradient Lines 27-28 Use BasicStroke to draw 2D red-border Line rectangle 29 Lines 33-34 BufferedImage produces image to be manipulated Lines 37-28 Lines 40-48 Draw texture into BufferedImage 2003 Prentice Hall, Inc All rights reserved 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 // paint buffImage onto the JFrame g2d.setPaint( new TexturePaint( buffImage, new Rectangle( 10, 10 ) ) ); g2d.fill( new RoundRectangle2D.Double( 155, 30, 75, 100, 50, 50 ) ); // draw 2D pie-shaped arc in white g2d.setPaint( Color.WHITE ); g2d.setStroke( new BasicStroke( 6.0f ) ); g2d.draw( new Arc2D.Double( 240, 30, 75, 100, 0, 270, Arc2D.PIE ) ); // draw 2D lines in green and yellow g2d.setPaint( Color.GREEN ); g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) ); Use BufferedImage asOutline texture for painting rounded rectangle Shapes.java Use Arc2D.PIE to drawLines white-border 51-52 2D pie-shaped arc Line 58 Line 62 Draw solid green line Line 69 float dashes[] = { 10 }; g2d.setPaint( Color.YELLOW ); g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, dashes, ) ); g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) ); Draw dashed yellow line that crosses solid green line } // end method paint 2003 Prentice Hall, Inc All rights reserved 73 74 75 76 77 78 79 80 // execute application public static void main( String args[] ) { Shapes application = new Shapes(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } Outline Shapes.java } // end class Shapes 2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig 12.23: Shapes2.java // Demonstrating a general path import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class Shapes2 extends JFrame { Outline Shapes2.java Lines 24-25 // set window's title bar String, background color and dimensions public Shapes2() { super( "Drawing 2D Shapes" ); getContentPane().setBackground( Color.WHITE ); setSize( 400, 400 ); setVisible( true ); } // draw general paths public void paint( Graphics g ) { super.paint( g ); // call superclass's paint method x-y coordinates that comprise star int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 }; int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 }; 2003 Prentice Hall, Inc All rights reserved 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 Graphics2D g2d = ( Graphics2D ) g; GeneralPath star = new GeneralPath(); // create GeneralPath object // set the initial coordinate of the General Path star.moveTo( xPoints[ ], yPoints[ ] ); // create the star this does not draw the star for ( int count = 1; count < xPoints.length; count++ ) star.lineTo( xPoints[ count ], yPoints[ count ] ); star.closePath(); // close the shape GeneralPath is a shape Outline constructed from straight lines and complex curves Shapes2.java Line 28 Create star Lines 31-37 Lines 42-50 g2d.translate( 200, 200 ); // translate the origin to (200, 200) // rotate around origin and draw stars in random colors for ( int count = 1; count