在Java中制作轮盘式Poly Wheel

问题描述 投票:2回答:2

我正在做一个项目,我已经停下来了。目前,我正在创建一个轮盘式轮子,它将会旋转,我遇到的问题是为轮子创建了一定数量的多边形。我想最终能够设置n'数字并使用for循环来创建多边形的点。我试图使用那个特定圆的参数方程,但是y值是不正确的,因为像素的处理方式不同。旋转与Rotateicon类一起正常工作。如此简单的问题,如何从给定的中心创建n'个多边形,请求x,y以轮子结束?

代码

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;




public class Wheel extends JPanel {
    private JLabel label;
    private Icon icon;
    private Icon rotated;
    private int degrees;
    private static Point center;

    public Wheel(Image image)
    {   
        setPreferredSize(new Dimension(250, 250));
        center = new Point(250/2, 250/2);

        icon = new ImageIcon( image );
        label = new JLabel(icon);
        label.setPreferredSize( label.getPreferredSize() );
        add( label );
        setDegrees( 0 );
    }

    public void setDegrees(int degrees)
    {
        this.degrees = degrees;
        double radians = Math.toRadians( degrees );
        rotated = new RotatedIcon(icon, degrees);
        label.setIcon(rotated);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                Image bi = RotatableImage.getImage(250);
                final Wheel r = new Wheel(bi);

                    final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
                    slider.addChangeListener(new ChangeListener()
                    {
                        public void stateChanged(ChangeEvent e)
                        {
                            int value = slider.getValue();
                            r.setDegrees( value );
                        }
                    });

                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new JScrollPane(r));
                f.add(slider, BorderLayout.SOUTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
    static class RotatableImage
    {
        private static final Random r = new Random();

        static public Image getImage(int size)
        {
            BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = bi.createGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
            g2d.setStroke(new BasicStroke(10.0f));

            Polygon flag = new Polygon();
            flag.addPoint(125, 125);
            flag.addPoint(205, 250/2);
            flag.addPoint(205, 250/2+10);



            g2d.draw(flag);

            g2d.setColor(Color.BLACK);
            g2d.fillOval(120, 120, 10, 10);

            g2d.dispose();
            return bi;
        }


    }


}

如果需要,旋转图标为:http://pastebin.com/Sbb38ifU

我得到了什么:

我想要最终得到的结果是:

java swing math polygons
2个回答
2
投票

解决此问题的一种方法是使用Arc2D绘制饼图切片。此Shape的边界矩形是一个矩形,用于包围切割此Arc切片的整个圆。然后,您可以在for循环中构造多色饼图切片,并将切片绘制到BufferedImage上,然后将其显示在JPanel的paintComponent方法中。可以使用绘制它的Graphics2D对象上的AffineTransform旋转此图像,但是您必须小心,因为您不希望将JVM提供给您的Graphics对象转换为paintComponent方法的参数。因此,您需要复制Graphics2D对象然后对其进行转换:

protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g.create(); // create a copy to rotate
  if (image != null) {
     if (af != null) {
        g2.transform(af);
     }
     g2.drawImage(image, 0, 0, null);
  }
  g2.dispose(); // OK to do this as we created this object
}

你永远不应该处理由JVM提供给你的Graphics对象,但由于我们的g2对象是一个副本,所以可以实际处理它,这样我们就不会耗尽资源。例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class FooArcs extends JPanel {
   public static final int PREF_W = 500;
   private PiePanel piePanel;
   private JSlider slider = new JSlider(0, 260, 0);

   public FooArcs(int imageWidth, int divisions) {
      slider.setMinorTickSpacing(5);
      slider.setMajorTickSpacing(20);
      slider.setPaintLabels(true);
      slider.setPaintTicks(true);

      piePanel = new PiePanel(imageWidth, divisions);

      slider.addChangeListener(new SliderListener());

      setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      setLayout(new BorderLayout(5, 5));
      add(piePanel, BorderLayout.CENTER);
      add(slider, BorderLayout.PAGE_END);
   }

   private class SliderListener implements ChangeListener {
      @Override
      public void stateChanged(ChangeEvent e) {
         int value = slider.getValue();
         piePanel.rotate(value);
      }
   }

   private static void createAndShowGui() {
      int imageWidth = PREF_W;
      int divisions = 24;
      FooArcs mainPanel = new FooArcs(imageWidth, divisions);

      JFrame frame = new JFrame("FooArcs");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class PiePanel extends JPanel {
   private BufferedImage image;
   private Random random = new Random();
   private AffineTransform af;
   private int imageWidth;

   public PiePanel(int imageWidth, int divisions) {
      this.imageWidth = imageWidth;
      image = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = image.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      for (int i = 0; i < divisions; i++) {
         drawPie(g2, i, imageWidth, divisions);
      }
      g2.dispose();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g.create(); // create a copy to rotate
      if (image != null) {
         if (af != null) {
            g2.transform(af);
         }
         g2.drawImage(image, 0, 0, null);
      }
      g2.dispose(); // OK to do this as we created this object
   }

   private void drawPie(Graphics2D g2, int i, int imageWidth2, int divisions) {
      Color c = getRandomColor();
      double x = 1.0;
      double y = x;
      double w = imageWidth2 - 2;
      double h = w;
      double start = i * 360.0 / divisions; // starting angle
      double extent = 360.0 / divisions; // size of slice in degrees
      int type = Arc2D.PIE;
      Arc2D arc = new Arc2D.Double(x, y, w, h, start, extent, type);

      g2.setColor(c);
      g2.fill(arc);
      g2.setColor(Color.black);
      g2.draw(arc);
   }

   private Color getRandomColor() {
      Integer a = random.nextInt(128) + 128;
      Integer b = random.nextInt(128) + (random.nextBoolean() ? 128 : 0);
      Integer c = random.nextInt(128);
      List<Integer> colors = Arrays.asList(new Integer[] {a, b, c});
      Collections.shuffle(colors);
      Color color = new Color(colors.get(0), colors.get(1), colors.get(2));

      return color;
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(imageWidth, imageWidth);
   }

   public void rotate(int degrees) {
      double theta = Math.toRadians(degrees);
      double anchorx = imageWidth / 2.0;
      double anchory = anchorx;
      af = AffineTransform.getRotateInstance(theta, anchorx, anchory);
      repaint();
   }
}

这显示为:


2
投票

您正在创建一个直角三角形。你想要做的是更接近一系列旋转的等腰三角形。

获得等腰的简单方法是将两个直角三角形粘在一起:

        flag.addPoint(125, 125);
//        flag.addPoint(205, 250/2);
        flag.addPoint(205, 250/2+10);
        flag.addPoint(205, 250/2-10);

这将需要更多调整。圆周应该是圆而不是多边形。仍然需要制作一系列这些并且现在有一个填充孔,因为您尝试使用线条粗细填充三角形。但现在至少角度是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.