speecash

bisnis online, jual beli online, sistem pembayaran, pembayaran online, bisnis online

laman

Senin, 05 Desember 2011

Program bezier 6 point dengan java

import java.awt.*;
import javax.swing.*;
import java.math.*;
import java.awt.event.*;
class bazier
{
    double x1,x2,x3,x4,x5,x6;
    double y1,y2,y3,y4,y5,y6;
    double x,y,xt,yt;
    double t=0.005;
    double ax,bx,cx,dx,ex;
    double ay,by,cy,dy,ey;
    public void set_x_y(double x_1,double y_1,double x_2,double y_2,double x_3,double y_3,double x_4,double y_4,double x_5,double y_5,double x_6,double y_6)
    {
        x1=x_1;x2=x_2;x3=x_3;x4=x_4;x5=x_5;x6=x_6;
        y1=y_1;y2=y_2;y3=y_3;y4=y_4;y5=y_5;y6=y_6;
    }

    public void hitung_abcde_x()
    {
        ex=5.0*(x2-x1);
        dx=10.0*(x3-x2)-ex;
        cx=10.0*(x4-x3)-dx-ex;
        bx=5.0*(x5-x4)-cx-dx-ex;
        ax=x6-x1-bx-cx-dx-ex;
    }
    public void hitung_abcde_y()
    {
        ey=5.0*(y2-y1);
        dy=10.0*(y3-y2)-ey;
        cy=10.0*(y4-y3)-dy-ey;
        by=5.0*(y5-y4)-cy-dy-ey;
        ay=y6-y1-by-cy-dy-ey;
    }
}
class canvas extends Canvas
{
    bazier bzr6 = new bazier();
    public void paint(Graphics g)
    {
        //g.drawLine(0,100,50,0);            //titik 1 dan 2
        //g.drawLine(50,0,100,0);            //titik 2 dan 3
        //g.drawLine(100,0,120,200);        //titik 3 dan 4
        //g.drawLine(120,200,200,200);    //titik 4 dan 5
        //g.drawLine(200,200,400,0);        //titik 5 dan 6

        g.drawLine((int)bzr6.x1,(int)bzr6.y1,(int)bzr6.x2,(int)bzr6.y2);
        g.drawLine((int)bzr6.x2,(int)bzr6.y2,(int)bzr6.x3,(int)bzr6.y3);
        g.drawLine((int)bzr6.x3,(int)bzr6.y3,(int)bzr6.x4,(int)bzr6.y4);
        g.drawLine((int)bzr6.x4,(int)bzr6.y4,(int)bzr6.x5,(int)bzr6.y5);
        g.drawLine((int)bzr6.x5,(int)bzr6.y5,(int)bzr6.x6,(int)bzr6.y6);

                    //    1        2        3      4          5         6       
        //bzr6.set_x_y(0,100,50,0,100,0,120,200,200,200,400,0);
        bzr6.hitung_abcde_x();bzr6.hitung_abcde_y();
        //bzr6.x=bzr6.x1;bzr6.y=bzr6.y1;
        for(double i=bzr6.t;i<=1+bzr6.t;i+=bzr6.t)
        {
            bzr6.xt=(bzr6.ax*i*i*i*i*i)+(bzr6.bx*i*i*i*i)+(bzr6.cx*i*i*i)+(bzr6.dx*i*i)+(bzr6.ex*i)+bzr6.x1;
            bzr6.yt=(bzr6.ay*i*i*i*i*i)+(bzr6.by*i*i*i*i)+(bzr6.cy*i*i*i)+(bzr6.dy*i*i)+(bzr6.ey*i)+bzr6.y1;
            g.drawLine((int)Math.ceil(bzr6.x),(int)Math.ceil(bzr6.y),(int)Math.ceil(bzr6.xt),(int)Math.ceil(bzr6.yt));
            bzr6.x=bzr6.xt;
            bzr6.y=bzr6.yt;
        }
    }
}
class frame extends JFrame implements ActionListener
{
    private canvas kanvas = new canvas();
    private JButton ok = new JButton("OK");

    private JLabel x1 = new JLabel("masukkan nilai x1");
    private JTextField x_1 = new JTextField("",10);
    private JLabel x2 = new JLabel("masukkan nilai x2");
    private JTextField x_2 = new JTextField("",10);
    private JLabel x3 = new JLabel("masukkan nilai x3");
    private JTextField x_3 = new JTextField("",10);
    private JLabel x4 = new JLabel("masukkan nilai x4");
    private JTextField x_4 = new JTextField("",10);
    private JLabel x5 = new JLabel("masukkan nilai x5");
    private JTextField x_5 = new JTextField("",10);
    private JLabel x6 = new JLabel("masukkan nilai x6");
    private JTextField x_6 = new JTextField("",10);

    private JLabel y1 = new JLabel("masukkan nilai y1");
    private JTextField y_1 = new JTextField("",10);
    private JLabel y2 = new JLabel("masukkan nilai y2");
    private JTextField y_2 = new JTextField("",10);
    private JLabel y3 = new JLabel("masukkan nilai y3");
    private JTextField y_3 = new JTextField("",10);
    private JLabel y4 = new JLabel("masukkan nilai y4");
    private JTextField y_4 = new JTextField("",10);
    private JLabel y5 = new JLabel("masukkan nilai y5");
    private JTextField y_5 = new JTextField("",10);
    private JLabel y6 = new JLabel("masukkan nilai y6");
    private JTextField y_6 = new JTextField("",10);

    private JPanel panel = new JPanel();
    private JLabel l = new JLabel(" ");
    frame()
    {
        super("gambar garis");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(800,0);
        setSize(600,500);
       
        ok.addActionListener(this);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(kanvas,BorderLayout.CENTER);
       
        panel.setLayout(new GridLayout(7,4));
        panel.add(x1);panel.add(x_1);
        panel.add(y1);panel.add(y_1);
        panel.add(x2);panel.add(x_2);
        panel.add(y2);panel.add(y_2);
        panel.add(x3);panel.add(x_3);
        panel.add(y3);panel.add(y_3);
        panel.add(x4);panel.add(x_4);
        panel.add(y4);panel.add(y_4);
        panel.add(x5);panel.add(x_5);
        panel.add(y5);panel.add(y_5);
        panel.add(x6);panel.add(x_6);
        panel.add(y6);panel.add(y_6);

        panel.add(l);panel.add(ok);
        getContentPane().add(panel,BorderLayout.SOUTH);       
        show();
    }
    public void actionPerformed(ActionEvent e)
    {
        Object O = e.getSource();
        if(O==ok)
        {
            int x1=Integer.parseInt(x_1.getText());
            int y1=Integer.parseInt(y_1.getText());
            int x2=Integer.parseInt(x_2.getText());
            int y2=Integer.parseInt(y_2.getText());
            int x3=Integer.parseInt(x_3.getText());
            int y3=Integer.parseInt(y_3.getText());
            int x4=Integer.parseInt(x_4.getText());
            int y4=Integer.parseInt(y_4.getText());
            int x5=Integer.parseInt(x_5.getText());
            int y5=Integer.parseInt(y_5.getText());
            int x6=Integer.parseInt(x_6.getText());
            int y6=Integer.parseInt(y_6.getText());
           
            kanvas.bzr6.set_x_y(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6);
            kanvas.bzr6.x= x1; kanvas.bzr6.y= y1;
            kanvas.repaint();
        }
    }
}
class demo_bazer_6point
{
    public static void main(String[] args)
    {
        new frame();
    }
}

Tidak ada komentar:

Posting Komentar