Minggu, 12 Februari 2012

Grafik Sinus dan Cosinus - Grafika Komputer

Buat class KanvasAnimasi

import javax.swing.*;
import java.awt.*;

class KanvasAnimasi extends Canvas {
public void paint(Graphics g){
                
g.setColor(Color.white);
g.drawLine(10,10,10,210);
g.drawLine(5,110,380,110);
g.drawString("1",2,14);
g.drawString("-1",14,210);
g.drawString("0",13,123);
g.drawString("90",100,123);
g.drawString("180",190,123);
g.drawString("270",280,123);
g.drawString("360",370,123);        
g.setColor(Color.red);
for(int i=0; i<360;i+=1){
int y=(int)(Math.sin(i*Math.PI/180)*100)*-1;
g.drawOval(i+10,y+110,1,1);
try{
Thread.sleep(10);}
catch(Exception e){
}
}

g.setColor(Color.green);
for(int i=0; i<360;i+=1){
int y=(int)(Math.cos(i*Math.PI/180)*100)*-1;
g.drawOval(i+10,y+110,1,1);
try{
Thread.sleep(10);
}catch(Exception e){
}
}
}
}


Buat class Animasi untuk memanggil method tersebut

import javax.swing.*;
import java.awt.*;
class Animasi extends JFrame {
KanvasAnimasi gambar = new KanvasAnimasi();
Animasi(){
setTitle("Grafik Sinus dan Cosinus");
setLocation(200,100);
setSize(410,260);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void komponenVisual(){
getContentPane().setLayout(new BorderLayout());
getContentPane().add(gambar,BorderLayout.CENTER);
getContentPane().setBackground(Color.BLACK);
setVisible(true);
}

public static void main(String[]args){
Animasi anime = new Animasi();
anime.komponenVisual();
}
}



Tampilan output


Tidak ada komentar:

Posting Komentar