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