Docsity

Java Graphics: Drawing 2D Shapes and Controlling Colors and Fonts, Papers of Computer Science

Partial preview of the text

Download Java Graphics: Drawing 2D Shapes and Controlling Colors and Fonts and more Papers Computer Science in PDF only on Docsity!

CS

Java Handout # 10

Graphics and Java2D

Outline

  • Introduction
  • Color Control
    • Example: ShowColors
    • Example: ShowColors
  • Font Control
    • Example: Fonts
  • Graphics Methods
    • Example: LinesRectsOvals
    • Example: DrawArcs
    • Example: DrawPolygons
  • Java 2D API
    • Example: Shapes
  • Summary

Java Coordinate System

  • 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
    • Units are measured in pixels

X a xis

Y a xis

(0, 0)

( x , (^) y )

  • x

  • y

Graphics Context 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

Color Class Static Constants and

RGB Values

  • public final static Color orange orange 255, 200, Color Constant Color RGB value
  • public final static Color pink pink 255, 175,
  • public final static Color cyan cyan 0, 255,
  • public final static Color magenta magenta 255, 0,
  • public final static Color yellow yellow 255, 255,
  • public final static Color black black 0, 0,
  • public final static Color white white 255, 255,
  • public final static Color gray gray 128, 128,
  • public final static Color lightGray light gray 192, 192,
  • public final static Color darkGray dark gray 64, 64,
  • public final static Color red red 255, 0,
  • public final static Color green green 0, 255,
  • public final static Color blue blue 0, 0,

Color Methods and Color-related

Graphics Methods

Method Description

public Color( int r , int g , int b )

Creates a color based on red, green and blue contents

expressed as integers from 0 to 255.

public Color( float r , float g , float b )

Creates a color based on red, green and blue contents

expressed as floating-point values from 0.0 to 1.0.

public int getRed() // Color class

Returns a value between 0 and 255 representing the red

content.

public int getGreen() // Color class

Returns a value between 0 and 255 representing the green

content.

public int getBlue() // Color class

Returns a value between 0 and 255 representing the blue

content.

public Color getColor() // Graphics class

Returns a Color object representing the current color for the

graphics context.

public void setColor( Color c ) // Graphics class

Sets the current color for drawing with the graphics context.

Example: ShowColors

22 // draw rectangles and Strings in different colors 23 public void paint( Graphics g ) 24 { 25 // call superclass's paint method 26 super.paint( g ); 27 28 // set new drawing color using integers 29 g.setColor( new Color( 255, 0, 0 ) ); 30 g.fillRect( 25, 25, 100, 20 ); 31 g.drawString( "Current RGB: " + g.getColor(), 130, 40 ); 32 33 // set new drawing color using floats 34 g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); 35 g.fillRect( 25, 50, 100, 20 ); 36 g.drawString( "Current RGB: " + g.getColor(), 130, 65 ); 37 38 // set new drawing color using static Color objects 39 g.setColor( Color.blue ); 40 g.fillRect( 25, 75, 100, 20 ); 41 g.drawString( "Current RGB: " + g.getColor(), 130, 90 ); 42

Example: ShowColors

43 // display individual RGB values 44 Color color = Color.magenta; 45 g.setColor( color ); 46 g.fillRect( 25, 100, 100, 20 ); 47 g.drawString( "RGB values: " + color.getRed() + ", " + 48 color.getGreen() + ", " + color.getBlue(), 130, 115 ); 49 } 50 51 // execute application 52 public static void main( String args[] ) 53 { 54 ShowColors application = new ShowColors(); 55 56 application.setDefaultCloseOperation( 57 JFrame.EXIT_ON_CLOSE ); 58 } 59 60 } // end class ShowColors

Example: ShowColors

21 container = getContentPane(); 22 container.setLayout( new FlowLayout() ); 23 24 // set up changeColorButton and register its event handler 25 changeColorButton = new JButton( "Change Color" ); 26 27 changeColorButton.addActionListener( 28 29 // anonymous inner class 30 new ActionListener() { 31 32 // display JColorChooser when user clicks button 33 public void actionPerformed( ActionEvent event ) 34 { 35 color = JColorChooser.showDialog(

36 ShowColors2.this, "Choose a color", color ); 37 38 // set default color, if no color is returned 39 if ( color == null ) 40 color = Color.lightGray; 41

Example: ShowColors

42 // change content pane's background color 43 container.setBackground( color ); 44 } 45 46 } // end anonymous inner class 47 48 ); // end call to addActionListener 49 50 container.add( changeColorButton ); 51 52 setSize( 400, 130 ); 53 setVisible( true ); 54 } 55 56 // execute application 57 public static void main( String args[] ) 58 { 59 ShowColors2 application = new ShowColors2(); 60 61 application.setDefaultCloseOperation( 62 JFrame.EXIT_ON_CLOSE );

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)

Font Methods, Constants and

Font-related Graphics Methods

Method or constant Description

public final static int PLAIN // Font class

A constant representing a plain font style.

public final static int BOLD // Font class

A constant representing a bold font style.

public final static int ITALIC // Font class

A constant representing an italic font style.

public Font( String name ,^ int style , int size )

Creates a Font object with the specified font, style and size.

public int getStyle() // Font class

Returns an integer value indicating the current font style.

public int getSize() // Font class

Returns an integer value indicating the current font size.

public String getName() // Font class

Returns the current font name as a string.

public String getFamily() // Font class

Returns the font’s family name as a string.

Example: Fonts

1 // Fonts.java 2 // Using fonts 3 4 // Java core packages 5 import java.awt.; 6 import java.awt.event.; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class Fonts extends JFrame { 12 13 // set window's title bar and dimensions 14 public Fonts() 15 { 16 super( "Using fonts" ); 17 18 setSize( 420, 125 ); 19 setVisible( true ); 20 } 21

Example: Fonts

22 // display Strings in different fonts and colors 23 public void paint( Graphics g ) 24 { 25 // call superclass's paint method 26 super.paint( g ); 27 28 // set current font to Serif (Times), bold, 12pt 29 // and draw a string 30 g.setFont( new Font( "Serif", Font.BOLD, 12 ) ); 31 g.drawString( "Serif 12 point bold.", 20, 50 ); 32 33 // set current font to Monospaced (Courier), 34 // italic, 24pt and draw a string 35 g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) ); 36 g.drawString( "Monospaced 24 point italic.", 20, 70 ); 37 38 // set current font to SansSerif (Helvetica), 39 // plain, 14pt and draw a string 40 g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); 41 g.drawString( "SansSerif 14 point plain.", 20, 90 ); 42