Gambar diatas merupakan SIN dan TANGEN dalam Java.
Berikut ini adalah Source Code nya....
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class tugas_umar extends Canvas
{
int awal;
int akhir;
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawLine(10,10,10,210);
g.drawLine(10,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.blue);
for(int i=awal;i<akhir;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)
{
}
}
}
void nilaiAwal(int nawal)
{
awal=nawal;
}
void nilaiAkhir(int nakhir)
{
akhir=nakhir;
}
}
class Animasi_umar extends Canvas
{
int pertama;
int kedua;
public void paint(Graphics g)
{
g.setColor(Color.black);
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.magenta);
for(int i=pertama;i<kedua;i+=1)
{
int y=(int)(Math.tan(i*Math.PI/180)*100)*-1;
g.drawOval(i+10,y+110,1,1);
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
}
}
void nilaiPertama(int npertama)
{
pertama=npertama;
}
void nilaiKedua(int nkedua)
{
kedua=nkedua;
}
}
class animasi extends JFrame
{
tugas_umar Ka=new tugas_umar();
Animasi_umar Ka2=new Animasi_umar();
JLabel lblAwal=new JLabel("Nilai Awal");
JLabel lblAkhir=new JLabel("Nilai Akhir");
JTextField tx1=new JTextField(5);
JTextField tx2=new JTextField(5);
JButton but2=new JButton("Sin");
JButton but3=new JButton("Tan");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
animasi()
{
setTitle("ploting Fungsi Cosinus dan Tangen");
setLocation(200,200);
setSize(600,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
void KomponenVisual()
{
getContentPane().setLayout(new BorderLayout());
p1.add(lblAwal);
p1.add(tx1);
p1.add(lblAkhir);
p1.add(tx2);
p1.add(but2);
p1.add(but3);
getContentPane().add(p1,BorderLayout.NORTH);
setVisible(true);
}
public void sin()
{
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String batasAwal=tx1.getText();
int x=Integer.parseInt(batasAwal);
Ka.nilaiAwal(x);
String batasAkhir=tx2.getText();
int y=Integer.parseInt(batasAkhir);
Ka.nilaiAkhir(y);
getContentPane().add(Ka,BorderLayout.CENTER);
setVisible(true);
getContentPane().add(Ka2,BorderLayout.CENTER);
}
});
}
public void tan()
{
but3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String batas2Pertama=tx1.getText();
int x=Integer.parseInt(batas2Pertama);
Ka2.nilaiPertama(x);
String batas2Kedua=tx2.getText();
int y=Integer.parseInt(batas2Kedua);
Ka2.nilaiKedua(y);
getContentPane().add(Ka2,BorderLayout.CENTER);
setVisible(true);
getContentPane().add(Ka,BorderLayout.CENTER);
}
});
}
public static void main(String[]args)
{
animasi a1=new animasi();
a1.KomponenVisual();
a1.sin();
a1.tan();
}
}
0 komentar:
Posting Komentar