1 / 15

CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods

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

Download Presentation

CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods

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 – Color and Drawing Methods Chengyu Sun California State University, Los Angeles

  2. The Need to Draw

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

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

  5. Drawing Area X (??) (0,0) (??) (??) Y

  6. Graphics Class • In java.awt package • Draw lines • Draw strings • Draw un/filled rectangles, ovals, arcs, and polygons • Set and get drawing color • Set and get drawing fonts • Clear a rectangular region • … more

  7. Line • void drawLine( int x1, int y1, int x2, int y2 ) (x1,y1) (x2,y2)

  8. Rectangle • void drawRect( int x, int y, int w, int h ) • void fillRect(int x, int y, int w, int h ) • void clearRect( int x, int y, int w, int h ) w (x,y) h

  9. Oval • void drawOval( int x, int y, int w, int h ) • void fillOval(int x, int y, int w, int h ) w (x,y) h

  10. Arc • void drawArc( int x, int y, int w, int h, int startAngle, int arcAngle ) • void fillArc(int x, int y, int w, int h, int startAngle, int arcAngle ) • Arc • Begins at startAngle • Extends arcAngle • 0 degree is at the 3 clock position • startAngle must be 0 or positive • arcAngle • Positive – counter clock-wise rotation • Negative – clock-wise rotation

  11. String • void drawString( String s, int x, int y ) Welcome to Java (x,y)

  12. Color Class • In java.awt package • Predefined colors • Color.red, Color.black, Color.blue … • Exercise: read Java API doc for Color • Constructor • Color( int r, int g, int b ) • getGreen(), getRed(), getBlue() • … more

  13. Drawing color g.getColor() g.setColor() Background color getBackground() setBackground( Color c) Program with Color // get current drawing color Color c = g.getColor(); // draw some red stuff g.setColor( Color.red ); … // draw some yellow stuff g.setColor( Color.yellow ); … // restore the original drawing color g.setColor( c );

  14. Color Chooser • JColorChooser • Color showDialog( Component parent, String title, Color initialColor ) • A static method • Could return null

  15. Happy Face Example

More Related