Results 1 to 2 of 2
  1. #1
    timdeweert is offline Member
    Join Date
    Apr 2012
    Location
    Belgium
    Posts
    1
    Rep Power
    0

    Lightbulb Draw circle that can't go further than certain distance

    Hey, i'm kinda stuck with this exercice. We have to make an catapult and now i'm just trying to draw a circle and when i drag it
    there comes a line from the point it was to the point i drag it. As long as i'm dragging the line and the circle will repaint itself. But when
    i go at a certain distance (in my code further than 49) of the starting point it should not be painted but painted at the distance of 49.
    In my code this kinda works but when i go further than 49 and move my mouse the circle doens't move but i want it to move on the circle then
    with radius 49 and center the beginpoint. Could you help me?
    Thank you.

    ================================================== ==============
    Java Code:
    <script type="text/javascript">
    
    
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.event.MouseInputListener;
    
    
    public class CirclePanel extends JPanel implements MouseInputListener {
    
        // Radius of the circle
        private static final int R = 10;
        // Center of the circle
        private int circleX;
        private int circleY;
        private int outlineX;
        private int outlineY;
        // Is the circle moving?
        private boolean outlineShown;
        private int coX;
        private int coY;
        private int launchX;
        private int launchY;
    
        //calculate if the mouse is in the circle
        private boolean inCircle(int x, int y) {
            int dx = circleX - x;
            int dy = circleY - y;
            return dx * dx + dy * dy < R * R;
        }
    
        /**
         * Make a new panel of 300x150 pixels with circle
         */
        public CirclePanel() {
            super(null);
            setPreferredSize(new Dimension(300, 150));
            outlineShown = false;
            circleX = 150;
            circleY = 75;
            addMouseListener(this);
            addMouseMotionListener(this);
        }
    
        /**
         * Draws the red circle and when its moving a grey line
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (outlineShown) {
                g.setColor(Color.GRAY);
                g.drawLine(coX, coY, launchX, launchY);
                g.setColor(Color.RED);
                g.fillOval(launchX - R, launchY - R, 2 * R, 2 * R);
            } else {
                g.setColor(Color.RED);
                g.fillOval(circleX - R, circleY - R, 2 * R, 2 * R);
            }
    
        }
        // Relative coordinates of the mouse compared to the middle
        // of the circle.
        private int mouseX;
        private int mouseY;
    
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            coX = x;
            coY = y;
            if (inCircle(x, y)) {
                mouseX = circleX - x;
                mouseY = circleY - y;
                outlineX = circleX;
                outlineY = circleY;
                outlineShown = true;
                repaint(); 
            }
        }
    
        
        public void mouseReleased(MouseEvent e) {
            if (outlineShown) {
                int x = e.getX();
                int y = e.getY();
                if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
                    circleX = launchX;
                    circleY = launchY;
                }
                outlineShown = false;
                repaint();
                System.out.println(x + " | " + y);
            }
        }
    
        
        public void mouseDragged(MouseEvent e) {
            if (outlineShown) {
                outlineX = e.getX() + mouseX;
                outlineY = e.getY() + mouseY;
                if (Math.sqrt(Math.pow(outlineX - coX, 2) + Math.pow(outlineY - coY, 2)) <= 49) {
                    launchX = outlineX;
                    launchY = outlineY;
                    repaint();
                } else {
                    //it will probably be something like this but it doesn't work
                    /*float rico = Math.abs(((outlineY - coY) / (outlineX - coX)));
                    float afstand = (float) (Math.sqrt(Math.pow(outlineX - coX, 2) + Math.pow(outlineY - coY, 2)) - 49);
                    launchX = (int) (outlineX + (afstand / (Math.sqrt(1 + (rico * rico)))));
                    launchY = (int) ((rico * launchX) - (rico * outlineX) - outlineY);
                    repaint();*/
                    }
    
            } else {
                //mouseMoved(e);
            }
        }
    
        public void mouseMoved(MouseEvent e) {
        }
    
    
        public void mouseClicked(MouseEvent e) {
        }
    
    
        public void mouseEntered(MouseEvent e) {
        }
    
        
        public void mouseExited(MouseEvent e) {      
        }
    
        
        public static void main(String[] arg) {
            JFrame window = new JFrame("CirclePanel.main (c) 2006 KC");
            JPanel cp = new CirclePanel();
            cp.setBorder(BorderFactory.createTitledBorder("verplaats de rode cirkel"));
    
            window.setContentPane(cp);
            window.pack();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
        }
    }
    </script>
    Last edited by timdeweert; 04-07-2012 at 04:30 PM. Reason: Better layout

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Draw circle that can't go further than certain distance

    Don't double post. The other thread you started in Advanced Java has been removed.

    Go through the forum FAQs and discover how to post code so that it retains its formatting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. diving distance program
    By maximumammo in forum New To Java
    Replies: 4
    Last Post: 04-05-2012, 08:09 PM
  2. Replies: 4
    Last Post: 01-04-2012, 09:28 AM
  3. Distance Between Two Points
    By HSKrustofsky in forum New To Java
    Replies: 6
    Last Post: 09-08-2011, 06:23 AM
  4. Set distance from border to component
    By Josep_16 in forum AWT / Swing
    Replies: 1
    Last Post: 08-22-2011, 12:21 AM
  5. How can i draw a circle on an image uploaded ?
    By motress in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 04:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •