COPAS

LightBlog

Tuesday, July 5, 2011

Contoh Running Teks pada java

Animasi ini merupakan contoh running teks pada java applet. menggunakan tehnik double-buffering dan clipping-region... tau nga artinya clipping-region pasti da tau ... klw nga tau buat apa low cari tentang running teks.... lansung aja ... baris source nya.. klw low mau cepat copas aja.... hahahah....
yg berwarna biru ya... ya..
// animasi dengan teknik double-buffering dan clipping-region:
// program ini menganimasikan sebaris teks di atas latar belakang yang
// cukup rumit

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Animasi extends Applet implements Runnable {
static final int deltax = 1;
static final int deltay = 1;
static final String message = "Running teks";
int x = 0;
int y = 0;
Color c1 = new Color(0x0000ff);
Color c2 = new Color(0xffffff);
Font font = new Font("TimesRoman", Font.BOLD, 25);
Image offscreen;
int imagewidth, imageheight;
int stringwidth, stringheight, stringascent;
Thread animator = null;
boolean please_stop = false;

// mencari ukuran teks yang akan dianimasikan. Ukuran ini
// diperlukan untuk menentukan clipping-rectangle
public void init(){
FontMetrics fm = this.getFontMetrics(font);
stringwidth = fm.stringWidth(message);
stringheight = fm.getHeight();
stringascent = fm.getAscent();
// agar bisa stop/mulai animasi dengan klik mouse
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
if (animator != null) please_stop = true;
else {please_stop = false; start();}
}
});
}

// memulai animasi
public void start(){
animator = new Thread(this);
animator.start();
}

// stop animasi
public void stop() {
if (animator != null) animator.stop();
animator = null;
}

//Gambar latar belakan dengan interpolasi warna
void drawBackground(Graphics gr, Color c1, Color c2, int numsteps){
int r,g,b;
int dr = (c2.getRed() - c1.getRed())/numsteps;
int dg = (c2.getGreen() - c1.getGreen())/numsteps;
int db = (c2.getBlue() - c1.getBlue())/numsteps;
Dimension size = this.getSize();
int w = size.width, h = size.height;
int dw = size.width/numsteps;
int dh = size.height/numsteps;

gr.setColor(c1);
gr.fillRect(0,0,w,h);

for (r = c1.getRed(), g = c1.getGreen(), b = c1.getBlue();
h > 0;
h -= dh, w -=dw, r+=dr, g += dg, b+= db){
gr.setColor(new Color(r,g,b));
gr.fillArc(-w, -h, 2*w, 2*h, 0, -90);
}
}

// Menggambar latar belakang dan teks pada posisi sekarang.
public void paint (Graphics g) {
drawBackground(g,c1,c2,25);
g.setColor(Color.black);
g.setFont(font);
g.drawString(message, x,y);
}

// Kerja si animator
public void run(){
while(!please_stop) {
Dimension d = this.getSize();

// buat citra offscreen dengan ukuran yang sesuai
if ((offscreen == null)||
((imagewidth != d.width) || (imageheight != d.height))){
offscreen = this.createImage(d.width, d.height);
imagewidth = d.width;
imageheight = d.height;
}

// siapkan clipping-region

// rectangle lama
Rectangle oldrect = new Rectangle(x, y=stringascent, stringwidth, stringheight);

// update koordinat x dan y untuk animasi
x = ((x+deltax)%d.width);
y = ((y+deltay)%d.height);

//rectangle baru
Rectangle newrect = new Rectangle(x, y-stringascent, stringwidth, stringheight);

// cari union dari kedua rectangle
Rectangle r = newrect.union(oldrect);

// pakai rectangle r sebagai clippping region
Graphics g = offscreen.getGraphics();
g.clipRect(r.x, r.y, r.width, r.height);

//buat citra lepas-layar
paint(g);

// salin citra ke layar applet, pakai clipping
g = this.getGraphics();
g.clipRect(r.x, r.y, r.width, r.height);
g.drawImage(offscreen, 0,0,this);

//tunggu sebentar dan kerja lagi!
try {Thread.sleep(10);} catch (InterruptedException e) {}
}
animator = null;
}

}

No comments:

Post a Comment