0

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();

        }
}
user234198
  • 36
  • 2
  • Do you mean you are trying to implement a dashed line stroke? I think you might want to look up how to use [strokes](http://docs.oracle.com/javase/7/docs/api/java/awt/BasicStroke.html). Basically, before you draw the line segment (given by two points), you need to set the stroke of the `Graphics2D` object doing the drawing. – Jared Apr 26 '14 at 06:27
  • Yes it is a dashed line from point A(0,0) to point B(5,5). But what i actually want to draw is each segment (0,0)->(1,1), (1,1)->(2,2) and so on finally plotting the pixel B(5,5). In this process i would like to erase the previous segment before drawing the next segment.. i cant wrap my head around the logic of this... – user234198 Apr 26 '14 at 13:43
  • I have attached the code. If u run it you can see that the JTextField (t1) is drawn on the panel along the line but it does so with a gray border. How do i eliminate that grey border and only draw the text field along the line and finally make the text field green after it reaches the end of line? – user234198 Apr 27 '14 at 16:48
  • There are tons of things wrong with this code. Why are you starting a new thread on every single paint() call? the code `if(i > 375)` will only cause `t5` to stop if `i` actually is greater than 375 (which you are not guaranteed once the thread is started). – Jared Apr 29 '14 at 04:22
  • I've put it in a thread because i want to show or paint each trace of the text field onto the line from a(200,200 ) to B(400,400). I'm iterating i in +20 for the same reason. While painting onto the Frame it paints along with a grey background. How do i eliminate that? Yes i remove the infinite for loop and the stop() condition. My only problem now is the background grey color that comes along with the trace... – user234198 Apr 29 '14 at 11:55
  • You shouldn't be starting threads in `paintComponent`. You should have a single thread which calls `repaint()` multiple times (and pausing in between with [`Thread.sleep()`](http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html)). Each time it calls repaint it should increment the state of the panel so that the line drawn is in the "next" position. – Jared May 06 '14 at 19:26

0 Answers0