2017-09-05 78 views
0

我想画这样一个圆圈绘制圆:http://35.197.37.158/Circle/的NetBeans:动画使用的DrawLine funtion

使用DrawLine的功能和相同的动画作为附加的链接。

这里是我试过,但它划清界限圈,并删除之前的一个

这是有我绘制一个圆,利用欧亚定时器制作动画代码的类。任何人有更好的想法来动画圈?

import java.awt.Color; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.GeneralPath; 
import javax.swing.*; 

public class CustomPanel extends JPanel implements ActionListener{ 
     Point [] coordinates; 
     GeneralPath circle; 
     final int C = 10; 
     int j =0; 
     int i =j; 
     Point p; 
     Point p2 ; 
     Timer clock = new Timer(100, this); 

public CustomPanel() 
{ 
    LinesCoordinates(); 
    clock.setInitialDelay(50); 
    clock.start(); 
} 


private void LinesCoordinates() 
{   
    int numberOfLines = 360/C; 
    coordinates = new Point[numberOfLines]; 
    double cx = 200.0; 
    double cy = 200.0; 
    double r = 75.0; 
    int count = 0; 
    for(int theta = 0; theta < 360; theta+=C) 
    { 
     int x = (int)(cx + r * Math.cos(Math.toRadians(theta))); 
     int y = (int)(cy + r * Math.sin(Math.toRadians(theta))); 
     coordinates[count++] = new Point(x, y); 
    } 
} 


@Override 
public void actionPerformed(ActionEvent e) { 
    Redraw(); 
    repaint(); 
    } 

public void Redraw(){ 
    j=j+1; 
    p = p2; 
} 

@Override 
protected void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D)g; 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
         RenderingHints.VALUE_ANTIALIAS_ON); 
    g2.setPaint(Color.red); 
    // while (j<=coordinates.length){ 

    while(i<=j){ 
    j--; 
    p2 = coordinates[j % coordinates.length]; 
    g2.drawLine(p.x, p.y, p2.x , p2.y); 

    }}} 

这是我的主要

public static void main(String[] args) 
    { 
     // SwingUtilities.invokeLater(new Runnable() { 
      // public void run() { 
       JFrame frame = new JFrame("Circle "); 
       CustomPanel co = new CustomPanel(); 
       frame.add(co); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setSize(300, 300); 
       frame.setVisible(true); 
} 

回答

0

由于我没有能够得到您的解决方案画圆我不得不重写你的代码位。这里是我的解决方案,保留你的JPanel监听Timer方法。希望这会对你有用。如果需要,我可以发送完整的NetBeans项目。

package circleanimation; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class CustomPanel extends JPanel implements ActionListener { 

    final int numberOfPoints = 20; 
    Point[] circleCoordinates = new Point[numberOfPoints]; 
    int nextPointToAnimate = 1; 
    Timer clock = new Timer(100, this); 

    public CustomPanel() { 
     calculateCirclePoints(); 
     clock.setInitialDelay(50); 
     clock.start(); 
    } 

    private void calculateCirclePoints() { 
     int angle = 360/numberOfPoints; 
     double cx = 150.0; 
     double cy = 150.0; 
     double r = 75.0; 
     int count = 0; 
     for (int totalAngle = 0; totalAngle < 360; totalAngle = totalAngle + angle) { 
      int x = (int) (cx + r * Math.cos(Math.toRadians(totalAngle))); 
      int y = (int) (cy + r * Math.sin(Math.toRadians(totalAngle))); 
      circleCoordinates[count++] = new Point(x, y); 
     } 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     g.setColor(Color.red); 
     for (int i = 0; i < nextPointToAnimate; i++) { 
      Point firstPoint = circleCoordinates[i]; 
      Point secondPoint; 
      if (i == numberOfPoints - 1) { 
       secondPoint = circleCoordinates[0]; 
      } else { 
       secondPoint = circleCoordinates[i + 1]; 
      } 
      g.drawLine(firstPoint.x, firstPoint.y, secondPoint.x, secondPoint.y); 
     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     nextPointToAnimate++; 
     if (nextPointToAnimate == numberOfPoints) { 
      clock.stop(); 
     } 
     repaint(); 
    } 
}