jump to navigation

Object Oriented Programming February 17, 2009

Posted by yasinisasi in Java.
trackback

Akhirnya jadi juga aq bikin tutorial kecil-kecilan kayak gini nih. Di bawah ada contoh kecil-kecilan gimana kita mau bikin animasi bola yang slaling bertabrakan. Ada beberapa kelas pada file ini. File Ball.java, Form.java, Warna.java, dan Balls.java. Ball.java merupakan objek bola yang akan dijadikan animasi, Form.java adalah objek yang mendefinisikan beberapa bentuk yang akan digambar. Sedangkan file Warna.java merupakan file yang mendefinisikan warna dari masing-masing bola. Dan yang terakhir, yaitu Balls.java merupakan file yang mengakses semua Objek-objek tadi.
ini file Warna.java

import java.awt.Color;

public class Warna{

	Color[] colors={

		Color.BLACK,

		Color.BLUE,

		Color.CYAN,

		Color.DARK_GRAY,

		Color.GRAY,

		Color.GREEN,

		Color.LIGHT_GRAY,

		Color.MAGENTA,

		Color.ORANGE,

		Color.PINK,

		Color.RED,

		Color.YELLOW

	};

	private int indeks;

	public Warna(){

		indeks=(int)(Math.random()*12);

	}

	public Color getWarna(){

		indeks=(int)(Math.random()*12);

		return colors[indeks];

	}

}

Kalau yang ini file Form.java

import java.awt.*;
public class Form{
	private int indeks;
	private int x,y;
	public Form(int x, int y){
		this.x=x;
		this.y=y;
		indeks=(int)(Math.random()*11);
	}
	public void setPosisi(int x, int y){
		this.x=x;
		this.y=y;
	}
	private final short KOTAK_KOSONG=0;
	private final short KOTAK_PADAT=1;
	private final short LINGKARAN_KOSONG=2;
	private final short LINGKARAN_PADAT=3;
	private final short SEGITIGA_KOSONG=4;
	private final short SEGITIGA_PADAT=5;
	private final short GARIS=6;
	private final short PERSEGI_PANJANG_KOSONG=7;
	private final short PERSEGI_PANJANG_PADAT=8;
	private final short OVAL_KOSONG=9;
	private final short OVAL_PADAT=10;
	public void draw(Graphics g){
		switch(indeks){
			case KOTAK_KOSONG:
				g.drawRect(x,y,10,10);
			break;
			case KOTAK_PADAT:
				g.fillRect(x,y,10,10);
			break;
			case LINGKARAN_KOSONG:
				g.drawArc(x,y,10,10,0,360);
			break;
			case LINGKARAN_PADAT:
				g.fillArc(x,y,10,10,0,360);
			break;
			case SEGITIGA_KOSONG:
				int X[]={x,x+5,x+10};
				int Y[]={y+10,y,y+10};
				g.drawPolygon(X,Y,3);
			break;
			case GARIS:
				g.drawLine(x,y,x+10,y+10);
				g.drawLine(x+10,y+10,x,y);
			break;
			case PERSEGI_PANJANG_KOSONG:
				g.drawRect(x,y,5,10);
			break;
			case PERSEGI_PANJANG_PADAT:
				g.fillRect(x,y,5,10);
			break;
			case OVAL_KOSONG:
				g.drawOval(x,y,10,5);
			break;
			case OVAL_PADAT:
				g.fillOval(x,y,10,5);
			break;
		}
	}
}

Nah kalo yang ini file Ball.java

import java.awt.*;
public class Ball{
	private int x, y, dx, dy;
	private Color color;
	private Form form;
	Ball(int x,int y,Color color){
		this.x=x;
		this.y=y;
		this.color=color;
		dx = (int)(Math.random()*19)-9;
		dy = (int)(Math.random()*19)-9;
		form=new Form(x,y);
	}
	public int getX(){
		return x;
	}
	public int getY(){
		return y;
	}
	public int getDx(){
		return dx;
	}
	public int getDy(){
		return dy;
	}
	public Color getColor(){
		return color;
	}
	public void set(int x,int y,int dx,int dy){
		this.x=x;
		this.y=y;
		this.dx=dx;
		this.dy=dy;
		form.setPosisi(x,y);
	}
	public void setColor(Color color){
		this.color=color;
	}
	public void draw(Graphics g){
		g.setColor(color);
		form.draw(g);
	}
}

Nah, kalo yang ini file Balls.java

import java.awt.*;
class Balls extends Frame implements Runnable{
	private boolean running=false;
	private final int FRAME_DELAY=25;
	private final int NUM_BAL=50;
	private int x,y,dx,dy,minX,maxX,minY,maxY;
	private Ball[] ball=new Ball[NUM_BAL];
	public Balls(){
		super("Ball Animation");
		addMenus();
		pack();
		resize(600,800);
		initialize();
		show();
	}
	private void addMenus(){
		MenuBar menu=new MenuBar();
		Menu file=new Menu("File");
		file.add("Quit");
		file.add("Pause");
		file.add("Resume");
		menu.add(file);
		setMenuBar(menu);
	}
	public boolean handleEvent(Event evt){
		if(evt.id==Event.WINDOW_DESTROY){
			System.exit(0);
			return true;
		}
		else if(evt.id==Event.ACTION_EVENT && evt.target instanceof MenuItem){
			if("Quit".equals(evt.arg)){
				System.exit(0);
				return true;
			}
			else if("Pause".equals(evt.arg)){
				running=false;
				return true;
			}
			else if("Resume".equals(evt.arg)){
				running=true;
				return true;
			}
			else return false;
		}
		else return false;
	}
	private void initialize(){
		minX=0;
		minY=50;
		maxX=getWidth()-10;
		maxY=getHeight()-10;
		Warna warna=new Warna();
		for(int i=0;imaxX)
							dx=-dx;
						if(y-3maxY)
							dy=-dy;
						ball[i].set(x,y,dx,dy);
					}
					repaint();
				}
			}
		}
		catch(InterruptedException e){
			e.printStackTrace();
		}
	}
	public void paint(Graphics g){
		g.setColor(Color.WHITE);
		g.fillRect(0,0,getWidth(),getHeight());
		drawBall(g);
		g.drawRect(1,1,maxX,maxY);
	}
	private synchronized void drawBall(Graphics g){
		for(Ball bola:ball){
			bola.draw(g);
		}
	}
	public static void main(String[]args){
		new Balls().run();
	}
}

coba deh dimodifikasi biar tampilannya lebih asyik.

Comments»

1. armanketigabelas - February 23, 2009

kok pemPrograman se..
sengaja salah tulis ya??

hehehe..

tar dimarahin Bu Mahendra looo,,

2. yasinisasi - February 28, 2009

@arman: Hmmm kayaknya emang sengaja salah tulis deh.😀

3. Anak-Rokan - March 8, 2009

Keren sin……

4. gemalayrita - March 12, 2009

hahaha.. salah tulis ato ngeles.. ?? (hehe.. piss yaa)

wuihh..
yasin,, cool bangettt

5. bagudung - April 17, 2010

Maaf, mas mau tanya.. kalo mw gambar oval :

g.drawOval(….);

Trus, kalo mau gambar garis melengkung :

g.drawaArc(….);

Nah, saya mau tanya.. Kalo mau gambar juring lingkaran gmn mas ??


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

%d bloggers like this: