I'm trying to make it so a line of text is drawn on the point of the cursor that states if the cursor is inside or outside of the drawn circle. I'm not entirely sure how to do that and I don't think that the global X and Y coordinates I've made are working properly.
Does anyone know of a way to some how trigger the string to be drawn when the cursor is in or out of the circle? I'm still sort of new to Java.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseCircleLocation extends JPanel {
boolean insideCircleStatus;
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
public MouseCircleLocation() {
//the listener that checks if the mouse if moving.
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
//gets the location of the mouse.
int x = e.getX();
int y = e.getY();
//simple check to see if the mouse location is inside the circle area.
if ( (Math.pow((x - 100), 2)) + (Math.pow((y - 60), 2)) < (Math.pow((50), 2))){
insideCircleStatus = true;
}
else{
insideCircleStatus = false;
}
}
});
}
//basic frame setup.
public static void main(String[] args) {
JFrame frame = new JFrame("Mouse Location");
frame.add(new MouseCircleLocation());
frame.setSize(210, 190);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
//draws the circle.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (insideCircleStatus = true){
g.drawString("Point is inside the circle", x, y);
System.out.println("Point is inside the circle");
}
if (insideCircleStatus = false){
g.drawString("Point is outside the circle", x, y);
System.out.println("Point is outside the circle");
}
g.setColor(Color.BLACK);
g.drawOval(100 - 50, 60 - 50, 50 *2, 50 * 2);
}
}