// FlyingArrowActivity.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2012-01-23 File created. // 2015-12-11 Last modification. /* 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 FlyingArrowActivity 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 FlyingArrowActivity.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 320 x 480. */ // 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 flying.arrow ; 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 FlyingArrowView extends View { public FlyingArrowView( Context context ) { super( context ) ; setBackgroundColor( 0xFFFFFFE0 ) ; // Light Yellow } @Override protected void onDraw( Canvas canvas ) { Paint filling_paint = new Paint() ; filling_paint.setStyle( Paint.Style.FILL ) ; filling_paint.setColor( Color.MAGENTA ) ; Paint outline_paint = new Paint() ; outline_paint.setStyle( Paint.Style.STROKE ) ; // Default color for a Paint is black. // The arrow coordinates are selected so that Point (0, 0) // is at the tip of the arrow, and the arrow points upwards. int[] arrow_shape_coordinates_x = { 0, 15, 5, 5, 15, 0, -15, -5, -5, -15 } ; int[] arrow_shape_coordinates_y = { 0, 40, 30, 120, 160, 130, 160, 120, 30, 40 } ; Path flying_arrow = new Path() ; flying_arrow.moveTo( arrow_shape_coordinates_x[ 0 ], arrow_shape_coordinates_y[ 0 ] ) ; for ( int point_index = 1 ; point_index < arrow_shape_coordinates_x.length ; point_index ++ ) { flying_arrow.lineTo( arrow_shape_coordinates_x[ point_index ], arrow_shape_coordinates_y[ point_index ] ) ; } flying_arrow.close() ; canvas.translate( 150, 250 ) ; // arrow tip to point (150, 250 ) canvas.drawPath( flying_arrow, filling_paint ) ; // draw solid arrow canvas.rotate( 45 ) ; // 45 degrees clockwise canvas.drawPath( flying_arrow, outline_paint ) ; // draw a hollow arrow canvas.translate( 0, -200 ) ; // flying "up" 200 points canvas.drawPath( flying_arrow, filling_paint ) ; // If you are running this program on an Android device that has // a wide screen, try to de-comment the lines below that are // commented out. canvas.rotate( 45 ) ; // 45 degrees clockwise //canvas.translate( 0, -200 ) ; // flying "up" (i.e. to the right) canvas.drawPath( flying_arrow, filling_paint ) ; //canvas.translate( 0, -100 ) ; // flying "up" 100 points canvas.rotate( 90 ) ; // 90 degrees clockwise canvas.scale( 1.5F, 1.5F ) ; // magnify everything by 1.5 canvas.drawPath( flying_arrow, outline_paint ) ; // draw a hollow arrow canvas.translate( 0, -200 ) ; // flying "up" (i.e. down) 300 points canvas.drawPath( flying_arrow, filling_paint ) ; } } public class FlyingArrowActivity extends Activity { FlyingArrowView flying_arrow_view; @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; flying_arrow_view = new FlyingArrowView( this ) ; setContentView( flying_arrow_view ) ; } }