Postingan kali ini masih membahas mengenai grafika komputer dalam bahasa java, sub bab kali ini penulis akan membahas animasi dasar dala grafika komputer.
Bila kita akan membuat animasi dalam java pasti memerlukan yang namanya Thread, nah di dalam Thread, method yang paling penting adalah run() karena dalam method inilah semua tugas animasi akan dijalankan. sebelum menjalankan method run kita harus memanggil terlebih dahulu yang namanya start().
Untuk mengatur perilaku Thread anda memerlukan sleep() disini anda bisa mengatur kecepatan pergerakan object dan lain lain.. untuk contoh animasi sederhana berikut source codenya
import javax.swing.JPanel;
/**
*
* @author syauqil
* JalanKaki.java
*/
public class JalanKaki extends Thread implements Runnable {
JPanel panel;
int posisi;
public int getPosisi() {
return posisi;
}
public final int garis = 0;
int a;
public JalanKaki(JPanel panel, int posisi) {
this.panel = panel;
this.a = posisi;
}
@Override
public void run() {
while(true){
try{
Thread.sleep(30);
}catch(Exception e){}
finally{
if(a == garis){
posisi++;
if(posisi == 25){
posisi = 0;
}
panel.repaint();
}
}
}
}
}
/**
*
* @author syauqil
* JalanKaki.java
*/
public class JalanKaki extends Thread implements Runnable {
JPanel panel;
int posisi;
public int getPosisi() {
return posisi;
}
public final int garis = 0;
int a;
public JalanKaki(JPanel panel, int posisi) {
this.panel = panel;
this.a = posisi;
}
@Override
public void run() {
while(true){
try{
Thread.sleep(30);
}catch(Exception e){}
finally{
if(a == garis){
posisi++;
if(posisi == 25){
posisi = 0;
}
panel.repaint();
}
}
}
}
}
import javax.swing.JPanel;
/**
*
* @author syauqil
* JalanBadan.java
*/
public class JalanBadan extends Thread implements Runnable{
JPanel panel;
int posisi;
public int getPosisi() {
return posisi;
}
public final int kanan = 0;
int a;
public JalanBadan(JPanel panel, int a) {
this.panel = panel;
this.a = a;
}
@Override
public void run() {
while(true){
try{
Thread.sleep(40);
}catch(Exception e){}
finally{
if(a == kanan){
posisi++;
if(posisi==700){
posisi = 5;
}
}
}
panel.repaint();
}
}
}
/**
*
* @author syauqil
* JalanBadan.java
*/
public class JalanBadan extends Thread implements Runnable{
JPanel panel;
int posisi;
public int getPosisi() {
return posisi;
}
public final int kanan = 0;
int a;
public JalanBadan(JPanel panel, int a) {
this.panel = panel;
this.a = a;
}
@Override
public void run() {
while(true){
try{
Thread.sleep(40);
}catch(Exception e){}
finally{
if(a == kanan){
posisi++;
if(posisi==700){
posisi = 5;
}
}
}
panel.repaint();
}
}
}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author syauqil
* Android.java
*/
public class Android extends JApplet {
public static void main(String s[]) {
JFrame android = new JFrame();
android.setTitle("Android Jalan");
android.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Android();
applet.init();
android.getContentPane().add(applet);
android.pack();
android.setVisible(true);
}
@Override
public void init() {
JPanel panel = new Gambar();
getContentPane().add(panel);
}
}
class Gambar extends JPanel{
int x1 = 0;
int x2 = 0;
JalanBadan badan = new JalanBadan(this, 0);
JalanKaki kaki = new JalanKaki(this, 0);
public Gambar() {
setPreferredSize(new Dimension(1000, 500));
setBackground(Color.white);
badan.start();
kaki.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gd = (Graphics2D) g;
Shape atas = new Rectangle2D.Double(0, 0, 1000, 300);
GradientPaint paint = new GradientPaint(400, 300, new Color(100, 250, 255), 500, 0, Color.white);
gd.setPaint(paint);
gd.fill(atas);
Shape bawah = new Rectangle2D.Double(0, 300, 1000, 500);
GradientPaint paint1 = new GradientPaint(400, 400, new Color(20, 220, 55), 400, 300, new Color(0, 200, 220, 200));
gd.setPaint(paint1);
gd.fill(bawah);
//Jalan
gd.setColor(Color.black);
Shape jalan = new Rectangle2D.Double(0, 250, 1000, 200);
gd.fill(jalan);
gd.setColor(Color.white);
float[]array;
float mulai = 0.0f;
array = new float[1];
array[0] = 50.0f;
BasicStroke tengah = new BasicStroke(20.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 20.0f, array, mulai);
gd.setStroke(tengah);
gd.drawLine(0, 350, 1000, 350);
x1 = badan.getPosisi();
x2 = kaki.getPosisi();
gd.setColor(new Color(165, 201, 57));
gd.setStroke(new BasicStroke(3.0f));
GeneralPath gp = new GeneralPath();
gp.moveTo(x1 + 242, 180);
gp.lineTo(x1 + 358, 180);
gp.quadTo(x1 + 350, 140, x1 + 300, 137);
gp.quadTo(x1 + 250, 140, x1 + 242, 180);
gp.moveTo(x1 + 280, 140);
gp.lineTo(x1 + 265, 120);
gp.moveTo(x1 + 320, 140);
gp.lineTo(x1 + 335, 120);
gd.fill(gp);
gd.draw(gp);
gd.setColor(Color.white);
Shape putih = new Ellipse2D.Double(x1 + 270, 150, 13, 13);
gd.fill(putih);
Shape putih1 = new Ellipse2D.Double(x1 + 320, 150, 13, 13);
gd.fill(putih1);
Stroke an = new BasicStroke(20, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
gd.setStroke(an);
gd.setColor(new Color(165, 201, 57));
GeneralPath gp2 = new GeneralPath();
gp2.moveTo(x1 + 250, 200);
gp2.lineTo(x1 + 250, 290);
gp2.lineTo(x1 + 350, 290);
gp2.lineTo(x1 + 350, 200);
gp2.lineTo(x1 + 250, 200);
gp2.moveTo(x1 + 225, 210);
gp2.lineTo(x1 + 210, 260);
gp2.moveTo(x1 + 375, 210);
gp2.lineTo(x1 + -x2 + 390, 160);
gd.fill(gp2);
gd.draw(gp2);
//kaki
Stroke kaki = new BasicStroke(25, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
gd.setStroke(kaki);
gd.drawLine(x1 + 280, 290, x1 + -x2 + 280, 340);
gd.drawLine(x1 + 320, 290, x1 + -x2 + 329, 340);
//lisensi
gd.setColor(Color.black);
Font font = new Font("Courier New", Font.BOLD, 16);
gd.setFont(font);
gd.drawString("CREATED BY:", 55, 50);
Font font1 = new Font("Courier New", Font.BOLD, 36);
gd.setFont(font1);
gd.drawString("Syauqil", 30, 80);
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author syauqil
* Android.java
*/
public class Android extends JApplet {
public static void main(String s[]) {
JFrame android = new JFrame();
android.setTitle("Android Jalan");
android.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Android();
applet.init();
android.getContentPane().add(applet);
android.pack();
android.setVisible(true);
}
@Override
public void init() {
JPanel panel = new Gambar();
getContentPane().add(panel);
}
}
class Gambar extends JPanel{
int x1 = 0;
int x2 = 0;
JalanBadan badan = new JalanBadan(this, 0);
JalanKaki kaki = new JalanKaki(this, 0);
public Gambar() {
setPreferredSize(new Dimension(1000, 500));
setBackground(Color.white);
badan.start();
kaki.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gd = (Graphics2D) g;
Shape atas = new Rectangle2D.Double(0, 0, 1000, 300);
GradientPaint paint = new GradientPaint(400, 300, new Color(100, 250, 255), 500, 0, Color.white);
gd.setPaint(paint);
gd.fill(atas);
Shape bawah = new Rectangle2D.Double(0, 300, 1000, 500);
GradientPaint paint1 = new GradientPaint(400, 400, new Color(20, 220, 55), 400, 300, new Color(0, 200, 220, 200));
gd.setPaint(paint1);
gd.fill(bawah);
//Jalan
gd.setColor(Color.black);
Shape jalan = new Rectangle2D.Double(0, 250, 1000, 200);
gd.fill(jalan);
gd.setColor(Color.white);
float[]array;
float mulai = 0.0f;
array = new float[1];
array[0] = 50.0f;
BasicStroke tengah = new BasicStroke(20.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 20.0f, array, mulai);
gd.setStroke(tengah);
gd.drawLine(0, 350, 1000, 350);
x1 = badan.getPosisi();
x2 = kaki.getPosisi();
gd.setColor(new Color(165, 201, 57));
gd.setStroke(new BasicStroke(3.0f));
GeneralPath gp = new GeneralPath();
gp.moveTo(x1 + 242, 180);
gp.lineTo(x1 + 358, 180);
gp.quadTo(x1 + 350, 140, x1 + 300, 137);
gp.quadTo(x1 + 250, 140, x1 + 242, 180);
gp.moveTo(x1 + 280, 140);
gp.lineTo(x1 + 265, 120);
gp.moveTo(x1 + 320, 140);
gp.lineTo(x1 + 335, 120);
gd.fill(gp);
gd.draw(gp);
gd.setColor(Color.white);
Shape putih = new Ellipse2D.Double(x1 + 270, 150, 13, 13);
gd.fill(putih);
Shape putih1 = new Ellipse2D.Double(x1 + 320, 150, 13, 13);
gd.fill(putih1);
Stroke an = new BasicStroke(20, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
gd.setStroke(an);
gd.setColor(new Color(165, 201, 57));
GeneralPath gp2 = new GeneralPath();
gp2.moveTo(x1 + 250, 200);
gp2.lineTo(x1 + 250, 290);
gp2.lineTo(x1 + 350, 290);
gp2.lineTo(x1 + 350, 200);
gp2.lineTo(x1 + 250, 200);
gp2.moveTo(x1 + 225, 210);
gp2.lineTo(x1 + 210, 260);
gp2.moveTo(x1 + 375, 210);
gp2.lineTo(x1 + -x2 + 390, 160);
gd.fill(gp2);
gd.draw(gp2);
//kaki
Stroke kaki = new BasicStroke(25, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
gd.setStroke(kaki);
gd.drawLine(x1 + 280, 290, x1 + -x2 + 280, 340);
gd.drawLine(x1 + 320, 290, x1 + -x2 + 329, 340);
//lisensi
gd.setColor(Color.black);
Font font = new Font("Courier New", Font.BOLD, 16);
gd.setFont(font);
gd.drawString("CREATED BY:", 55, 50);
Font font1 = new Font("Courier New", Font.BOLD, 36);
gd.setFont(font1);
gd.drawString("Syauqil", 30, 80);
}
}
ntar hasilnya kurang lebih seperti ini
Contoh di atas adalah pengembangan dari postingan sebelumnya yaitu java text
nb: sengaja source codenya saya tampilkan agar Anda Belajar menghadapi & memahami sedikit kerumitan
Sekian terima kasih
mantab gan. .
ReplyDeleteterus amalkan ilmunya melaui posingan agan. .
Terima kasih atas kunjungannya...
ReplyDeleteaq coba pake netbean 6.8 dan jdk 1.7 kok g bisa tampil ya
ReplyDelete#shinta: Bisa dijelasin errornya gimana?
ReplyDeletegan yang gerak nanti pas di run apanya ya ?
ReplyDeleteapakah jalannya yang gerak dan logo androidnya yang diam atau sebaliknya ?
Maaf saya nanya begini karena di laptop saya belum terinstal netbeans dikarenakan space yang sudah penuh dan maunya saya terapkan di komputer kampus saya yang telah terinstal netbeans. Jadi tolong gan dijawab pertanyaan saya ya ^_^
thanks ka sourch kodenya :v saya nyoba berhasil ka maaf ka saya jga izin ngeposting hasilnya di sosmed :v
ReplyDelete