Home > Android > How To Develop Graphics(String and Shape) in Android

How To Develop Graphics(String and Shape) in Android

in the latest article, we discuss about J2ME Game development version 1.0, in this article, we discuss how to develop graphics in android. the differences in my opinion is:

1. different super class. in J2ME, we usually using canvas, in android, we can use View class

2. different method. in Android, we can use onDraw class

ok, lets begin

– Make 2 class in your android projects. Main.java and Game.java

– paste this script into Game.java


package game.game;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class Game extends View{
 private Context ctx;
 private float x,y;
 private float posX ;
 private float posY = getHeight()/2;
 public Game(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 this.ctx = context;
 }
 @Override
 protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 try
 {
 posX = getWidth()/2;
 posY = getHeight()/2;
 Paint paint = new Paint();
 paint.setColor(Color.rgb(255, 255, 255));
 paint.setTextSize(20);
 canvas.drawCircle(getWidth()/2, getHeight()/2, 50, paint);
 canvas.drawText("OCTAV", getWidth()/2+50,getHeight()/2+50, paint);
 canvas.drawText("X : "+x, 30, 30, paint);
 canvas.drawText("\nY : "+y, 100, 100, paint);
 }

<!--more-->
 catch (Exception e) {
 // TODO: handle exception
 Toast.makeText(ctx, "Error : "+e.getMessage(), Toast.LENGTH_LONG).show();
 }
 }
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
 // TODO Auto-generated method stub
 if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN)
 {
 Toast.makeText(ctx, "Ditarik kebawah", Toast.LENGTH_LONG).show();
 }
 return true;
 }
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 // TODO Auto-generated method stub
 x = event.getX();
 y = event.getY();
 this.invalidate();
 return true;
 }
}

– paste this script into Main.java


package game.game;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 try
 {
 setContentView(new Game(this));
 }
 catch (Exception e) {
 System.out.println("Error : "+e.getMessage());
 }
 }
}

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s