gpt4 book ai didi

java - 如何画圆的半径而不比周长短或大

转载 作者:行者123 更新时间:2023-11-30 02:15:41 25 4
gpt4 key购买 nike

我正在开发一个程序,我想在其中从圆的中心到其参数点绘制一条线。但它画的是圆周外和圆周内的线。我想根据 XY 角度在圆周上精确地画线。

圆心点:

x = 200
y = 100
radius= 100

[here i just want to draw a line for circle center to its circumference.

public SecondActivity(String azim,String ele) {
initialize();
setTitle("My WIndow");`

angle_x = Integer.parseInt(azim);
angle_y = Integer.parseInt(ele);

x = 200+r*Math.cos(angle_x);
y = 100+r*Math.sin(angle_y);
}

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

drawCircle(g,200,100,r);
drawLine(g);
}

public void drawCircle(Graphics g,int x,int y,int r) {
g.setColor(Color.BLACK);
g.drawOval(x, y, r*2,r*2);
}
public void drawLine(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
g2d.draw(new Line2D.Double(300.0d,200.0d,x,y));
}

最佳答案

您的代码(和逻辑)中有一些错误:

  1. 您使用 2 个角度来计算 XY 坐标,因此会产生奇怪的结果。根据这个Wikipedia图像角度 theta 与机器人坐标相同。

    enter image description here

  2. 您使用的是度数角度,但 Math.cos(angle)Math.sin(angle) 要求 角度 以弧度给出,因此,您必须将角度转换为弧度,如 Math.roRadians(angle),如本问题所示:How to use Math.cos() & Math.sin()?

  3. 这并不是一个真正的错误,而是 @MadProgrammer 在 this other answer of mine 中的建议。要使用 Shape API 而不是像绘制线条时那样使用纯 .drawOval,请将 drawOval 更改为 draw(new Ellipse2D.Double(...)) .

  4. 您正在重写 paint(Graphics g) 方法,而不是 paintComponent(Graphics g),这可能会在绘画时导致一些问题。使用 JPanels 并重写其 paintComponent 方法,并将这些 JPanel 添加到您的 JFrame 中。

    <

说了以上所有内容,我找到了一个遵循上述建议并解决问题的好例子:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RadiusDrawer {
private JFrame frame;
private int centerX = 50;
private int centerY = 50;
private int x = 0;
private int y = 0;
private int r = 100;

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

private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());

frame.add(new MyCircle());
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@SuppressWarnings("serial")
class MyCircle extends JPanel {
int cx = 0;
int cy = 0;
public MyCircle() {
int angle = 0;

x = (int) (r * Math.cos(Math.toRadians(angle)));
y = (int) (r * Math.sin(Math.toRadians(angle)));

y *= -1; //We must inverse the Y axis since in Math Y axis starts in the bottom while in Swing it starts at the top, in order to have or angles correctly displayed, we must inverse the axis

calculateCenter();
}

private void calculateCenter() {
cx = centerX + r;
cy = centerY + r;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;

drawCircle(g2d, centerX, centerY, r);
drawRadius(g2d);
}

private void drawCircle(Graphics2D g2d, int x, int y, int r) {
g2d.setColor(Color.BLACK);
g2d.draw(new Ellipse2D.Double(x, y, r * 2, r * 2));
}

private void drawRadius(Graphics2D g2d) {
g2d.setColor(Color.BLUE);
g2d.draw(new Line2D.Double(cx, cy, cx + x, cy + y));
}

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
}

以下是 0、60 和 90 度输出的一些屏幕截图。

enter image description here enter image description here enter image description here

关于java - 如何画圆的半径而不比周长短或大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48539169/

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