public void run() { while (Thread.currentThread() == animator) { line.setX1(line.getX1() + line.getX1Run() * line.getX1Dir()); line.setY1(line.getY1() + line.getY1Rise() * line.getY1Dir()); line.setX2(line.getX2() + line.getX2Run() * line.getX2Dir()); line.setY2(line.getY2() + line.getY2Rise() * line.getY2Dir()); if (line.getX1() < 0) { line.setX1(0); line.switchX1Dir(); } // end if else if (line.getX1() > this.getWidth()) { line.setX1(this.getWidth()); line.switchX1Dir(); } // end else if if (line.getY1() < 0) { line.setY1(0); line.switchY1Dir(); } // end if else if (line.getY1() > this.getHeight()) { line.setY1(this.getHeight()); line.switchY1Dir(); } // end else if if (line.getX2() < 0) { line.setX2(0); line.switchX2Dir(); } // end if else if (line.getX2() > this.getWidth()) { line.setX2(this.getWidth()); line.switchX2Dir(); } // end else if if (line.getY2() < 0) { line.setY2(0); line.switchY2Dir(); } // end if else if (line.getY2() > this.getHeight()) { line.setY2(this.getHeight()); line.switchY2Dir(); } // end else if } repaint(); try { Thread.sleep(delay); // have the thread sleep for "delay" milliseconds } // end try catch (InterruptedException e) { System.exit(-1); } // end catch } // end run method
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; line.setLine(); g2.draw(line.getLine()); } // end paint method