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 uasgrafkom;
/**
*
* @author AlvianLingga
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.Line2D.Double;
import java.awt.geom.Rectangle2D;
import java.awt.GradientPaint;
/**
*
* @author AlvianLingga
*/
public class Uasgrafkom extends Frame implements Runnable{
Thread Animasi;
int Delay = 200;
int Range = 1500;
int Awal = 0;
int Akhir = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Uasgrafkom App = new Uasgrafkom();
}
public Uasgrafkom(){
setTitle("UAS GRAFKOM");
Author();
setSize(800, 400);
setBackground (new Color(56,179,237));
setLocation(230, 0);
addWindowListener(new WindowEventHandler());
show();
Animasi = new Thread(this);
Animasi.start();
}
void Author(){
setFont(new Font("Comic Sans MS",Font.BOLD,12));
setForeground(Color.white);
}
public void paint(Graphics g){
//gradasi
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(new GradientPaint(50,0,Color.black,0,getHeight(), Color.blue));
g.fillRect(0, 0, getWidth(), getHeight());
g.drawString("mobilku", 10, 1000);
g.setColor(Color.ORANGE);
g.fillOval(550, 50, 90, 90);
g.setColor(Color.black);
g.fillOval(550, 50, 80, 80);
//AWANNNNNNNNNNNNNNNNN satu
g.setColor(Color.LIGHT_GRAY);
g.fillOval(1210-Awal, 50, 85, 55);
g.fillOval(1160-Awal, 50, 100, 45);
g.fillOval(1230-Awal, 50, 150, 75);
//Awan 2
g.setColor(Color.LIGHT_GRAY);
g.fillOval(920-Awal, 50, 205, 55);
g.fillOval(960-Awal, 50, 100, 45);
g.fillOval(930-Awal, 50, 150, 75);
//awan banyak
g.setColor(Color.LIGHT_GRAY);
g.fillOval(1310-Awal, 50, 115, 45);
g.fillOval(1060-Awal, 50, 130, 75);
g.fillOval(930-Awal, 50, 180, 75);
g.fillOval(210-Awal, 50, 105, 55);
g.fillOval(160-Awal, 50, 100, 45);
g.fillOval(230-Awal, 50, 150, 75);
g.fillOval(410-Awal, 50, 85, 55);
g.fillOval(360-Awal, 50, 100, 45);
g.fillOval(530-Awal, 50, 150, 75);
//prosedur Membuat jalan
g.setColor(Color.black);
g.fillRect(0, 300, 1020, 100);
g.setColor(Color.black);
//mobil
g.fillRect(205,305,100,30 ); //badan mobil bawah
g.fillOval(207,278,104,45); //badan mobil atas
g.setColor(Color.magenta);
g.fillRect(210,305,100,30 ); //badan mobil bawah
g.fillOval(210,280,100,45); //badan mobilatas
g.fillArc (180,298,60,70,0,180);// atas ban mobil
g.fillArc (285,298,60,70,0,180);// atas ban mobil
g.setColor(Color.red);
g.fillArc (318,301,23,23,0,80);// atas ban mobil
g.setColor(Color.DARK_GRAY); //ban mobil
g.fillOval(198, 313, 32,32); //ban mobil
g.fillOval(298, 313, 32,32); //ban mobil
g.setColor(Color.red); //ban mobil
g.fillOval(204, 319, 20,20); //ban mobil
g.fillOval(304, 319, 20,20); //ban mobil
g.setColor(Color.red);
g.fillArc (230,285,70,40,0,90);// atas ban mobil
g.setColor(Color.black);
g.fillArc (234,286,64,36,0,90);// atas ban mobil
g.setColor(Color.red);
g.fillArc (220,285,70,40,90,90);// atas ban mobil
g.setColor(Color.black);
g.fillArc (223,286,68,36,90,90);// atas ban mobil
g.setColor(Color.red);
g.fillRect(255,287,10,18 );
g.setColor(Color.white); //garis dalan
g.fillRect(100, 340, 70, 10);
g.setColor(Color.white);
g.fillRect(300, 340, 70, 10);
g.setColor(Color.white);
g.fillRect(500, 340, 70, 10);
g.setColor(Color.white);
g.fillRect(700, 340, 70, 10);
g.setColor(Color.black);
g.fillRect(400, 250, 10, 50);
g.setColor(Color.black);
g.fillRect(390, 210, 30, 60);
g.setColor(Color.green); // lampu merah
g.fillOval(400, 250, 10, 10);
g.setColor(Color.yellow);
g.fillOval(400, 235, 10, 10);
g.setColor(Color.red);
g.fillOval(400, 220, 10, 10);
g.setColor(Color.black); //lingguhan
g.fillRect(60, 260, 8, 40);
g.setColor(Color.black);
g.fillRect(100, 260, 8, 40);
g.setColor(Color.black);
g.fillRect(60, 280, 40, 8);
g.setColor(Color.darkGray); //meja
g.fillRect(44, 255, 80, 20);
}
//delay
public void run(){
do{
long time = System.currentTimeMillis();
if(time-Akhir>Delay){
//repaint();
try{
Thread.sleep(Delay);
}catch(InterruptedException ex){
}
Awal = Awal+10;
Awal%=Range;
Akhir=(int) time;
}
}while(true);
}
class WindowEventHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
* 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 uasgrafkom;
/**
*
* @author AlvianLingga
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.Line2D.Double;
import java.awt.geom.Rectangle2D;
import java.awt.GradientPaint;
/**
*
* @author AlvianLingga
*/
public class Uasgrafkom extends Frame implements Runnable{
Thread Animasi;
int Delay = 200;
int Range = 1500;
int Awal = 0;
int Akhir = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Uasgrafkom App = new Uasgrafkom();
}
public Uasgrafkom(){
setTitle("UAS GRAFKOM");
Author();
setSize(800, 400);
setBackground (new Color(56,179,237));
setLocation(230, 0);
addWindowListener(new WindowEventHandler());
show();
Animasi = new Thread(this);
Animasi.start();
}
void Author(){
setFont(new Font("Comic Sans MS",Font.BOLD,12));
setForeground(Color.white);
}
public void paint(Graphics g){
//gradasi
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(new GradientPaint(50,0,Color.black,0,getHeight(), Color.blue));
g.fillRect(0, 0, getWidth(), getHeight());
g.drawString("mobilku", 10, 1000);
g.setColor(Color.ORANGE);
g.fillOval(550, 50, 90, 90);
g.setColor(Color.black);
g.fillOval(550, 50, 80, 80);
//AWANNNNNNNNNNNNNNNNN satu
g.setColor(Color.LIGHT_GRAY);
g.fillOval(1210-Awal, 50, 85, 55);
g.fillOval(1160-Awal, 50, 100, 45);
g.fillOval(1230-Awal, 50, 150, 75);
//Awan 2
g.setColor(Color.LIGHT_GRAY);
g.fillOval(920-Awal, 50, 205, 55);
g.fillOval(960-Awal, 50, 100, 45);
g.fillOval(930-Awal, 50, 150, 75);
//awan banyak
g.setColor(Color.LIGHT_GRAY);
g.fillOval(1310-Awal, 50, 115, 45);
g.fillOval(1060-Awal, 50, 130, 75);
g.fillOval(930-Awal, 50, 180, 75);
g.fillOval(210-Awal, 50, 105, 55);
g.fillOval(160-Awal, 50, 100, 45);
g.fillOval(230-Awal, 50, 150, 75);
g.fillOval(410-Awal, 50, 85, 55);
g.fillOval(360-Awal, 50, 100, 45);
g.fillOval(530-Awal, 50, 150, 75);
//prosedur Membuat jalan
g.setColor(Color.black);
g.fillRect(0, 300, 1020, 100);
g.setColor(Color.black);
//mobil
g.fillRect(205,305,100,30 ); //badan mobil bawah
g.fillOval(207,278,104,45); //badan mobil atas
g.setColor(Color.magenta);
g.fillRect(210,305,100,30 ); //badan mobil bawah
g.fillOval(210,280,100,45); //badan mobilatas
g.fillArc (180,298,60,70,0,180);// atas ban mobil
g.fillArc (285,298,60,70,0,180);// atas ban mobil
g.setColor(Color.red);
g.fillArc (318,301,23,23,0,80);// atas ban mobil
g.setColor(Color.DARK_GRAY); //ban mobil
g.fillOval(198, 313, 32,32); //ban mobil
g.fillOval(298, 313, 32,32); //ban mobil
g.setColor(Color.red); //ban mobil
g.fillOval(204, 319, 20,20); //ban mobil
g.fillOval(304, 319, 20,20); //ban mobil
g.setColor(Color.red);
g.fillArc (230,285,70,40,0,90);// atas ban mobil
g.setColor(Color.black);
g.fillArc (234,286,64,36,0,90);// atas ban mobil
g.setColor(Color.red);
g.fillArc (220,285,70,40,90,90);// atas ban mobil
g.setColor(Color.black);
g.fillArc (223,286,68,36,90,90);// atas ban mobil
g.setColor(Color.red);
g.fillRect(255,287,10,18 );
g.setColor(Color.white); //garis dalan
g.fillRect(100, 340, 70, 10);
g.setColor(Color.white);
g.fillRect(300, 340, 70, 10);
g.setColor(Color.white);
g.fillRect(500, 340, 70, 10);
g.setColor(Color.white);
g.fillRect(700, 340, 70, 10);
g.setColor(Color.black);
g.fillRect(400, 250, 10, 50);
g.setColor(Color.black);
g.fillRect(390, 210, 30, 60);
g.setColor(Color.green); // lampu merah
g.fillOval(400, 250, 10, 10);
g.setColor(Color.yellow);
g.fillOval(400, 235, 10, 10);
g.setColor(Color.red);
g.fillOval(400, 220, 10, 10);
g.setColor(Color.black); //lingguhan
g.fillRect(60, 260, 8, 40);
g.setColor(Color.black);
g.fillRect(100, 260, 8, 40);
g.setColor(Color.black);
g.fillRect(60, 280, 40, 8);
g.setColor(Color.darkGray); //meja
g.fillRect(44, 255, 80, 20);
}
//delay
public void run(){
do{
long time = System.currentTimeMillis();
if(time-Akhir>Delay){
//repaint();
try{
Thread.sleep(Delay);
}catch(InterruptedException ex){
}
Awal = Awal+10;
Awal%=Range;
Akhir=(int) time;
}
}while(true);
}
class WindowEventHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
Semoga tutorial yang kami bagi ini dapat membantu sobat sekalian.
SELAMAT BELAJAR
0 Response to "Tutorial Grafika Komputer Menggunakan JAVA 2D ( ANIMASI 7 ) "
Post a Comment