在java中绘制极坐标图

发布于 2024-11-24 00:49:46 字数 98 浏览 1 评论 0原文

有谁知道我如何开始在java中绘制极坐标图并在该图上绘制一些点?我的意思是圆和线,我希望用 swing 之类的东西来做到这一点,而不是使用像 Jfreechart 这样的任何库 谢谢

Does anyone know how I can get started to draw a polar graph in java and plot some points on this graph? I mean the circles and lines, I wish to do this with something like swing, and not use any library like Jfreechart
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘴硬脾气大 2024-12-01 00:49:49

Java2D 是官方 JDK 的一部分,非常适合您的目的。您可以在此处找到 Java 文档:Java2d

Java2D is part of the official JDK and fits your purposes perfectly. You can find the java doc here: Java2d

梦断已成空 2024-12-01 00:49:49

您将需要使用 Java2D 来绘制适合您需要的圆形/多边形。在您想要绘制的控件的 public void Paint(Graphics g) 方法中,您可以绘制到 Graphics 对象。一些可能有用的示例:

//Draw a polygon
public void paint(Graphics g) 
{
    int xVals[] = {25, 145, 25, 145, 25};
    int yVals[] = {25, 25, 145, 145, 25};

    g.drawPolygon(xVals, yVals, xVals.length);
}

//Draw an ellipse/circle
public void paint(Graphics g)
{
    int xPos = 50;
    int yPos = 50;
    int xWidth = 100;
    int yWidth = 100;
    g.drawOval(xPos, yPos, xWidth, yWidth);
}

请记住,drawOval、drawRect 等调用的位置是形状的左上角,而不是形状的中心。如果您希望椭圆形以 50 为中心且宽度为 100,则需要将 xPosyPos 设置为 0。

You'll want to use Java2D to draw circles/polygons that fit your needs. In the public void paint(Graphics g) method of the control you wish to draw on, you can draw to the Graphics object. Some examples of various things that might be helpful:

//Draw a polygon
public void paint(Graphics g) 
{
    int xVals[] = {25, 145, 25, 145, 25};
    int yVals[] = {25, 25, 145, 145, 25};

    g.drawPolygon(xVals, yVals, xVals.length);
}

//Draw an ellipse/circle
public void paint(Graphics g)
{
    int xPos = 50;
    int yPos = 50;
    int xWidth = 100;
    int yWidth = 100;
    g.drawOval(xPos, yPos, xWidth, yWidth);
}

Keep in mind that the position on calls like drawOval, drawRect, etc is for the top left corner of the shape, not the center of the shape. If you want your oval to be centered on 50 and with width 100, you'd need to set the xPos and yPos to 0.

倾`听者〃 2024-12-01 00:49:48

您可能想查看利萨如曲线;下面显示了 a = 5, b = 4 (5:4) 的示例。

附录:一旦您了解了如何在xy坐标中绘制点,那么您应该查看极坐标和笛卡尔坐标之间的转换

LissajousPanel

public class LissajousPanel extends JPanel {

    private static final int SIZE = 400;
    private GeneralPath path = new GeneralPath();

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        double dt = Math.PI / 180;
        int w = getWidth() / 2;
        int h = getHeight() / 2;
        path.reset();
        path.moveTo(w, h);
        for (double t = 0; t < 2 * Math.PI; t += dt) {
            double x = w * Math.sin(5 * t) + w;
            double y = h * Math.sin(4 * t) + h;
            path.lineTo(x, y);
        }
        g2d.setColor(Color.blue);
        g2d.draw(path);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new LissajousPanel());
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

You might like to look at Lissajous curves; an example of a = 5, b = 4 (5:4) is shown below.

Addendum: Once you see how to plot points in xy coordinates, then you should look at converting between polar and Cartesian coordinates.

LissajousPanel

public class LissajousPanel extends JPanel {

    private static final int SIZE = 400;
    private GeneralPath path = new GeneralPath();

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        double dt = Math.PI / 180;
        int w = getWidth() / 2;
        int h = getHeight() / 2;
        path.reset();
        path.moveTo(w, h);
        for (double t = 0; t < 2 * Math.PI; t += dt) {
            double x = w * Math.sin(5 * t) + w;
            double y = h * Math.sin(4 * t) + h;
            path.lineTo(x, y);
        }
        g2d.setColor(Color.blue);
        g2d.draw(path);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new LissajousPanel());
                f.pack();
                f.setVisible(true);
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文