0

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

jpo
  • 3,959
  • 20
  • 59
  • 102

3 Answers3

4

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);
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

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

sealz
  • 5,348
  • 5
  • 40
  • 70
Ricardo Ferreira
  • 766
  • 5
  • 15
-1

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.

TaylorP
  • 1,039
  • 2
  • 13
  • 28
  • -1, Never override a component's `paint(...)` method (unless you absolutely have to). Instead, override the component's `paintComponent(...)` method. – mre Jul 14 '11 at 15:22