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! 1 CS402 Java Handout # 10 Graphics and Java2D 2 Outline • Introduction • Color Control – Example: ShowColors – Example: ShowColors2 • Font Control – Example: Fonts • Graphics Methods – Example: LinesRectsOvals – Example: DrawArcs – Example: DrawPolygons • Java 2D API – Example: Shapes • Summary 5 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 6 Color Control • Class Color – Defines methods and constants for manipulating colors – Colors are created from red, green and blue components • RGB values 7 Color Class Static Constants and RGB Values Color Constant Color RGB value public final static Color orange orange 255, 200, 0 public final static Color pink pink 255, 175, 175 public final static Color cyan cyan 0, 255, 255 public final static Color magenta magenta 255, 0, 255 public final static Color yellow yellow 255, 255, 0 public final static Color black black 0, 0, 0 public final static Color white white 255, 255, 255 public final static Color gray gray 128, 128, 128 public final static Color lightGray light gray 192, 192, 192 public final static Color darkGray dark gray 64, 64, 64 public final static Color red red 255, 0, 0 public final static Color green green 0, 255, 0 public final static Color blue blue 0, 0, 255 10 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 11 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 12 Example: ShowColors2 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 15 Example: ShowColors2 63 } 64 65 } // end class ShowColors2 16 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) 17 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. 20 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 21 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 22 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 25 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 26 Example: LinesRectsOvals 22 // display various lines, rectangles and ovals 23 public void paint( Graphics g ) 24 { 25 // call superclass's paint method 26 super.paint( g ); 27 28 g.setColor( Color.red ); 29 g.drawLine( 5, 30, 350, 30 ); 30 31 g.setColor( Color.blue ); 32 g.drawRect( 5, 40, 90, 55 ); 33 g.fillRect( 100, 40, 90, 55 ); 34 35 g.setColor( Color.cyan ); 36 g.fillRoundRect( 195, 40, 90, 55, 50, 50 ); 37 g.drawRoundRect( 290, 40, 90, 55, 20, 20 ); 38 39 g.setColor( Color.yellow ); 40 g.draw3DRect( 5, 100, 90, 55, true ); 41 g.fill3DRect( 100, 100, 90, 55, false ); 42 27 Example: LinesRectsOvals 43 g.setColor( Color.magenta ); 44 g.drawOval( 195, 100, 90, 55 ); 45 g.fillOval( 290, 100, 90, 55 ); 46 } 47 48 // execute application 49 public static void main( String args[] ) 50 { 51 LinesRectsOvals application = new LinesRectsOvals(); 52 53 application.setDefaultCloseOperation( 54 JFrame.EXIT_ON_CLOSE ); 55 } 56 57 } // end class LinesRectsOvals 30 Drawing Arcs • Arc – Portion of oval – Measured in degrees – Sweeps the number of degrees in arc angle – Sweep starts at starting angle • Counterclockwise sweep is measure in positive degrees • Clockwise sweep is measure in negative degrees 31 Positive and Negative arc Angles 90° 0°180° 270° 90° 0°180° 270° Positive angles Negative angles 32 Graphics methods for drawing arcs. Method Description public void drawArc( int x, int y, int width, int height, int startAngle, int arcAngle ) Draws an arc relative to the bounding rectangle’s top-left coordinates (x, y) with the specified width and height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees. public void fillArc( int x, int y, int width, int height, int startAngle, int arcAngle ) Draws a solid arc (i.e., a sector) relative to the bounding rectangle’s top-left coordinates (x, y) with the specified width and height. The arc segment is drawn starting at startAngle and sweeps arcAngle degrees. 35 Example: DrawArcs 40 // start at 0 and sweep -270 degrees 41 g.setColor( Color.yellow ); 42 g.drawRect( 185, 35, 80, 80 ); 43 g.setColor( Color.black ); 44 g.drawArc( 185, 35, 80, 80, 0, -270 ); 45 46 // start at 0 and sweep 360 degrees 47 g.fillArc( 15, 120, 80, 40, 0, 360 ); 48 49 // start at 270 and sweep -90 degrees 50 g.fillArc( 100, 120, 80, 40, 270, -90 ); 51 52 // start at 0 and sweep -270 degrees 53 g.fillArc( 185, 120, 80, 40, 0, -270 ); 54 } 55 56 // execute application 57 public static void main( String args[] ) 58 { 59 DrawArcs application = new DrawArcs(); 36 Example: DrawArcs 60 61 application.setDefaultCloseOperation( 62 JFrame.EXIT_ON_CLOSE ); 63 } 64 65 } // end class DrawArcs 37 Drawing Polygons and Polylines • Class Polygon – Polygons • Multisided shapes – Polylines • Series of connected points 40 Example: DrawPolygons 22 // draw polygons and polylines 23 public void paint( Graphics g ) 24 { 25 // call superclass's paint method 26 super.paint( g ); 27 28 int xValues[] = { 20, 40, 50, 30, 20, 15 }; 29 int yValues[] = { 50, 50, 60, 80, 80, 60 }; 30 Polygon polygon1 = new Polygon( xValues, yValues, 6 ); 31 32 g.drawPolygon( polygon1 ); 33 34 int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 }; 35 int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 }; 36 37 g.drawPolyline( xValues2, yValues2, 7 ); 38 39 int xValues3[] = { 120, 140, 150, 190 }; 40 int yValues3[] = { 40, 70, 80, 60 }; 41 42 g.fillPolygon( xValues3, yValues3, 4 ); 41 Example: DrawPolygons 43 44 Polygon polygon2 = new Polygon(); 45 polygon2.addPoint( 165, 135 ); 46 polygon2.addPoint( 175, 150 ); 47 polygon2.addPoint( 270, 200 ); 48 polygon2.addPoint( 200, 220 ); 49 polygon2.addPoint( 130, 180 ); 50 51 g.fillPolygon( polygon2 ); 52 } 53 54 // execute application 55 public static void main( String args[] ) 56 { 57 DrawPolygons application = new DrawPolygons(); 58 59 application.setDefaultCloseOperation( 60 JFrame.EXIT_ON_CLOSE ); 61 } 62 63 } // end class DrawPolygons 42 Java 2D API • Java 2D API – Provides advanced 2D graphics capabilities • java.awt • java.awt.image • java.awt.color • java.awt.font.geom • java.awt.print • java.awt.image.renderable – Uses class java.awt.Graphics2D • Extends class java.awt.Graphics 45 Example: Shapes 22 } 23 24 // draw shapes with Java2D API 25 public void paint( Graphics g ) 26 { 27 // call superclass's paint method 28 super.paint( g ); 29 30 // create 2D by casting g to Graphics2D 31 Graphics2D g2d = ( Graphics2D ) g; 32 33 // draw 2D ellipse filled with a blue-yellow gradient 34 g2d.setPaint( new GradientPaint( 5, 30, Color.blue, 35, 35 100, Color.yellow, true ) ); 36 g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) ); 37 38 // draw 2D rectangle in red 39 g2d.setPaint( Color.red ); 40 g2d.setStroke( new BasicStroke( 10.0f ) ); 41 g2d.draw( new Rectangle2D.Double( 80, 30, 65, 100 ) ); 42 46 Example: Shapes 43 // draw 2D rounded rectangle with a buffered background 44 BufferedImage buffImage = new BufferedImage( 45 10, 10, BufferedImage.TYPE_INT_RGB ); 46 47 Graphics2D gg = buffImage.createGraphics(); 48 gg.setColor( Color.yellow ); // draw in yellow 49 gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle 50 gg.setColor( Color.black ); // draw in black 51 gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle 52 gg.setColor( Color.blue ); // draw in blue 53 gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle 54 gg.setColor( Color.red ); // draw in red 55 gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle 56 57 // paint buffImage onto the JFrame 58 g2d.setPaint( new TexturePaint( 59 buffImage, new Rectangle( 10, 10 ) ) ); 60 g2d.fill( new RoundRectangle2D.Double( 61 155, 30, 75, 100, 50, 50 ) ); 62 47 Example: Shapes 63 // draw 2D pie-shaped arc in white 64 g2d.setPaint( Color.white ); 65 g2d.setStroke( new BasicStroke( 6.0f ) ); 66 g2d.draw( new Arc2D.Double( 67 240, 30, 75, 100, 0, 270, Arc2D.PIE ) ); 68 69 // draw 2D lines in green and yellow 70 g2d.setPaint( Color.green ); 71 g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) ); 72 73 float dashes[] = { 10 }; 74 75 g2d.setPaint( Color.yellow ); 76 g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, 77 BasicStroke.JOIN_ROUND, 10, dashes, 0 ) ); 78 g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) ); 79 } 80 81 // execute application 82 public static void main( String args[] ) 83 {