~:Catatan Belajar MazHel:~

Acak Warna sampai pusing ngeliatnya

Posted in java by m42h31 on 22 Agustus 2008

/*
@author : m42h31
@date    : 18-08-2008, 05:20 AM
*/

import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class RandomColor{
public final static Random acak=new Random();
private final static Dimension dimensi=new Dimension(50,50);
private final static int waktunya=60,variasi=60,next=128,panjangSisi=35,jrk=2;

public RandomColor(){  //konstruktornya
JFrame frame=new JFrame(“Acak Warna”);
Container con=new Container();
LayoutManager lm=new GridLayout(panjangSisi,panjangSisi,jrk,jrk);
con.setLayout(lm);
int luasPanel=panjangSisi*panjangSisi; //luas panel
for (int i=0;i<luasPanel;i++ )
{    con.add(new Animasi()); }
frame.add(con);
frame.setBounds(100,100,500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public static Color acakWarna(){  //random warna
int wr1=next+acak.nextInt(next);
int wr2=next+acak.nextInt(next);
int wr3=next+acak.nextInt(next);
Color warna=new Color(wr1,wr2,wr3);
return (warna);
}

public static void main(String[]args){ //main program
new RandomColor();
}

public class Animasi extends JPanel        //inner class 1
{
private Timer timer;
public Animasi(){
timer=new Timer();
int phs=acak.nextInt(variasi);
int drf=acak.nextInt(variasi);
timer.scheduleAtFixedRate(new PanelTimer(this),phs,waktunya+drf);

}
public void paint(Graphics gp){
gp.setColor(acakWarna());
gp.fillRect(0,0,dimensi.width,dimensi.height);
}
}

private class PanelTimer extends TimerTask { //inner class 2
private JPanel panel;
PanelTimer(Animasi panel){
this.panel=panel;
}
public void run(){  //overide run() di kelas TimerTask
panel.repaint();
}
}
}

anda pusing membaca codinganku ??

silahkan compile code diatas lalu runing trus pandangi hasilnya , di jamin dalam 5 menit ………………….

tambah pusing…hehhehehe….

selamat mencoba…

3 Tanggapan

Subscribe to comments with RSS.

  1. echo said, on 22 Agustus 2008 at 17:38

    boros memory tuh bro source codenya😛

    pake konsep MVC dunk biar keren😀

  2. mbahsomo said, on 2 April 2009 at 11:17

    Mas echo😀 Sip

  3. m42h31 said, on 14 April 2009 at 00:13

    sukron2 dah mamfir😀, emang boros memory pake ini, saya dah buat yg lebih simple&hemat memory, sourcecode-nya jg pendek.. tp sapa yg butuh beginian yah,ha..ha..ha..


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: