Hello, does anyone know how i can draw a small handle (square) on the edge of the circle at each quadrant( for e.g at the north,east,west and south quadrants) I used general path to draw the circle, now I'm having difficulties to draw the handles. Here is my code:

//class1


import javax.swing.*;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;




public class RoundShape {


    Point[] points;
    GeneralPath circle;
    final int INC = 5;

    public RoundShape(){
        initPoints();
        initCircle();
    }



    private void initPoints()
    {
        int numberOfPoints = 360/INC;
        points = new Point[numberOfPoints];
        double x = 175.0;
        double y = 175.0;
        double r = 50.0;
        int count = 0;
        for(int theta = 0; theta < 360; theta+=INC)
        {
            int xpt = (int)(x + r * Math.cos(Math.toRadians(theta)));
            int ypt = (int)(y + r * Math.sin(Math.toRadians(theta)));
            points[count++] = new Point(xpt, ypt);
        }
    }

    private void initCircle()
    {
        circle = new GeneralPath();
        for(int j = 0; j < points.length; j++)
        {
            if(j == 0)
                circle.moveTo(points[j].x, points[j].y);
            else
                circle.lineTo(points[j].x, points[j].y);
        }
        circle.closePath();
    }

    public void paintRound(Graphics2D g2d){

        Shape round=getroundShape();
        g2d.draw(round);

    }



     private Shape getSPointRectangle(int x, int y) {
            return new Rectangle2D.Double(x - 3, y - 3, 6, 6);
        }





    public Shape getroundShape(){
        Shape s=circle;
        return s;

    }





}

//class2

import javax.swing.*;
import java.awt.*;



public class MainRound extends JPanel {

    RoundShape rs=new RoundShape();

    public MainRound(){


    }

    public void paintComponent(Graphics g){

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setPaint(Color.black);
        g2d.setStroke(new BasicStroke(15));

        rs.paintRound(g2d);

    }


    public static void main(String args[]){

        MainRound mr=new MainRound();
        JFrame frame=new JFrame();
        frame.setContentPane(mr);
        frame.setVisible(true);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Dani AI

Generated

A straightforward way is to compute each handle from the circle center and radius instead of trying to pick points out of the GeneralPath. Remember Swing’s coordinate system: angle 0 = east (right), 90 = south (down), 180 = west (left), 270 = north (up). So for north/east/west/south use angles 270, 0, 180, 90 respectively. Keep the circle center/radius as fields so the paint method and hit-testing can use the same values.

Here is a compact helper and usage you can drop into your paint code (adjust size and r as needed):

private Rectangle2D.Double handleRect(double cx, double cy,
                                     double r, double angleDeg, double size) {
    double rad = Math.toRadians(angleDeg);
    double hx = cx + r * Math.cos(rad);
    double hy = cy + r * Math.sin(rad);
    double s = size;
    return new Rectangle2D.Double(hx - s/2, hy - s/2, s, s);
}

// usage inside paintComponent:
double[] angles = {270, 0, 180, 90}; // north, east, west, south
double size = 6;
double stroke = (g2d.getStroke() instanceof BasicStroke)
                ? ((BasicStroke) g2d.getStroke()).getLineWidth() : 1.0;
double drawRadius = r + stroke/2 + size/2; // move handles outside thick stroke if needed
for (double a : angles) {
    Rectangle2D.Double h = handleRect(cx, cy, drawRadius, a, size);
    g2d.fill(h);
}

Notes and troubleshooting

  • If you keep generating your circle from sampled points, those integer roundings can make handles look slightly off; computing handles from the exact center/radius is more precise.
  • If the circle is drawn with a thick stroke, adjust the radius by half the line width so handles sit visually on the circle edge.
  • For interactive dragging store the Rectangle2D objects in a list and use contains(...) in mouse events, then update the radius/angle or center and repaint.

This follows ’s advice to generalize point creation and gives concrete angles and a ready-to-use helper for .

You could generalize the point generation you have in initPoints to a method that takes an angle and returns the point at that angle; it would look something like Point CreatePoint(double theta), and would make use of the code inside your for(int theta... loop. Then get points for 45, 235, 225, and 315 degrees and draw little rectangles centered on those points.

Does that sounds like what you're looking for?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.