// AceOfSpadesApplet.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2008-01-14 File created. // 2010-01-28 Latest modification. This is now a JPanel-based applet. /* This program draws the playing card "ace of spades" to the screen. This is a Java applet that demonstrates: - Java 2D graphics - drawing of curved shapes with the curveTo() and quadTo() methods of class GeneralPath - the use of class TextLayout */ import java.awt.* ; import java.awt.geom.* ; // Classes GeneralPath etc. import javax.swing.* ; import java.awt.font.TextLayout ; class AceOfSpadesPanel extends JPanel { static int CARD_WIDTH = 300 ; static int CARD_HEIGHT = 400 ; int card_center_point_x = 500 ; int card_center_point_y = 350 ; public AceOfSpadesPanel() { setBackground( Color.GREEN.darker() ) ; } public void paintComponent( Graphics graphics ) { super.paintComponent( graphics ) ; Graphics2D graphics2D = (Graphics2D) graphics ; GeneralPath spade_shape = new GeneralPath() ; spade_shape.moveTo( 0, 20 ) ; spade_shape.append( new Arc2D.Double( 0, -30, 100, 100, 180, 180, Arc2D.OPEN ), true ) ; spade_shape.curveTo( 100,-40, 60,-70, 0, -100 ) ; // right upper part spade_shape.curveTo( -60,-70,-100,-40, -100, 20 ) ; // left upper part spade_shape.append( new Arc2D.Double( -100, -30, 100, 100, 180, 180, Arc2D.OPEN ), true ) ; spade_shape.quadTo( -10, 90, -20, 100 ) ; // left line of spade handle spade_shape.lineTo( 20, 100 ) ; spade_shape.quadTo( 10, 90, 0, 20 ) ; // right line of spade handle spade_shape.closePath() ; graphics2D.setColor( Color.LIGHT_GRAY ) ; graphics2D.fillRoundRect( card_center_point_x - CARD_WIDTH / 2 + 3, card_center_point_y - CARD_HEIGHT / 2 + 3, CARD_WIDTH - 6, CARD_HEIGHT - 6, 6, 6 ) ; graphics2D.setColor( Color.YELLOW ) ; graphics2D.setStroke( new BasicStroke( 6 ) ) ; graphics2D.drawRoundRect( card_center_point_x - CARD_WIDTH / 2 + 3, card_center_point_y - CARD_HEIGHT / 2 + 3, CARD_WIDTH - 6, CARD_HEIGHT - 6, 6, 6 ) ; graphics2D.translate( card_center_point_x, card_center_point_y ) ; AffineTransform saved_graphics_transform = graphics2D.getTransform() ; graphics2D.draw( spade_shape ) ; // colored outline for the large spade graphics2D.setColor( Color.BLACK ) ; graphics2D.fill( spade_shape ) ; // draw the large spade shape Font font_to_print_card_value = new Font( "SansSerif", Font.BOLD, 36 ) ; // By using the TextLayout class we can find out the width and // height of the text "A" which is the text that tells the card // value in textual form. TextLayout text_layout_of_value_text = new TextLayout( "A", font_to_print_card_value, graphics2D.getFontRenderContext() ) ; Rectangle2D bounds_of_value_text = text_layout_of_value_text.getBounds(); double width_of_value_text = bounds_of_value_text.getWidth() ; double height_of_value_text = bounds_of_value_text.getHeight() ; graphics2D.setFont( font_to_print_card_value ) ; graphics2D.translate( - CARD_WIDTH * 2 / 5, - CARD_HEIGHT * 3 / 10 ) ; graphics2D.scale( 1.0, 1.50 ) ; // scale in y direction to get a "high A" graphics2D.drawString( "A", - (int) ( width_of_value_text / 2 ), - (int) ( height_of_value_text * 0.666 ) ) ; graphics2D.scale( 1.0, 0.6666 ) ; // back to previous scale graphics2D.scale( 0.15, 0.20 ) ; // scale to draw the small spade shape graphics2D.fill( spade_shape ) ; // The following method call discards all previous scalings and // we can restart drawing activities from the center point of the card. graphics2D.setTransform( saved_graphics_transform ) ; graphics2D.rotate( Math.PI ) ; // rotating to draw upside down graphics2D.translate( - CARD_WIDTH * 2 / 5, - CARD_HEIGHT * 3 / 10 ) ; graphics2D.scale( 1.0, 1.50 ) ; // scale in y direction to get a "high A" graphics2D.drawString( "A", - (int) ( width_of_value_text / 2 ), - (int) ( height_of_value_text * 0.666 ) ) ; graphics2D.scale( 1.0, 0.6666 ) ; // back to previous scale graphics2D.scale( 0.15, 0.20 ) ; // scale to draw the small spade shape graphics2D.fill( spade_shape ) ; // draw a small spade upside down } } public class AceOfSpadesApplet extends JApplet { public void init() { getContentPane().add( new AceOfSpadesPanel() ) ; } }