import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
class garis
{
double x1=0;double y1=0;double x2=0;double y2=0;
double x_inc,y_inc;
double dx,dy,max;
public void set_x_y(double x11,double y11,double x22,double y22)
{
x1=x11;y1=y11;
x2=x22;y2=y22;
}
public void d_x()
{
dx=x2-x1;
}
public void d_y()
{
dy=y2-y1;
}
public void maximum()
{
if(dx<=dy)
{
max = dy;
}
else
{
max = dx;
}
}
public void Xinc()
{
x_inc = (dx/max);
}
public void Yinc()
{
y_inc =(dy/max);
}
}
class canvas extends Canvas
{
public garis gar = new garis();
public void paint(Graphics g)
{
//gar.set_x_y(0,300,800,300);
gar.d_x();gar.d_y();gar.maximum();gar.Xinc();gar.Yinc();
for(int i=0;i<gar.max;i++)
{
g.drawLine((int)Math.ceil(gar.x1),(int)Math.ceil(gar.y1),(int)Math.ceil(gar.x1+gar.x_inc),(int)Math.ceil(gar.y1+gar.y_inc));
gar.x1=(gar.x1+gar.x_inc);
gar.y1=(gar.y1+gar.y_inc);
}
}
}
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 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 JPanel panel = new JPanel();
private JLabel l = new JLabel(" ");
frame()
{
super("gambar garis");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200,0);
setSize(800,600);
ok.addActionListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(kanvas,BorderLayout.CENTER);
panel.setLayout(new GridLayout(5,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(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());
kanvas.gar.set_x_y(x1,y1,x2,y2);
System.out.println("dx = "+kanvas.gar.dx+" dy = "+kanvas.gar.dy+" mak = "+kanvas.gar.max);
kanvas.repaint();
}
}
}
class demo_dda
{
public static void main(String[] args)
{
new frame();
}
}
Komentar ini telah dihapus oleh administrator blog.
BalasHapus