gpt4 book ai didi

java - 用 "trail"绘制一个平滑的圆

转载 作者:行者123 更新时间:2023-12-03 02:11:59 26 4
gpt4 key购买 nike

我可以使用 Graphics2D 制作一个能够自行构建且没有“Swing ”效果的平滑圆圈吗?如果是,怎么办?要理解我的意思,您必须运行以下示例:

import java.awt.Color;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.*;

public class CircleExample {

static int angle = 0;

public static void main(String[] args) {

JFrame frame = new JFrame();

new Thread(new Runnable() {
public void run() {
while (true) {

if (angle >= 360) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
angle = 0;
}

angle++;

try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}

frame.repaint();
}
}

}).start();

frame.getContentPane().add(new Circle());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);

}

static class Circle extends JPanel {

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D gg = (Graphics2D) g;

gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

gg.setColor(Color.BLACK);

gg.drawArc(40, 40, 100, 100, 0, angle);

}

}

}

最佳答案

Swing 效果是由不同弧线的抗锯齿决策略有不同引起的,可能是由于舍入误差所致。

解决此问题的一种方法是绘制一个完整的圆 - 始终以相同的抗锯齿方式渲染 - 但将其剪辑到给定的弧线。所以实际上只会绘制其中的一部分。

以下内容基于 Andrew Thompson 基于计时器的答案(已被删除),但我为您的原始线程添加了一些延迟,并将 angle 设为 Circle< 的字段 类而不是静态变量。

public static void main(String[] args) {
final JFrame frame = new JFrame();

frame.getContentPane().add(new Circle());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}

static class Circle extends JPanel {

private int angle = 0;
// The clip arc is going to be slightly larger than the actual
// arc, otherwise some antialiasing pixels will be clipped off.
private Arc2D clip = new Arc2D.Double(35,35,110,110,0,360,Arc2D.PIE);

Circle() {

ActionListener actionListener = new ActionListener() {

int waitCounter = 0;

@Override
public void actionPerformed(ActionEvent e) {
// When the angle reaches 360, set a wait for 100
// runs of the timer.
if (angle >= 360) {
angle = 0;
waitCounter = 100;
}
// If waiting, count down the time. When the wait
// counter is zero, do the actual work.
if (waitCounter > 0) {
waitCounter--;
} else {
angle++;
repaint();
}
}
};

// The timer itself runs every 10 milliseconds.
Timer timer = new Timer(10, actionListener);
timer.start();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gg = (Graphics2D) g;
gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// Set the clipping arc to the current angle.
clip.setAngleExtent(angle);
gg.setClip(clip);

// Draw a full circle. It will be clipped to the arc set above.
gg.setColor(Color.BLACK);
gg.drawOval(40,40,100,100);
}
}

关于java - 用 "trail"绘制一个平滑的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32286609/

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