I would like to plot the line, preferably a dashed segment at a time. Given the co-ordinates of the 2 end points, we can find the equation of the line right? And hence deduce all points(pixels) that lie on this line? But i'm having trouble with plotting a segment at a time and finally reaching the 2nd end point and erasing the previous segments. It should look like we start plotting from (0,0) towards (5,5) and in the process we show the segment (0,0)->(1,1) then make this disappear and show the segment (1,1)->(2,2) and so on and finally show just the pixel at (5,5)
public class pracDraw extends JFrame {
private Color red=Color.red;
public int i;
private Color white = Color.white;
JPanel pr=new JPanel();
JTextField t=new JTextField();
JTextField t1=new JTextField();
JTextField t2=new JTextField();
JTextArea ta=new JTextArea();
pracDraw(){
setTitle("Practise");
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(t);
add(t1);
add(pr);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.drawLine(100, 100, 200, 200);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.BLUE);
g.drawLine(200, 200, 350, 375);
t.setBounds(390, 370, 10, 10);
t.setBackground(Color.RED);
t.setEnabled(false);
Thread t5=new Thread(){
public void run()
{
for(;;){
int x,y,x1=200,y1=200,x2=400,y2=400,t_x,t_y,slope;
slope=(y2-y1)/(x2-x1); //slope=1
int c=(y1-(slope*x1)); //c=0
for(i=200;i<375;i+=20){
if(i==(slope*i+c)){
t1.setBounds(i,i, 10, 10);
t1.setBackground(Color.RED);
t1.setEnabled(false);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
t5.start();
if(i>375){
t5.stop();
t1.setBackground(Color.GREEN);
}
}
public static void main(String args[]){
pracDraw p=new pracDraw();
}
}