Graphics and Java 2 D Introduction Javas graphics

  • Slides: 22
Download presentation
Graphics and Java 2 D

Graphics and Java 2 D

Introduction • Java’s graphics capabilities – Drawing 2 D shapes – Controlling colors –

Introduction • Java’s graphics capabilities – Drawing 2 D shapes – Controlling colors – Controlling fonts • Java 2 D API – More sophisticated graphics capabilities • Drawing custom 2 D shapes • Filling shapes with colors and patterns 2

Introduction: Classes and Interfaces Object Color Component Font. Metrics Graphics Polygon Classes and interfaces

Introduction: Classes and Interfaces Object Color Component Font. Metrics Graphics Polygon Classes and interfaces from the Java 2 D API that appear in package java. awt Graphics 2 D interface java. awt. Paint Basic. Stroke interface java. awt. Shape Gradient. Paint interface java. awt. Stroke Texture. Paint Classes from the Java 2 D API that appear in package java. awt. geom General. Path Line 2 D Rectangular. Shape Arc 2 D Ellipse 2 D Rectangle 2 D Round. Rectangle 2 D 3

Introduction • Java’s coordinate system – Scheme for identifying all points on screen –

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 ycoordinate 4

Graphics Contexts and Graphics Objects • Graphics context – Enables drawing on screen –

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 5

Color Control • Class Color – Defines methods and constants for manipulating colors –

Color Control • Class Color – Defines methods and constants for manipulating colors – Colors are created from red, green and blue components • RGB values 6

Color constant and value 7

Color constant and value 7

Color methods & color-related Graphic methods 8

Color methods & color-related Graphic methods 8

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Show. Colors. java // Choosing colors with JColor. Chooser. import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Show. Colors extends JFrame { private JButton change. Color. Button; private Color color = Color. LIGHT_GRAY; private Container container; // set up GUI public Show. Colors() { super( "Using JColor. Chooser" ); container = get. Content. Pane(); container. set. Layout( new Flow. Layout() ); // set up change. Color. Button and register its event handler change. Color. Button = new JButton( "Change Color" ); change. Color. Button. add. Action. Listener( 9

