Graphics and Java 2D - PowerPoint PPT Presentation

niveditha
graphics and java 2d l.
Skip this Video
Loading SlideShow in 5 Seconds..
Graphics and Java 2D PowerPoint Presentation
Download Presentation
Graphics and Java 2D

play fullscreen
1 / 22
Download Presentation
Graphics and Java 2D
321 Views
Download Presentation

Graphics and Java 2D

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Graphics and Java 2D

  2. 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

  3. Object Color Component Arc2D Font FontMetrics Graphics BasicStroke Polygon Classes and interfaces from the Java2D API that appear in package java.awt Classes from the Java2D API that appear in package java.awt.geom interface java.awt.Paint interface java.awt.Shape GeneralPath interface java.awt.Stroke Line2D RectangularShape Graphics2D Ellipse2D GradientPaint Rectangle2D TexturePaint RoundRectangle2D Introduction:Classes and Interfaces

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

  5. 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 publicvoidpaint(Graphicsg) • Called through method repaint

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

  7. Color constant and value

  8. Color methods & color-related Graphic methods

  9. 1 // ShowColors.java 2 // Choosing colors with JColorChooser. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 publicclass ShowColors extends JFrame { 8 private JButton changeColorButton; 9 private Color color = Color.LIGHT_GRAY; 10 private Container container; 11 12 // set up GUI 13 public ShowColors() 14 { 15 super( "Using JColorChooser" ); 16 17 container = getContentPane(); 18 container.setLayout( new FlowLayout() ); 19 20 // set up changeColorButton and register its event handler 21 changeColorButton = new JButton( "Change Color" ); 22 changeColorButton.addActionListener( 23

  10. JColorChooser allows user to choose from among several colors static method showDialog displays the color chooser dialog 24 new ActionListener() { // anonymous inner class 25 26 // display JColorChooser when user clicks button 27 publicvoid actionPerformed( ActionEvent event ) 28 { 29 color = JColorChooser.showDialog( 30 ShowColors.this, "Choose a color", color ); 31 32 // set default color, if no color is returned 33 if ( color == null ) 34 color = Color.LIGHT_GRAY; 35 36 // change content pane's background color 37 container.setBackground( color ); 38 } 39 } // end anonymous inner class 40 ); // end call to addActionListener 41 42 container.add( changeColorButton ); 43 44 setSize( 400, 130 ); 45 setVisible( true ); 46 } // end ShowColor2 constructor

  11. 47 // execute application 48 publicstaticvoid main( String args[] ) 49 { 50 ShowColors application = new ShowColors(); 51 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 52 } 53 } // end class ShowColors2

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

  13. Font-related methods and constants

  14. Font-related methods and constants // set font to Serif (Times), bold, 12pt and draw a string g.setFont( new Font( "Serif", Font.BOLD, 12 ) ); g.drawString( "Serif 12 point bold.", 20, 50 ); // set font to Monospaced (Courier), italic, 24pt and draw a string g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) ); g.drawString( "Monospaced 24 point italic.", 20, 70 ); // set font to SansSerif (Helvetica), plain, 14pt and draw a string g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); g.drawString( "SansSerif 14 point plain.", 20, 90 ); // set font to Serif (Times), bold/italic, 18pt and draw a string g.setColor( Color.RED ); g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) ); g.drawString( g.getFont().getName() + " " + g.getFont().getSize() + " point bold italic.", 20, 110 );

  15. Java 2D 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

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

  17. Java 2D API • Java 2D shapes • Package java.awt.geom • Ellipse2D.Double • Rectangle2D.Double • RoundRectangle2D.Double • Arc3D.Double • Lines2D.Double

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

  19. Java 2D Drawing Process: Step 1 • Cast Graphics object to Graphics 2D public void paintComponent(Graphics g) { super.paingComponent(g); // Typical Swing Graphics2d g2d = (Graphics2d) g; g2d.doSomeStuff(...); ... } • Note • All methods that return Graphics in Java 1.1 return Graphics2D in Java 2 • paint, paintComponent • getGraphics

  20. Java 2D Drawing Process: Step 2 • Set pen parameters • g2d.setPaint(fillColorOrPattern); • g2d.setStroke(penThicknessOrPattern); • g2d.setComposite(someAlphaComposite); • g2d.setFont(someFont); • g2d.translate(...); • g2d.rotate(...); • g2d.scale(...); • g2d.shear(...); • g2d.setTransform(someAffineTransform);

  21. Java 2D Drawing Process: Step 3 • Create a Shape object. Rectangle2D.Double rect = ...; Ellepse2D.Double ellipse = ...; Polygon poly = ...; GeneralPath path = ...; // satisfies Shape interface SomeShapeYouDefined shape = ...; • Note • Most shapes are in the java.awt.geom package • There is a corresponding Shape class for most of the drawXxx methods of Graphics

  22. Java 2D Drawing Process: Step 4 • Draw an outlined or filled version of the Shape g2d.draw(someShape); g2d.fill(someShape); • The legacy methods are still supported • drawString • drawLine, drawRect, fillRect