Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
862 views4 pages

Pertemuan XIV Grafik

The document discusses graphics and drawing in Android mobile programming. It describes creating a new project and modifying the MainActivity class to draw simple shapes like circles, points, rectangles and lines on a canvas. It also covers drawing on touch events by adding points to an array list every time the user touches the screen and redrawing them on the canvas. The code examples show how to set up the drawing surface and handle touch events to allow for interactive graphics.

Uploaded by

Abdul Syukur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
862 views4 pages

Pertemuan XIV Grafik

The document discusses graphics and drawing in Android mobile programming. It describes creating a new project and modifying the MainActivity class to draw simple shapes like circles, points, rectangles and lines on a canvas. It also covers drawing on touch events by adding points to an array list every time the user touches the screen and redrawing them on the canvas. The code examples show how to set up the drawing surface and handle touch events to allow for interactive graphics.

Uploaded by

Abdul Syukur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Materi Mobile Programming (Pertemuan XIV) Universitas Pamulang

Pertemuan XIV
GRAFIK

14.1 Grafik
Buat project baru, desain form (file XML Layout) tidak perlu diubah. Kemudian
source code dalam class MainActivity diubah menjadi:

package com.unpam.graphicssimplepoint;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

setContentView(new MediaGambar(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it
is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private static class MediaGambar extends View {


private Paint areaPaint = new Paint();

public MediaGambar(Context context) {


super(context);

protected void onDraw(Canvas canvas) {


Paint paint = areaPaint;
int x, y;

canvas.drawColor(Color.WHITE);

paint.setColor(Color.MAGENTA);
canvas.drawCircle(60,60,50, paint);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(3);
canvas.drawPoint(60,60, paint);

[email protected] (2012-2013) page 1 of 4


Materi Mobile Programming (Pertemuan XIV) Universitas Pamulang

canvas.drawRect(180, 20, 260, 70, paint);

paint.setStrokeWidth(1);
paint.setColor(Color.RED);
x=0;
canvas.drawLine(x+10,80,x+10,320, paint);
y=0;
canvas.drawLine(0,y+200,400,y+200, paint);

paint.setColor(Color.BLUE);
for (x=0; x<=360; x++ ){
y = (int) (Math.sin(x*Math.PI/180)*100);
canvas.drawPoint(x+10,y+200,paint);

if ((x % 90) == 0){


canvas.drawText(Integer.toString(x), x+10,
215, paint);
paint.setStrokeWidth(3);
canvas.drawPoint(x+10,200,paint);
paint.setStrokeWidth(1);
}
}
}

14.2 Menggambar pada Metode onTouch


Buat project baru, desain form (file XML Layout) tidak perlu diubah. Kemudian
source code dalam class MainActivity diubah menjadi:

package com.unpam.graphicsmotion;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;

public class MainActivity extends Activity {


TempatGambar tempatGambar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tempatGambar = new TempatGambar(this);


tempatGambar.setBackgroundColor(Color.WHITE);
setContentView(tempatGambar);
tempatGambar.requestFocus();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it
is present.

[email protected] (2012-2013) page 2 of 4


Materi Mobile Programming (Pertemuan XIV) Universitas Pamulang

getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Dan buat class dengan nama TempatGambar, ubah source code menjadi seperti di
bawah ini:

package com.unpam.graphicsmotion;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class TempatGambar extends View implements OnTouchListener {


List<Titik> lokasiTitik = new ArrayList<Titik>();
Paint paint = new Paint();

public TempatGambar(Context context) {


super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
}

@Override
public void onDraw(Canvas canvas) {
for (Titik titik : lokasiTitik) {
canvas.drawCircle(titik.x, titik.y, 2, paint);
}
}

public boolean onTouch(View view, MotionEvent event) {


Titik titik = new Titik();
titik.x = event.getX();
titik.y = event.getY();
lokasiTitik.add(titik);
invalidate();
return true;
}
}

class Titik {
float x, y;
}

[email protected] (2012-2013) page 3 of 4


Materi Mobile Programming (Pertemuan XIV) Universitas Pamulang

Referensi:
1. Allen, Grant, Beginning Android 4, 2012, Apress, New York
2. H., Nazruddin Safaat, ANDROID Pemrograman Aplikasi Mobile Smartphone dan
Tablet PC Berbasis Android
3. http://www.oracle.com/
4. http://www.android.com/

[email protected] (2012-2013) page 4 of 4

You might also like