// AceOfSpadesActivity.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2015-12-11 File created. // 2015-12-14 Last modification. /* This program draws the Ace of Spades card of playing cards into a view. This program is an Android application that shows how the coordinate system can be re-positioned (i.e."translated"), rotated, and scaled. In addition, you can see how to draw shapes with the Path class. To run this program, set up an Android Studio project, so that AceOfSpadesActivity is the name of the main activity class. You should also use the package name used below. After creating the project, you should replace the generated AceOfSpadesActivity.java file with this file. No XML definitions are necessary. To see all the drawings made by this program, the screen size should be at least 480 x 800. */ // Documentation of the Canvas and Path classes can be found at // http://developer.android.com/reference/android/graphics/Canvas.html // http://developer.android.com/reference/android/graphics/Path.html package ace.of.spades ; import android.app.Activity ; import android.os.Bundle ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. import android.view.View ; import android.content.Context ; final class AceOfSpadesView extends View { static int CARD_WIDTH = 300 ; static int CARD_HEIGHT = 400 ; Paint card_background_paint = new Paint() ; Paint card_outline_paint = new Paint() ; Paint spades_suit_paint = new Paint() ; Path spade_shape = new Path() ; public AceOfSpadesView( Context context ) { super( context ) ; setBackgroundColor( 0xFF32CD32 ) ; // LimeGreen card_background_paint.setStyle( Paint.Style.FILL ) ; card_background_paint.setColor( 0xFFF4A460 ) ; // SandyBrown color card_outline_paint.setStyle( Paint.Style.STROKE ) ; card_outline_paint.setColor( Color.YELLOW ) ; card_outline_paint.setStrokeWidth( 6 ) ; spades_suit_paint.setStyle( Paint.Style.FILL ) ; spades_suit_paint.setColor( Color.BLACK ) ; spades_suit_paint.setTypeface( Typeface.SANS_SERIF ) ; spades_suit_paint.setTextSize( 36 ) ; // Next we build the Path that will be used to draw the spade spade_shape.moveTo( 0, 20 ) ; spade_shape.addArc( 0, -30, 100, 70, 180, -180 ) ; spade_shape.cubicTo( 100,-40, 60,-70, 0, -100 ) ; // right upper part spade_shape.cubicTo( -60,-70,-100,-40, -100, 20 ) ; // left upper part spade_shape.addArc( -100, -30, 0, 70, 180, -180 ) ; 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.close() ; } @Override protected void onDraw( Canvas canvas ) { int card_center_point_x = getWidth() / 2 ; int card_center_point_y = getHeight() / 2 ; canvas.drawRoundRect( card_center_point_x - CARD_WIDTH / 2 + 3, card_center_point_y - CARD_HEIGHT / 2 + 3, card_center_point_x + CARD_WIDTH / 2 - 6, card_center_point_y + CARD_HEIGHT / 2 - 6, 6, 6, card_background_paint ) ; canvas.drawRoundRect( card_center_point_x - CARD_WIDTH / 2 + 3, card_center_point_y - CARD_HEIGHT / 2 + 3, card_center_point_x + CARD_WIDTH / 2 - 6, card_center_point_y + CARD_HEIGHT / 2 - 6, 6, 6, card_outline_paint ) ; canvas.translate( card_center_point_x, card_center_point_y ) ; canvas.save() ; canvas.drawPath( spade_shape, card_outline_paint ) ; // outline for the large spade canvas.drawPath( spade_shape, spades_suit_paint ) ; // draw the large spade shape Rect letter_A_bounds = new Rect() ; spades_suit_paint.getTextBounds( "A", 0, 1, letter_A_bounds ) ; canvas.translate( - CARD_WIDTH * 2 / 5, - CARD_HEIGHT * 3 / 10 ) ; canvas.scale( 1.0F, 1.50F ) ; // scale in y direction to get a "high A" canvas.drawText( "A", - letter_A_bounds.width() / 2, - (int) ( letter_A_bounds.height() * 0.666 ), spades_suit_paint ) ; canvas.scale( 1.0F, 0.6666F ) ; // back to previous scale canvas.scale( 0.15F, 0.20F ) ; // scale to draw the small spade shape canvas.drawPath( spade_shape, spades_suit_paint ) ; // The following method call discards all previous scalings and // we can restart drawing activities from the center point of the card. canvas.restore() ; canvas.rotate( 180 ) ; // rotating to draw upside down canvas.translate( - CARD_WIDTH * 2 / 5, - CARD_HEIGHT * 3 / 10 ) ; canvas.scale( 1.0F, 1.50F ) ; // scale in y direction to get a "high A" canvas.drawText( "A", - letter_A_bounds.width() / 2, - (int) ( letter_A_bounds.height() * 0.666 ), spades_suit_paint ) ; canvas.scale( 1.0F, 0.6666F ) ; // back to previous scale canvas.scale( 0.15F, 0.20F ) ; // scale to draw the small spade shape canvas.drawPath( spade_shape, spades_suit_paint ) ; } } public class AceOfSpadesActivity extends Activity { @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; setContentView( new AceOfSpadesView( this ) ) ; } }