Ikuti Langkah-Langkah Dibawah ini Untuk Membuat Line Chart Menggunakan Java NetBeans :
1. Buatlah project baru di NetBeans, Pilih File > New Project
2. Pilih kategori di Java NetBeans, Pilih Java dan klik project Java Aplication
3. Klik Tombol Next
4. Setelah melakukan Langkah ke 3 buatlah project dengan nama "linechart" lalu klik Finish
5. Lalu copy script dibawah ini dan masukan ke project yang telah kalian buat tadi
Baca Juga :
package linechart;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
/**
*
* @author asus
*/
public class LineChart extends JPanel{
public LineChart() {
this.setPreferredSize(new Dimension(500, 340));
this.setBackground(Color.WHITE);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
gp.moveTo(30, 50);
gp.lineTo(370,50);
gp.moveTo(30,50);
gp.lineTo(30,290);
gp.moveTo(30, 290);
gp.lineTo(370,290);
gp.moveTo(30, 90);
gp.lineTo(370,90);
gp.moveTo(30, 130);
gp.lineTo(370,130);
gp.moveTo(30, 170);
gp.lineTo(370,170);
gp.moveTo(30, 210);
gp.lineTo(370,210);
gp.moveTo(30, 250);
gp.lineTo(370,250);
//LineCharts
gp.moveTo(45,230);
gp.lineTo(80,192);
gp.moveTo(80,192);
gp.lineTo(115,90);
gp.moveTo(115,90);
gp.lineTo(150,130);
gp.moveTo(150,130);
gp.lineTo(185,122);
gp.moveTo(185,122);
gp.lineTo(220,94);
gp.moveTo(220,94);
gp.lineTo(255,144);
gp.moveTo(255,144);
gp.lineTo(290,72);
gp.moveTo(290,72);
gp.lineTo(325,218);
gp.moveTo(325,218);
gp.lineTo(360,218);
gp.closePath();
g2.setColor(Color.blue);
gp.closePath();
Stroke s= new BasicStroke(2);
g2.setStroke(s);
g2.draw(gp);
g2.setColor(Color.BLACK);
g2.drawString("120",7,50);
//g2.drawString("110",5,70);
g2.drawString("100",7,90);
//g2.drawString("90",13,110);
g2.drawString("80",13,130);
//g2.drawString("70",13,150);
g2.drawString("60",13,170);
//g2.drawString("50",13,190);
g2.drawString("40",13,210);
//g2.drawString("30",13,230);
g2.drawString("20",13,250);
//g2.drawString("10", 13, 270);
g2.drawString("0",20,290);
g2.drawString("N01",40,310);
g2.drawString("N02",75,310);
g2.drawString("N03",110,310);
g2.drawString("N04",145,310);
g2.drawString("N05",180,310);
g2.drawString("N06",215,310);
g2.drawString("N07",250,310);
g2.drawString("N08",285,310);
g2.drawString("N09",320,310);
g2.drawString("N10",355,310);
Shape lingkaran1 = new Ellipse2D.Double(42,223,11,11); //30
Shape lingkaran2 = new Ellipse2D.Double(73,190,11,11); //51
Shape lingkaran3 = new Ellipse2D.Double(110,88,11,11); //100
Shape lingkaran4 = new Ellipse2D.Double(145,123,11,11); //80
Shape lingkaran5 = new Ellipse2D.Double(183,115,11,11); //84
Shape lingkaran6 = new Ellipse2D.Double(215,90,11,11); //98
Shape lingkaran7 = new Ellipse2D.Double(250,138,11,11); // 73
Shape lingkaran8 = new Ellipse2D.Double(285,66,11,11); // 109
Shape lingkaran9 = new Ellipse2D.Double(321,212,11,11); //36
Shape lingkaran10 = new Ellipse2D.Double(358,212,11,11); //36
g2.setColor(Color.red);
g2.fill(lingkaran1);
g2.fill(lingkaran2);
g2.fill(lingkaran3);
g2.fill(lingkaran4);
g2.fill(lingkaran5);
g2.fill(lingkaran6);
g2.fill(lingkaran7);
g2.fill(lingkaran8);
g2.fill(lingkaran9);
g2.fill(lingkaran10);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame("Line Charts");
frame.setSize(600,500);
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
//tambahkan panel
LineChart canvas = new LineChart();
frame.getContentPane().add(canvas);
frame.pack();
frame.setVisible(true);
}
}
Hasil :
SELAMAT MENCOBA!
No comments:
Post a Comment