1 / 11

CS202 Java Object Oriented Programming GUI Programming – Introduction to Java2D

CS202 Java Object Oriented Programming GUI Programming – Introduction to Java2D. Chengyu Sun California State University, Los Angeles. The paint() Method. How does Java draw the GUI components? public void paint(Graphics g) A method of java.awt.Component

rickyr
Télécharger la présentation

CS202 Java Object Oriented Programming GUI Programming – Introduction to Java2D

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. CS202 Java Object Oriented ProgrammingGUI Programming – Introduction to Java2D Chengyu Sun California State University, Los Angeles

  2. The paint() Method • How does Java draw the GUI components? • public void paint(Graphics g) • A method of java.awt.Component • Inherits by all AWT and Swing components • Responsible for “drawing” the component

  3. Customize paint() public class PaintBoard extends JFrame { … … public void paint( Graphics g ) { super.paint(g); // paint some more … } }

  4. paint() Again public class PaintBoard2D extends JFrame { … … public void paint( Graphics g ) { super.paint(g); Graphics2D g2 = (Graphics2D) g; // paint some more … } }

  5. Old Friends • Graphics2D is a subclass of Graphics • drawLine, drawString • draw/fillRect, draw/fillOval, draw/fillArc • ...

  6. New Friends in java.awt.geom • Shape • Line2D, Arc2D, Ellipse2D, Rectangle2D, RoundedRectangle2D • Curves • GeneralPath (generalized polygon) • Area (boolean of shapes ) • g2.draw( Shape ), g2.fill( Shape ) • http://java.sun.com/docs/books/tutorial/2d/display/strokeandfill.html

  7. Coordinates • Float and double coordinates • Line2D.Float, Line2D.Double • Rectangle2D.Float, Rectangle2D.Double • Elipse2D.Float, Elipse2D.Double • Why?

  8. Stroke – Line Width and More • g2.setStroke( Stroke ) • BasicStroke • width • end caps • line joins • miter limit • dash pattern

  9. Paint – Fancy Colors • g2.setPaint( Paint ) • Paint • Color • GradientPaint • (x1, y1, color1, x2, y2, color2) • TexturePaint

  10. Transformations • It’s the coordinate system that moves. • Transformations • g2.translate(double x, double y) • g2.rotate(double theta, double x, double y) • g2.scale(double sx, double sy)

  11. Simple Animation public void paint( Graphics g ) { ... ... try { Thread.sleep(100); } catch( Exception e ) {} repaint(); }

More Related