Penulis kali ini akan membagikan Program Diagram 2D yang menggunakan
hasil input user untuk ditampilkan kedalam diagram . Program ini masih
terbilang newbie , namun Penulis akan mencoba menghasilkan Program
Diagram yang lebih sulit dan bagus lagi seperti menghasilkan Diagram
yang bersifat 3D dan dapat menyimpan hasil output kedalam Microsoft Word
. Berikut Program Diagram 2D Java GUI buatan Penulis :
Hasil Output yang keluar akan seperti berikut ini :
Contoh Program dapat Anda download DISINI
Sekian Program kali ini , Semoga Bermanfaat ya!
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;
public class Pie extends Applet implements
ActionListener {
TextField input1, input2, input3,input4;
Button b1;
int
number[], degrees[];
int
sum, counter;
public void init()
{
sum=0;
counter=0;
number=new int[4];
degrees=new int[4];
JLabel inputlabel=new JLabel("Fill Data in the Blank");
JLabel nama1=new JLabel("Data 1 : ");
JLabel nama2=new JLabel("Data 2 : ");
JLabel nama3=new JLabel("Data 3 : ");
JLabel nama4=new JLabel("Data 4 : ");
inputlabel.setBounds(170,280,150,20);
nama1.setBounds(130,305,50,10);
nama2.setBounds(130,325,50,10);
nama3.setBounds(130,345,50,10);
nama4.setBounds(130,365,50,10);
input1=new TextField(5);
input2=new TextField(5);
input3=new TextField(5);
input4=new TextField(5);
input1.setBounds(220,300,150,20);
input2.setBounds(220,320,150,20);
input3.setBounds(220,340,150,20);
input4.setBounds(220,360,150,20);
b1=new Button("Draw Piechart");
b1.setBounds(190,400,90,30);
setLayout(null);
add(inputlabel);
add(nama1);
add(nama2);
add(nama3);
add(nama4);
add(input1);
add(input2);
add(input3);
add(input4);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed (ActionEvent e)
{
try{
number[0]=Integer.parseInt(input1.getText());
number[1]=Integer.parseInt(input2.getText());
number[2]=Integer.parseInt(input3.getText());
number[3]=Integer.parseInt(input4.getText());
sum=number[0]+number[1]+number[2]+number[3];
degrees[0]=number[0]*360/sum;
degrees[1]=number[1]*360/sum;
degrees[2]=number[2]*360/sum;
degrees[3]=360-degrees[0]-degrees[1]-degrees[2];
repaint();
}
catch(Exception ex){
JOptionPane.showMessageDialog(null,"Fill in the Blanks with
Number");
}
}
public void paint(Graphics g){
super.paint(g); Graphics2D g2=(Graphics2D) g;
{
g.setColor(Color.red);
g.fillArc(80,60,200,200,0,degrees[0]);
g.setColor(Color.green);
g.fillArc(80,60,200,200,degrees[0], degrees[1]);
g.setColor(Color.blue);
g.fillArc(80,60,200,200,degrees[0]+degrees[1],degrees[2]);
g.setColor(Color.cyan);
g.fillArc(80,60,200,200,degrees[0]+degrees[1]+degrees[2],degrees[3]);
g2.setColor(Color.black);
g2.drawString("Legend :", 330, 100);
g2.setColor(Color.red);
g2.drawString("Data 1", 330, 130);
g2.setColor(Color.green);
g2.drawString("Data 2", 330, 160);
g2.setColor(Color.blue);
g2.drawString("Data 3", 330, 190);
g2.setColor(Color.cyan);
g2.drawString("Data 4", 330, 220);
}
}
public static void main(String[]args){
Pie app = new Pie();
JFrame
frame = new JFrame ("Diagram Application");
frame.getContentPane().add(app);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,480);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
app.init();
app.repaint();
}
}
|
Hasil Output yang keluar akan seperti berikut ini :
Contoh Program dapat Anda download DISINI
Sekian Program kali ini , Semoga Bermanfaat ya!
EmoticonEmoticon