【问题标题】:netbeans: animate circle drawn using drawline funtionnetbeans:使用drawline函数绘制的动画圆
【发布时间】:2017-09-07 20:11:41
【问题描述】:

我正在尝试像这样画一个圆圈:http://35.197.37.158/Circle/

使用 drawline 函数并将其动画化,与附加的链接相同。

这是我尝试过的,但它画了一条圆线并删除了前一个

这是一个类,我的代码用于绘制一个圆圈并使用 swin Timer 对其进行动画处理。有人有更好的想法来为圆圈设置动画吗?

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

【问题讨论】:

    标签: java animation netbeans timer


    【解决方案1】:

    由于我无法让您的解决方案画出圆圈,因此我不得不稍微重写您的代码。这是我的解决方案,保留您的 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 2013-11-19
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      相关资源
      最近更新 更多