gpt4 book ai didi

java - 用Java制作轮盘赌风格的Poly Wheel

转载 作者:行者123 更新时间:2023-11-30 07:01:38 26 4
gpt4 key购买 nike

我正在做一个项目,但我已经停滞不前了。目前,我正在创建一个会旋转的轮盘式轮盘,我遇到的问题是为轮盘创建 n' 个多边形。我希望最后能够设置 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

我得到了什么:

enter image description here

我想要得到的结果:

enter image description here

最佳答案

解决此问题的一种方法是使用 Arc2D 绘制饼图切片。此 Shape 的边界矩形是包围从中切割出此 Arc 切片的整个圆的矩形。然后,您可以在 for 循环中构建多色饼图切片,并将切片绘制到 BufferedImage 上,然后在 JPanel 的 paintComponent 方法中显示。可以通过在绘制它的 Graphics2D 对象上使用 AffineTransform 来旋转该图像,但是您必须小心,因为您不想转换 JVM 作为 paintComponent 方法的参数提供给您的 Graphics 对象。因此,您需要复制 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();
}
}

这显示为:

enter image description here

关于java - 用Java制作轮盘赌风格的Poly Wheel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29730918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com