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.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Canny Edge Detection
    By tiny in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2012, 01:54 PM
  2. Replies: 1
    Last Post: January 3rd, 2012, 06:28 PM
  3. trying to draw a circle at the location of every mouse click
    By jopeters in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2011, 05:24 PM
  4. Sobel Operator (Edge Detection)
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 18th, 2011, 06:21 PM
  5. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM

Tags for this Thread