Java2D:如何使用GeneralPath绘制圆的切片

时间:2018-08-31 03:59:41

标签: java java-2d

我想创建一个命运之轮进行练习。 我使用GeneralPath创建了车轮切片,但是在查找应该具有理想圆的beizer点时遇到了问题。看看屏幕截图,您会看到问题。

enter image description here

我不知道应该使用什么公式来获取正确的值,现在我基本上只是在猜测:

        path.moveTo(x, y); // the center

        this.firstPointX = x + wSize * Math.cos(angle*i);
        this.firstPointY = y + wSize * Math.sin(angle*i);

        path.lineTo(this.firstPointX, this.firstPointY);

        this.secondPointX = x + wSize * Math.cos(angle*(i+1));
        this.secondPointY = y + wSize * Math.sin(angle*(i+1));

        path.moveTo(x, y); // back to the center
        path.lineTo(this.secondPointX, this.secondPointY);

        this.beizerX = x + (THIS IS THE VALUE I NEED) * Math.cos((angle*i+angle/2));
        this.beizerY = y + (THIS IS THE VALUE I NEED) * Math.sin((angle*i+angle/2));

        path.curveTo(this.secondPointX, this.secondPointY, this.beizerX, this.beizerY, this.firstPointX, this.firstPointY);

        path.closePath();

1 个答案:

答案 0 :(得分:2)

为什么不只使用Arc2D还是简单地画一个圆和6条线?那么基本的问题是如何确定一个圆上的两个点(起点和终点,相距180度)

也许像...

Spin me

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private GeneralPath gp = new GeneralPath();

        public TestPane() {

            //GeneralPath gp = new GeneralPath();
            gp.append(new Ellipse2D.Double(0, 0, 200, 200), true);

            for (double angle = 0; angle < 180; angle += 30) {
                Point2D startPoint = pointOnCircle(angle, 100);
                Point2D endPoint = pointOnCircle(angle + 180, 100);

                gp.moveTo(startPoint.getX(), startPoint.getY());
                gp.lineTo(endPoint.getX(), endPoint.getY());
            }

        }

        protected Point2D pointOnCircle(double degrees, double radius) {
            double origin = radius;
            double rads = Math.toRadians(degrees);
            double x = origin + (Math.cos(rads) * radius);
            double y = origin + (Math.sin(rads) * radius);

            return new Point2D.Double(x, y);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHints(hints);
            g2d.translate(50, 50);
            g2d.draw(gp);
            g2d.dispose();
        }
    }

}

Arc2D

因为您知道,您可以

Spin me some more

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private GeneralPath gp = new GeneralPath();

        public TestPane() {
            gp.append(new Ellipse2D.Double(0, 0, 200, 200), true);

            for (double angle = 0; angle < 360; angle += 30) {
                Arc2D arc = new Arc2D.Double(0, 0, 200, 200,
                        angle,
                        30,
                        Arc2D.PIE);
                gp.append(arc, false);
            }

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHints(hints);
            g2d.translate(50, 50);
            g2d.draw(gp);
            g2d.dispose();
        }
    }

}
  

我尝试使用拱门,但无法旋转它们。

像Pi一样容易

Easy as Pi

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private GeneralPath gp = new GeneralPath();
        private double spinValue = 0;

        public TestPane() {
            gp.append(new Ellipse2D.Double(0, 0, 200, 200), true);

            for (double angle = 0; angle < 360; angle += 30) {
                Arc2D arc = new Arc2D.Double(0, 0, 200, 200,
                        angle,
                        30,
                        Arc2D.PIE);
                gp.append(arc, false);
            }

            Timer timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    spinValue += 0.01;
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHints(hints);

            Rectangle2D bounds = gp.getBounds2D();
            double x = (getWidth() - bounds.getWidth()) / 2d;
            double y = (getHeight() - bounds.getHeight()) / 2d;

            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            at.rotate(spinValue, bounds.getCenterX(), bounds.getCenterY());

            g2d.transform(at);
            g2d.draw(gp);
            g2d.dispose();
        }
    }

}
  

我还想用渐变填充单个切片,并通过旋转等独立地设置动画

然后,您将不能使用单个Path,因为它被视为单个图形元素,因此您将需要使用单个切片,因此我强烈建议使用Arc2D,因为它很简单维护它们的列表,并根据需要简单地转换Graphics上下文