Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

An overview of java's graphics capabilities, focusing on drawing 2d shapes, controlling colors, and fonts using the java 2d api. It covers creating custom shapes, filling them with colors and patterns, and controlling the java coordinate system and graphics context.

Typology: Papers

Pre 2010

Uploaded on 08/16/2009

koofers-user-8p9
koofers-user-8p9 🇺🇸

10 documents

1 / 49

Toggle sidebar

Related documents


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

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

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 Control

• Class Color

  • Defines methods and constants for manipulating colors
  • Colors are created from red, green and blue components
    • RGB values

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

1 // ShowColors.java 2 // Demonstrating Colors. 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 ShowColors extends JFrame { 12 13 // constructor sets window's title bar string and dimensions 14 public ShowColors() 15 { 16 super( "Using colors" ); 17 18 setSize( 400, 130 ); 19 setVisible( true ); 20 } 21

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

1 // ShowColors2.java 2 // Demonstrating JColorChooser. 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 ShowColors2 extends JFrame { 12 private JButton changeColorButton; 13 private Color color = Color.lightGray; 14 private Container container; 15 16 // set up GUI 17 public ShowColors2() 18 { 19 super( "Using JColorChooser" ); 20

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 );

Example: ShowColors

63 }

64

65 } // end class ShowColors

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.

Font Methods, Constants and

Font-related Graphics Methods

Me thod or c o ns tant De s c riptio n public boolean isPlain() // Font class

Tests a font for a plain font style. Returns (^) true if the font is plain. public boolean isBold() // Font class

Tests a font for a bold font style. Returns true if the font is bold. public boolean isItalic() // Font class

Tests a font for an italic font style. Returns true if the font is italic. public Font getFont() // Graphics class

Returns a Font object reference representing the current font. public void setFont( Font f ) // Graphics class

Sets the current font to the font, style and size specified by the Font object reference f.

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

Example: Fonts

43 // set current font to Serif (times), bold/italic, 44 // 18pt and draw a string 45 g.setColor( Color.red ); 46 g.setFont( 47 new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) ); 48 g.drawString( g.getFont().getName() + " " + 49 g.getFont().getSize() + 50 " point bold italic.", 20, 110 ); 51 } 52 53 // execute application 54 public static void main( String args[] ) 55 { 56 Fonts application = new Fonts(); 57 58 application.setDefaultCloseOperation( 59 JFrame.EXIT_ON_CLOSE ); 60 } 61 62 } // end class Fonts

Font Metrics

• Height

• Descent (amount character dips below baseline)

• Ascent (amount character rises above baseline)

• Leading (difference between descent and ascent)

leading

ascent

baseline descent

height

Graphics Methods That Draw

Lines, Rectangle and Ovals.

Method Description public void drawLine( int x1, int y1, int x2, int y2 )

Draws a line between the point ( x1 , y1 ) and the point ( x2 , y2 ).

public void drawRect( int x, int y, int width, int height )

Draws a rectangle of the specified (^) width and (^) height. The top- left corner of the rectangle has the coordinates ( x , y ). public void fillRect( int x, int y, int width, int height )

Draws a solid rectangle with the specified width and height. The top-left corner of the rectangle has the coordinate ( x , (^) y ). public void clearRect( int x, int y, int width, int height )

Draws a solid rectangle with the specified width and height in the current background color. The top-left corner of the rectangle has the coordinate ( x , y ). public void drawRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )

Draws a rectangle with rounded corners in the current color with the specified (^) width and (^) height. The (^) arcWidth and (^) arcHeight determine the rounding of the corners (see Fig. 11.15).

public void fillRoundRect( int x, int y, int width, int height, int arcWidth, int arcHeight )

Draws a solid rectangle with rounded corners in the current color with the specified (^) width and (^) height. The (^) arcWidth and arcHeight determine the rounding of the corners (see Fig. 11.15).

Graphics methods that draw

lines, rectangle and ovals.

Method Description

public void fill3DRect(

int x, int y, int width,

int height, boolean b )

Draws a filledthree-dimensional rectangle in the current color withthe specified width and height. The top-left corner of the rectangle hasthe coordinates ( x , (^) y ). The rectangle appears raised whenbis true andis loweredwhenbis false.

public void drawOval( int

x, int y, int width, int

height )

Drawsanoval inthe current color withthe specified width and height. The bounding rectangle’s top-left corner isat the coordinates ( x , (^) y ). Theoval touchesall four sidesof thebounding rectangle at thecenter of eachside (see Fig. 11.16).

public void fillOval( int

x, int y, int width, int

height )

Draws a filledoval inthe current color withthe specified width and height. The bounding rectangle’s top-left corner is at the coordinates ( x, y). The oval touches all four sides of the bounding rectangle at thecenter of eachside (see Fig. 11.16).

Example: LinesRectsOvals

1 // LinesRectsOvals.java 2 // Drawing lines, rectangles and ovals 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 LinesRectsOvals extends JFrame { 12 13 // set window's title bar String and dimensions 14 public LinesRectsOvals() 15 { 16 super( "Drawing lines, rectangles and ovals" ); 17 18 setSize( 400, 165 ); 19 setVisible( true ); 20 } 21