Tutorial Grafika Komputer Menggunakan JAVA 2D ( ANIMASI 3 )

Kali ini kami akan mencoba menjelaskan tentang bagaimana cara untuk membuat Animasi menggunakan Java Netbeen. Tak usah banyak basa basi lagi mari ikuti Langkah - langkah berikut :
1. Yang pertama perlu kita lakukan adalah kita buat file.java
2. Lalu kita isikan dengan scriptnya, sebagai berikut :

/*

* To change this license header, choose License Headers in

Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package ua;

/**

*

* @author VIRNA

*/

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;

public class Ua extends JApplet {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

JFrame android = new JFrame();

android.setTitle("Android Jalan");

android.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JApplet applet = new Ua();

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);

}

}

- JalanKaki.java

/*

* To change this license header, choose License Headers in

Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package ua;

/**

*

* @author VIRNA

*/

import javax.swing.JPanel;

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(20);

}catch(Exception e){}

finally{

if(a == garis){

posisi++;

if(posisi == 35){

posisi = 0;

}

panel.repaint();

}

}

}

}

}

- JalanBadan.java

/*

* To change this license header, choose License Headers in

Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package ua;

/**

*

* @author VIRNA

*/

import javax.swing.JPanel;

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();

}

}

}

3. Run project F6




Semoga tutorial yang kami bagi ini dapat membantu sobat sekalian.
SELAMAT BELAJAR

0 Response to "Tutorial Grafika Komputer Menggunakan JAVA 2D ( ANIMASI 3 )"

Post a Comment