import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JFrame;
class kanvas extends Canvas{
int x0, y0, x1, y1;
int dx,dy,steps;
int xInc,yInc,x,y;
void input(int x00, int y00, int x11, int y11, Graphics g){
x0 = x00;
y0 = y00;
x1 = x11;
y1 = y11;
dx = x1 - x0;
dy = y1 - y0;
if (Math.abs(dx) > Math.abs(dy)){
steps = Math.abs(dx);
}
else{
steps = Math.abs(dy);
}
xInc = dx / steps;
yInc = dy / steps;
x = x0;
y = y0;
g.drawString(".",Math.round(x), Math.round(y));
for (int i = 0; i< steps ;i++){
x += xInc;
y += yInc;
g.drawString(".",Math.round(x), Math.round(y));
try{
Thread.sleep(20);
}catch (InterruptedException ie){
System.out.println(ie.getMessage());
}
}
}
public void paint(Graphics g) {
input(10,10,400,400,g);
}
}
class Garis extends JFrame{
kanvas gambar = new kanvas();
Garis(){
super("Garis");
setLocation(400,250);
setSize(700,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void komponen(){
getContentPane().setLayout(new BorderLayout());
getContentPane().add(gambar,BorderLayout.CENTER);
setVisible(true);
}
public static void main(String [] args){
Garis r = new Garis();
r.komponen();
}
}
Tidak ada komentar:
Posting Komentar