speecash

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

laman

Kamis, 10 November 2011

program bazier 4 point dengan java

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

    public void hitung_abc_x()
    {
        cx=3.0*(x2-x1);
        bx=3.0*(x3-x2)-cx;
        ax=x4-x1-cx-bx;
    }
    public void hitung_abc_y()
    {
        cy=3.0*(y2-y1);
        by=3.0*(y3-y2)-cy;
        ay=y4-y1-cy-by;
    }
}
class canvas extends Canvas
{
    bazier bzr = new bazier();
    public void paint(Graphics g)
    {
        g.drawLine((int)bzr.x1,(int)bzr.y1,(int)bzr.x2,(int)bzr.y2);
        g.drawLine((int)bzr.x3,(int)bzr.y3,(int)bzr.x4,(int)bzr.y4);
         //bzr.set_x_y(x1,y1,x2,y2,x3,y3,x4,y4);
         bzr.hitung_abc_x(); bzr.hitung_abc_y();
         //bzr.x= x1; bzr.y= y1;
        for(double i= bzr.t;i<=1+ bzr.t;i+= bzr.t)
        {
            bzr.xt=( bzr.ax*i*i*i)+( bzr.bx*i*i)+( bzr.cx*i)+ bzr.x1;
            bzr.yt=( bzr.ay*i*i*i)+( bzr.by*i*i)+( bzr.cy*i)+ bzr.y1;
            g.drawLine((int)Math.ceil( bzr.x),(int)Math.ceil( bzr.y),(int)Math.ceil( bzr.xt),(int)Math.ceil( bzr.yt));
            bzr.x= bzr.xt;
            bzr.y= bzr.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 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 JPanel panel = new JPanel();
    private JLabel l = new JLabel(" ");
    frame()
    {
        super("gambar garis");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(100,100);
        setSize(1000,600);
        ok.addActionListener(this);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(kanvas,BorderLayout.CENTER);
        panel.setLayout(new GridLayout(9,2));
        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(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());
            kanvas.bzr.set_x_y(x1,y1,x2,y2,x3,y3,x4,y4);
            kanvas.bzr.x= x1; kanvas.bzr.y= y1;

            kanvas.repaint();
        }
    }
}
class demo_bazer_post
{
    public static void main(String[] args)
    {
        new frame();
    }
}

Tidak ada komentar:

Posting Komentar