gpt4 book ai didi

Java Graphics2D 正在绘制一个像素(舍入错误?)

转载 作者:行者123 更新时间:2023-11-29 06:57:06 24 4
gpt4 key购买 nike

我一直在尝试使用 Graphics2D 和 PaintComponent 在 Java 中编写程序。然而,Java 没有正确地舍入某些值,导致一些点被渲染为与它应该在的位置不同的像素,这给出了不干净的图像。你可以在下图中看到我的意思。

enter image description here

这是我的代码。我可以改变什么来解决这个问题?谢谢!

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

g.setColor(Color.red);

int orginX = getWidth()/2;
int orginY = getHeight()/2;

for(int i=0; i<=360; i+= 10)
{
double angle = Math.toRadians(i);
double centerX = radius * Math.cos(angle) + orginX;
double centerY = radius * Math.sin(angle) + orginY;
int[] anglePointsX = {(int) (radius * Math.cos(angle+Math.toRadians(60)) + centerX), (int) (radius * Math.cos(angle-Math.toRadians(60)) + centerX), orginX};
int[] anglePointsY = {(int) (radius * Math.sin(angle+Math.toRadians(60)) + centerY), (int) (radius * Math.sin(angle-Math.toRadians(60)) + centerY), orginY};
g.drawPolygon(anglePointsX, anglePointsY, 3);
}

最佳答案

我建议你画两条单独的线:

  g.drawPolygon(anglePointsX, anglePointsY, 2); // the outter line.
g.drawLine(orginX, orginY, anglePointsX[0], anglePointsY[0]); // from origin to one outer point

几乎成功了,只有一行不符合预期。

那么我的下一个建议是再次绘制两条单独的线,但这次只是旋转从原点绘制的线。

super.paintComponent( g );

g.setColor(Color.red);
Graphics2D g2 = (Graphics2D)g.create();

int radius = 100;
int orginX = getWidth()/2;
int orginY = getHeight()/2;
double radians = Math.toRadians(60);

for(int i=0; i < 360; i+= 10)
{
double angle = Math.toRadians(i);
double centerX = radius * Math.cos(angle) + orginX;
double centerY = radius * Math.sin(angle) + orginY;

int[] anglePointsX = {(int) (radius * Math.cos(angle + radians) + centerX), (int) (radius * Math.cos(angle - radians) + centerX), orginX};
int[] anglePointsY = {(int) (radius * Math.sin(angle + radians) + centerY), (int) (radius * Math.sin(angle - radians) + centerY), orginY};

g.drawPolygon(anglePointsX, anglePointsY, 2);
// g2.drawLine(orginX, orginY, anglePointsX[0], anglePointsY[0]);

AffineTransform af = new AffineTransform();
af.translate(orginX, orginY);
af.rotate( angle );
g2.setTransform( af );
g2.drawLine(0, 0, 175, 0);
}

这种方法的问题是我不知道如何从原点计算线的长度,只是硬编码 175。也许你的数学比我的好,你知道如何计算线的固定长度.

确定最大行长度的蛮力方法可以是创建两个循环。第一个循环绘制外线并确定使用的最大 x 值。第二个循环然后使用此值作为长度绘制 36 条旋转线:

protected void paintComponent(Graphics g)
{
super.paintComponent( g );

g.setColor(Color.red);
Graphics2D g2 = (Graphics2D)g.create();

int radius = 100;
int orginX = getWidth()/2;
int orginY = getHeight()/2;
double radians60 = Math.toRadians(60);
int maxX = 0;

for(int i=0; i < 360; i+= 10)
{
double angle = Math.toRadians(i);
double centerX = radius * Math.cos(angle) + orginX;
double centerY = radius * Math.sin(angle) + orginY;

int[] anglePointsX = {(int) (radius * Math.cos(angle + radians60) + centerX), (int) (radius * Math.cos(angle - radians60) + centerX), orginX};
int[] anglePointsY = {(int) (radius * Math.sin(angle + radians60) + centerY), (int) (radius * Math.sin(angle - radians60) + centerY), orginY};

g.drawPolygon(anglePointsX, anglePointsY, 2);

maxX = Math.max(maxX, anglePointsX[0]);
}

for(int i=0; i < 360; i+= 10)
{
double angle = Math.toRadians(i);
AffineTransform af = new AffineTransform();
af.translate(orginX, orginY);
af.rotate( angle );
g2.setTransform( af );
g2.drawLine(0, 0, maxX - orginX, 0);
}

g2.dispose();
}

关于Java Graphics2D 正在绘制一个像素(舍入错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32532038/

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