import java.awt.*;
import javax.swing.*;
import java.math.*;
import java.awt.event.*;
class bazier_3_point
{
double x1,x2,x3;
double y1,y2,y3;
double x,y,xt,yt;
double t=0.005;
double ax,bx;
double ay,by;
public void set_x_y(double x_1,double y_1,double x_2,double y_2,double x_3,double y_3)
{
x1=x_1;x2=x_2;x3=x_3;
y1=y_1;y2=y_2;y3=y_3;
}
public void hitung_ab_x()
{
bx=2.0*(x2-x1);
ax=x3-x1-bx;
}
public void hitung_ab_y()
{
by=2.0*(y2-y1);
ay=y3-y1-by;
}
}
class canvas extends Canvas
{
bazier_3_point bzr = new bazier_3_point();
public void paint(Graphics g)
{
g.drawLine((int)bzr.x1,(int)bzr.y1,(int)bzr.x2,(int)bzr.y2);
g.drawLine((int)bzr.x2,(int)bzr.y2,(int)bzr.x3,(int)bzr.y3);
//bzr.set_x_y(100,50,150,70,200,50);
bzr.hitung_ab_x();bzr.hitung_ab_y();
//bzr.x=bzr.x1;bzr.y=bzr.y1;
for(double i=bzr.t;i<=1+bzr.t;i+=bzr.t)
{
bzr.xt=(bzr.ax*i*i)+(bzr.bx*i)+bzr.x1;
bzr.yt=(bzr.ay*i*i)+(bzr.by*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 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 JPanel panel = new JPanel();
private JLabel l = new JLabel(" ");
frame()
{
super("gambar garis");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100,0);
setSize(800,600);
ok.addActionListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(kanvas,BorderLayout.CENTER);
panel.setLayout(new GridLayout(7,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(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());
kanvas.bzr.set_x_y(x1,y1,x2,y2,x3,y3);
kanvas.bzr.x= x1; kanvas.bzr.y= y1;
kanvas.repaint();
}
}
}
class demo_bazer_3point
{
public static void main(String[] args)
{
new frame();
}
}
Tidak ada komentar:
Posting Komentar