1 / 26

Java Graphics

Java Graphics. JButton. Graphics. Window is like a painter’s canvas Application must paint its window contents Java components paint themselves Anything else: Programmer When to paint? How to paint?. How to Paint?. (0,0). (width,0). (0,height). (width, height). Coordinate System.

bart
Download Presentation

Java Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Graphics

  2. JButton Graphics • Window is like a painter’s canvas • Application must paint its window contents • Java components paint themselves • Anything else: Programmer • When to paint? • How to paint?

  3. How to Paint?

  4. (0,0) (width,0) (0,height) (width, height) Coordinate System • Upside-down Cartesian

  5. Component Hierarchy • Each component has its own subwindow • Subwindow = rectangular area within parent component • Has own coordinate system • Clipping: • Can’t paint outside its subwindow • Can’t paint over child components? (0,0) JPanel JButton (0,0) JButton (wb, hb) (wp, hp)

  6. Painting Components • Can paint any component • JPanel is good for custom graphics area JPanel JButton

  7. Painting in Java import java.awt.Graphics import java.awt.Graphics2D // Java2 1. Get the “graphics context” of component public void paintComponent (Graphics g) { Graphics2D g2 = (Graphics2D) g; … 2. Paint in it g2.drawLine(x1,y1, x2,y2);

  8. Graphics Primitives DrawFill • Point (x,y) • Line (pt1,pt2) • PolyLine (pt list) • Arc • Oval (pt, w,h) • Rectangle (pt, w,h) • RoundRectangle • Polygon (pt list) • Image (file, x,y) • Text (string, x,y) label

  9. Graphics Attributes • Color • Font • Stroke attributes: • Line width, dash, end caps, joins, miter • Paint attributes: • Color, gradient, texture • Composite: • Blending • Transforms: • Translate, rotate, flip, shear, scale

  10. Graphics Class: Methods include • drawArc() – draws a hollow arc • drawLine() – draws a straight line • drawOval() - draws a hollow oval • If width and height are same, it draws a circle • drawPolygon() - draws a hollow polygon • drawRect() - draws a hollow rectangle • drawRoundRect() - draws a hollow round cornered rectangle • drawString() – display a text string • fillArc() - draw a filled arc • fillOval() • fillPolygon() • fillRect() • fillRoundRect() • getColor – retrieve the current drawing colour • getFont • setColor – sets the drawing color • setFont More at http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html

  11. Color • Combinations of Red, Green, Blue- each [0, 255] • Java.awt.Color offers predefined constants of 13 colors BLACK, YELLOW, WHITE, PINK, RED, … g2.setPaint(Color.RED); • Specify a custom color g2.setPaint (new Color(255, 150, 0)); Hokie Orange

  12. in Java • Drawing primitives: • g2.drawLine( ), g2.drawRect( ), … g2.fillRect( ), … • Object oriented: • Create shape: • import java.awt.geom.* • Shape interface • ClassImplements shape = new ClassImplements ( args ); • Line2D, Rect2D, CubicCurve2D, … Rectangle2D rect = new Rectangle2D.Double (leftX, topY, width, height)

  13. in Java • Draw the shape: • g2.draw(shape); • g2.fill(shape); • Java uses floating-point coordinates • Either float or double • Rectangle2D is actually abstract class • Two concrete subclasses (static inner classes) • Rectangle2D.Float • Rectangle2D.Double

  14. in Java • Color and font: • g2.setColor( new Color(r,g,b) ); • g2.setFont( new Font(…) ); • Advanced: • g2.setStroke(…); • g2.setPaint(…); • g2.setComposite(…); • g2.setTransform(…); • Set graphics attributes • Draw graphics

  15. When to Paint?

  16. Re-Paint • Screen is like a painter’s canvas • All windows paint on the same surface! • Windows don’t “remember” whats under them • Need to re-paint when portions are newly exposed • Receive Repaint events • Open, resize, bring to front • When other windows in front move, resize, close

  17. MyApp

  18. Open Explorer, Notepad

  19. Close Explorer Repaint event sent to: MyApp, Desktop

  20. Desktop gets repaint event

  21. MyApp gets repaint event MyApp JPanel gets repaint event

  22. MyApp gets repaint event MyApp JPanel forwards repaint event to JButton

  23. Repainting Static Graphics • Repaint event: • Erase subwindow (fill with background color) • Draw subwindow contents

  24. In Java • Repaint event: • Java Swing components catch repaint event, and call their paintComponent( ) method • Default paintComponent( ) method paints component • E.g. panel background color • Sub-class the component (typically JPanel) • Over-ride paintComponent( ) method • Custom graphics here • Can call repaint( ) to invoke paintComponent( )

  25. Code public class MyPanel extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); // erases background Graphics2D g2 = (Graphics2D)g; //cast for java2 // my graphics: g2.setColor(new Color(255,0,0)); g2.fillRect(10,10,200,50); g2.setColor(new Color(0,0,0)); g2.drawString("Hello World", 10, 10); } } Hello World

  26. DEMO DrawTest.java

More Related