speecash

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

laman

Senin, 05 Desember 2011

Program bezier 5 point dengan java

import java.awt.*;
import javax.swing.*;
import java.math.*;
import java.awt.event.*;
class bazier
{
    double x1,x2,x3,x4,x5;
    double y1,y2,y3,y4,y5;
    double x,y,xt,yt;
    double t=0.005;
    double ax,bx,cx,dx;
    double ay,by,cy,dy;
    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)
    {
        x1=x_1;x2=x_2;x3=x_3;x4=x_4;x5=x_5;
        y1=y_1;y2=y_2;y3=y_3;y4=y_4;y5=y_5;
    }

    public void hitung_abcd_x()
    {
        dx=4.0*(x2-x1);
        cx=6.0*(x3-x2)-dx;
        bx=4.0*(x4-x3)-cx-dx;
        ax=x5-x1-bx-cx-dx;
    }
    public void hitung_abcd_y()
    {
        dy=4.0*(y2-y1);
        cy=6.0*(y3-y2)-dy;
        by=4.0*(y4-y3)-cy-dy;
        ay=y5-y1-by-cy-dy;
    }
}
class canvas extends Canvas
{
    bazier bzr5 = 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,100);    //titik 4 dan 5

        g.drawLine((int)bzr5.x1,(int)bzr5.y1,(int)bzr5.x2,(int)bzr5.y2);
        g.drawLine((int)bzr5.x2,(int)bzr5.y2,(int)bzr5.x3,(int)bzr5.y3);
        g.drawLine((int)bzr5.x3,(int)bzr5.y3,(int)bzr5.x4,(int)bzr5.y4);
        g.drawLine((int)bzr5.x4,(int)bzr5.y4,(int)bzr5.x5,(int)bzr5.y5);
       
                    //    1        2        3      4          5       
        //bzr5.set_x_y(0,100,50,0,100,0,120,200,200,100);
        bzr5.hitung_abcd_x();bzr5.hitung_abcd_y();
        //bzr5.x=bzr5.x1;bzr5.y=bzr5.y1;
        for(double i=bzr5.t;i<=1+bzr5.t;i+=bzr5.t)
        {
            bzr5.xt=(bzr5.ax*i*i*i*i)+(bzr5.bx*i*i*i)+(bzr5.cx*i*i)+(bzr5.dx*i)+bzr5.x1;
            bzr5.yt=(bzr5.ay*i*i*i*i)+(bzr5.by*i*i*i)+(bzr5.cy*i*i)+(bzr5.dy*i)+bzr5.y1;
            g.drawLine((int)Math.ceil(bzr5.x),(int)Math.ceil(bzr5.y),(int)Math.ceil(bzr5.xt),(int)Math.ceil(bzr5.yt));
            bzr5.x=bzr5.xt;
            bzr5.y=bzr5.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 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 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(6,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(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());
                       
            kanvas.bzr5.set_x_y(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);
            kanvas.bzr5.x= x1; kanvas.bzr5.y= y1;
            kanvas.repaint();
        }
    }
}
class demo_bazer_5point
{
    public static void main(String[] args)
    {
        new frame();
    }
}

Tidak ada komentar:

Posting Komentar