Java 如何在不小于或大于圆周的情况下绘制圆的半径
我正在做一个程序,我想画一条从圆心到参数点的线。但它在周长和内周长之间画了一条线。我想根据Java 如何在不小于或大于圆周的情况下绘制圆的半径,java,swing,geometry,trigonometry,custom-painting,Java,Swing,Geometry,Trigonometry,Custom Painting,我正在做一个程序,我想画一条从圆心到参数点的线。但它在周长和内周长之间画了一条线。我想根据X和Y角度准确地在圆周上画一条线 圆心点: x = 200 y = 100 radius= 100 您的代码(和逻辑)中有几个错误: 您使用两个角度来计算X和Y坐标,因此,会得到奇怪的结果。根据此图,机器人坐标的角度θ相同 您使用的是以度为单位的角度,但是Math.cos(angle)和Math.sin(angle)要求角度以弧度为单位,因此,必须将角度转换为弧度,就像Math.roRadians(a
X
和Y
角度准确地在圆周上画一条线
圆心点:
x = 200
y = 100
radius= 100
您的代码(和逻辑)中有几个错误:
X
和Y
坐标,因此,会得到奇怪的结果。根据此图,机器人坐标的角度θ相同
Math.cos(angle)
和Math.sin(angle)
要求角度以弧度为单位,因此,必须将角度转换为弧度,就像Math.roRadians(angle)
一样,如本问题所示:
。drawOval
正如您在绘制线条时所做的那样,将drawOval
更改为draw(新的Ellipse2D.Double(…)
paint(Graphics g)
方法,而不是paintComponent(Graphics g)
这可能会导致绘制时出现一些问题。使用JPanel
并覆盖其paintComponent
方法,并将这些JPanel
s添加到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度时输出的一些屏幕截图
您能提供一张图片(用油漆或其他材料制作)来展示您正在努力实现的目标,并展示您现在拥有的东西吗?对不起,伙计们。。。但是请帮帮我…谢谢你帮我…但是我不明白你为什么要声明这些变量。双线x=0;双线Y=0;双圆角x=0;双圆Y=0;这些是我正在做的一些测试,你可以安全地删除它们,我忘记了这么做。。。(我已经在上面的程序中进行了编辑@AshuAnsari)不客气。我很高兴能帮助^^^保持下去C:顺便说一句,我在你的程序中发现了另一个错误,让我在我的答案中提到它(见第4点)请告诉我。@AshuAnsari告诉你什么?我已经在我的帖子中添加了第4点,这是我看到的另一个错误。。。
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);
}
}
}