日期:2014-05-20 浏览次数:20805 次
public class ScrewPanel extends JPanel{ private Point p1; private Point p2; private double distance; private static final int OVAL_SIZE = 20; private static final Double ANGLE = Math.PI/60; private double rate; private double th = 0; private Timer timer; public ScrewPanel(Point p1, final Point p2, int times) { super(); this.p1 = p1; this.p2 = p2; this.distance = Math.sqrt(Math.pow(p1.x-p2.x, 2)+Math.pow(p1.y-p2.y, 2)); this.rate = distance/times; this.setPreferredSize(new Dimension(500, 600)); timer = new Timer((int) rate, new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { EventQueue.invokeLater(new Runnable(){ @Override public void run() { distance-=rate; if(distance<=0)return; th = th+ANGLE; p2.x = (int) (distance*Math.cos(th)); p2.y = (int) (distance*Math.sin(th)); repaint(); try { Thread.sleep(30); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }); timer.start(); } @Override public void paint(Graphics g) { super.paint(g); g.fillOval(p1.x, p1.y, OVAL_SIZE, OVAL_SIZE); g.translate(p1.x, p1.y); g.fillOval(p2.x, p2.y, OVAL_SIZE, OVAL_SIZE); } @Override public void finalize() { timer.stop(); timer = null; } /** * @param args */ public static void main(String[] args) { JFrame f = new JFrame(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new ScrewPanel(new Point(200, 200), new Point(50, 50), 1000)); f.pack(); } }
------解决方案--------------------
import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class ScrewPanel extends JPanel{ private Point p1; private Point p2; private double distance; private static final int OVAL_SIZE = 20; private static final Double ANGLE = Math.PI/60; private double rate; private double th = 0; private Timer timer; public ScrewPanel(Point p1, final Point p2, int times) { super(); this.p1 = p1; this.p2 = p2; this.distance = Math.sqrt(Math.pow(p1.x-p2.x, 2)+Math.pow(p1.y-p2.y, 2)); this.rate = distance/times; this.setPreferredSize(new Dimension(500, 600)); timer = new Timer((int) rate, new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { EventQueue.invokeLater(new Runnable(){ @Override public void run() { distance-=rate; if(distance<=0) { timer.stop(); timer = null; return; } th = th+ANGLE; p2.x = (int) (distance*Math.cos(th)); p2.y = (int) (distance*Math.sin(th)); repaint(); try { Thread.sleep(30); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }); timer.start(); } @Override public void paint(Graphics g) { super.paint(g); g.fillOval(p1.x, p1.y, OVAL_SIZE, OVAL_SIZE); g.translate(p1.x, p1.y); g.fillOval(p2.x, p2.y, OVAL_SIZE, OVAL_SIZE); } public static void main(String[] args) { JFrame f = new JFrame(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new ScrewPanel(new Point(200, 200), new Point(50, 50), 1000)); f.pack(); } }