24 new Action. Listener() { // anonymous inner class 25 26 // display JColor.

24 new Action. Listener() { // anonymous inner class 25 26 // display JColor. Chooser when user clicks button 27 public void action. Performed( Action. Event event ) 28 { 29 color = JColor. Chooser. show. Dialog( 30 Show. Colors. this, "Choose a color", color ); 31 32 JColor. Chooser allows // set default color, if no color is returned 33 user to choose from if ( color == null ) 34 color = Color. LIGHT_GRAY; among several colors 35 36 // change content pane's background color 37 container. set. Background( color ); 38 } static method 39 } // end anonymous inner class show. Dialog displays 40 ); // end call to add. Action. Listener the color chooser dialog 41 42 container. add( change. Color. Button ); 43 44 set. Size( 400, 130 ); 45 set. Visible( true ); 46 } // end Show. Color 2 constructor 10

47 48 49 50 51 52 53 // execute application public static void main(

47 48 49 50 51 52 53 // execute application public static void main( String args[] ) { Show. Colors application = new Show. Colors(); application. set. Default. Close. Operation( JFrame. EXIT_ON_CLOSE ); } } // end class Show. Colors 2 11

Font Control • Class Font – Contains methods and constants for font control –

Font Control • Class Font – Contains methods and constants for font control – Font constructor takes three arguments • Font name – Monospaced, Sans. Serif, etc. • Font style – Font. PLAIN, Font. ITALIC and Font. BOLD • Font size – Measured in points (1/72 of inch) 12

Font-related methods and constants 13

Font-related methods and constants 13

Font-related methods and constants // set font to Serif (Times), bold, 12 pt and

Font-related methods and constants // set font to Serif (Times), bold, 12 pt and draw a string g. set. Font( new Font( "Serif", Font. BOLD, 12 ) ); g. draw. String( "Serif 12 point bold. ", 20, 50 ); // set font to Monospaced (Courier), italic, 24 pt and draw a string g. set. Font( new Font( "Monospaced", Font. ITALIC, 24 ) ); g. draw. String( "Monospaced 24 point italic. ", 20, 70 ); // set font to Sans. Serif (Helvetica), plain, 14 pt and draw a string g. set. Font( new Font( "Sans. Serif", Font. PLAIN, 14 ) ); g. draw. String( "Sans. Serif 14 point plain. ", 20, 90 ); // set font to Serif (Times), bold/italic, 18 pt and draw a string g. set. Color( Color. RED ); g. set. Font( new Font( "Serif", Font. BOLD + Font. ITALIC, 18 ) ); g. draw. String( g. get. Font(). get. Name() + " " + g. get. Font(). get. Size() + " point bold italic. ", 20, 110 ); 14

Java 2 D API • Java 2 D API – Provides advanced 2 D

Java 2 D API • Java 2 D API – Provides advanced 2 D graphics capabilities • java. awt. image • java. awt. color • java. awt. font • java. awt. geom • java. awt. print • java. awt. image. renderable – Uses class java. awt. Graphics 2 D • Extends class java. awt. Graphics 15

Java 2 D API • when using Swing overide paint. Component instead of paint.

Java 2 D API • when using Swing overide paint. Component instead of paint. 16

Java 2 D API • Java 2 D shapes – Package java. awt. geom

Java 2 D API • Java 2 D shapes – Package java. awt. geom • Ellipse 2 D. Double • Rectangle 2 D. Double • Round. Rectangle 2 D. Double • Arc 3 D. Double • Lines 2 D. Double 17

Java 2 D API • The Java 2 D API introduces java. awt. Graphics

Java 2 D API • The Java 2 D API introduces java. awt. Graphics 2 D , a new type of Graphics object. • Graphics 2 D extends the Graphics class to provide access to the enhanced graphics and rendering features of the Java 2 D API. • To use Java 2 D API features, you cast the Graphics object passed into a component's rendering method to a Graphics 2 D object. public void Paint(Graphics g) { Graphics 2 D g 2 = (Graphics 2 D) g; . . . } 18

Java 2 D Drawing Process: Step 1 • Cast Graphics object to Graphics 2

Java 2 D Drawing Process: Step 1 • Cast Graphics object to Graphics 2 D public void paint. Component(Graphics g) { super. paing. Component(g); // Typical Swing Graphics 2 d g 2 d = (Graphics 2 d) g; g 2 d. do. Some. Stuff(. . . ); . . . } • Note – All methods that return Graphics in Java 1. 1 return Graphics 2 D in Java 2 • paint, paint. Component • get. Graphics 19

Java 2 D Drawing Process: Step 2 • Set pen parameters – – –

Java 2 D Drawing Process: Step 2 • Set pen parameters – – – – – g 2 d. set. Paint(fill. Color. Or. Pattern); g 2 d. set. Stroke(pen. Thickness. Or. Pattern); g 2 d. set. Composite(some. Alpha. Composite); g 2 d. set. Font(some. Font); g 2 d. translate(. . . ); g 2 d. rotate(. . . ); g 2 d. scale(. . . ); g 2 d. shear(. . . ); g 2 d. set. Transform(some. Affine. Transform); 20

Java 2 D Drawing Process: Step 3 • Create a Shape object. Rectangle 2

Java 2 D Drawing Process: Step 3 • Create a Shape object. Rectangle 2 D. Double rect =. . . ; Ellepse 2 D. Double ellipse =. . . ; Polygon poly =. . . ; General. Path path =. . . ; // satisfies Shape interface Some. Shape. You. Defined shape =. . . ; • Note – Most shapes are in the java. awt. geom package – There is a corresponding Shape class for most of the draw. Xxx methods of Graphics 21

Java 2 D Drawing Process: Step 4 • Draw an outlined or filled version

Java 2 D Drawing Process: Step 4 • Draw an outlined or filled version of the Shape g 2 d. draw(some. Shape); g 2 d. fill(some. Shape); • The legacy methods are still supported – draw. String – draw. Line, draw. Rect, fill. Rect 22