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

1. Member
Join Date
Apr 2012
Location
Belgium
Posts
1
Rep Power
0

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 {

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

/**
* 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 05:30 PM. Reason: Better layout

2. 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