gpt4 book ai didi

java - 尝试划分颜色来绘制彩虹 donut

转载 作者:行者123 更新时间:2023-12-02 10:11:48 27 4
gpt4 key购买 nike

板级:

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;

public class Board extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

super.setBackground(Color.BLACK);
drawDonut(g, Color.WHITE);
}

private void drawDonut(Graphics g, Color c) {

Graphics2D g2d = (Graphics2D) g;

RenderingHints rh
= new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);

g2d.setRenderingHints(rh);

Dimension size = getSize();
double w = size.getWidth();
double h = size.getHeight();

Ellipse2D e = new Ellipse2D.Double(0, 0, 160, 260);
g2d.setStroke(new BasicStroke(3));

//RainbowColors rb = new RainbowColors(6);

for (double deg = 0; deg < 360; deg += 5) {
AffineTransform at
= AffineTransform.getTranslateInstance(w/2, h/2);
at.rotate(Math.toRadians(deg));

g2d.setColor(Color.RED/*rb.getNewColor()*/);
g2d.draw(at.createTransformedShape(e));
}
}
}

当我启动这个程序时,我的屏幕上首先出现白色圆​​圈,其余为紫色。我做了一些调试,并在控制台程序中从“RainbowColors”类的构造函数写入了两次消息

但是该对象只生成一次。

最佳答案

给定正确的颜色生成逻辑(RainbowColors 类),Board 类就可以正常工作:

class Board extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
super.setBackground(Color.BLACK);
drawDonut(g, Color.WHITE);
}

private void drawDonut(Graphics g, Color c) {

Graphics2D g2d = (Graphics2D) g;

RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);

Dimension size = getSize();
double w = size.getWidth();
double h = size.getHeight();

Ellipse2D e = new Ellipse2D.Double(0, 0, 160, 260);
g2d.setStroke(new BasicStroke(3));

double range = 360, step = 5;
RainbowColors rb = new RainbowColors((int)(range/step));

for (double deg = 0; deg < range; deg += step) {
AffineTransform at = AffineTransform.getTranslateInstance(w/2, h/2);
at.rotate(Math.toRadians(deg));
g2d.setColor(rb.getNewColor());
g2d.draw(at.createTransformedShape(e));
}
}
}

class RainbowColors {

private static final int DEFAULT_NUMBER_OF_COLORS = 100;
private final int numberOfColors;
private int index;
private Color[] colors;

RainbowColors() {
this(DEFAULT_NUMBER_OF_COLORS);
}

RainbowColors(int numberOfColors) {
this.numberOfColors = numberOfColors > 0 ? numberOfColors : DEFAULT_NUMBER_OF_COLORS;
makeColors();
}

//based on https://stackoverflow.com/a/22973823/3992939
private void makeColors() {

int range = numberOfColors / 6; //6 for loops, each changes R G or B
int step = 255/range;

List<Color> colorsList = new ArrayList<>();
for (int r=0; r< range; r++) {
colorsList.add(new Color(r*step, 255, 0));
}
for (int g=range; g>0; g--) {
colorsList.add(new Color( 255, g*step, 0));
}
for (int b=0; b<range; b++) {
colorsList.add(new Color( 255, 0, b*step));
}
for (int r=range; r>0; r--) {
colorsList.add(new Color(r*step, 0, 255));
}
for (int g=0; g<range; g++) {
colorsList.add(new Color( 0, g*step, 255));
}
for (int b=range; b>0; b--) {
colorsList.add(new Color( 0, 255, b*step));
}

colors = colorsList.toArray(new Color[colorsList.size()]);
}

Color getNewColor() {
index = index >= colors.length ? 0 : index;
return colors[index++];
}
}

enter image description here

可以从 here 复制 mcve .

关于java - 尝试划分颜色来绘制彩虹 donut ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54952512/

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