Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: How to draw a small square(handles) on the edge of a circle at each quadrant?

1. ## How to draw a small square(handles) on the edge of a circle at each quadrant?

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);
}

}```

2. ## Re: How to draw a small square(handles) on the edge of a circle at each quadrant?

Can you compute the x,y values for each of the 4 locations where you want to draw?
Given those values and a little math you should be able to compute the correction to each of the x,y values to place the rectangles where you want them. Paper and pencil are useful for working out the details.

3. ## Re: How to draw a small square(handles) on the edge of a circle at each quadrant?

Hello, i tried the +/- x and +/- y directions. I modified the code so that i can draw the circle on mouse click and i use mouse dragged so that i can drag it to any locations. But i when i run the codes, it draws itself on the top at 0,0 and i'm not able to drag it to other part of the screen. Here is my code

```
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 p = 5;

double x ;
double y;

public RoundShape(double x, double y){
this.x=x;
this.y=y;
initPoints();
initCircle();
}

private void initPoints()
{
int numberOfPoints = 360/p;
points = new Point[numberOfPoints];

double r = 50.0;
int count = 0;
for(int theta = 0; theta < 360; theta+=p)
{
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.setColor(Color.black);
g2d.draw(round);
g2d.setColor(Color.gray);
g2d.draw(new Rectangle2D.Double(x, y-50, 4.0, 5.0));
g2d.draw(new Rectangle2D.Double(x, y+50, 4.0, 5.0));
g2d.draw(new Rectangle2D.Double(x-50, y, 4.0, 5.0));
g2d.draw(new Rectangle2D.Double(x+50, y, 4.0, 5.0));
//Shape s1=getSPointRectangle(175, 125, 4, 5);
//g2d.draw(s1);

}

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

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

}

}

//class2

import javax.swing.*;

//import MainDrawing.MouseDrag;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainRound extends JPanel {

double x,y;
int width,height;
MouseDrag mouseDrag;
RoundShape rs=new RoundShape(x,y );

public MainRound(){

mouseDrag = new MouseDrag();
addMouseListener(mouseDrag);
addMouseMotionListener(mouseDrag);
}

private final class MouseDrag extends MouseAdapter {
private boolean dragging = false;
private Point last;

@Override
public void mousePressed(MouseEvent m) {
last = m.getPoint();
// dragging = isInsideEllipse(last);
if (!dragging) {
x = last.x;
y = last.y;
width = 0;
height = 0;
}
//   repaint();
}

@Override
public void mouseReleased(MouseEvent m) {
//last = null;
dragging = true;
repaint();
}

@Override
public void mouseDragged(MouseEvent m) {
int dx = m.getX() - last.x;
int dy = m.getY() - last.y;
if (dragging) {
x += dx;
y += dy;
} else {
width += dx;
height += dy;
}
last = m.getPoint();
repaint();
}

}

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);
}

}```

4. ## Re: How to draw a small square(handles) on the edge of a circle at each quadrant?

Where does the code set the x,y values so that they are not == 0?