I want to add in a feature so that when the mouse goes over a button then it will add a drop shadow. Currently I'm just trying to get the mechanics to work. I have my game loop calling a update method which I know works but here it is anyway
public void updateManager(double delta){
mhandler.updateCoordinates();
if(mhandler.getX() >= 144 && mhandler.getX() <= 444 && mhandler.getY() >= 784 && mhandler.getY() <= 980){
oversp = true;
}else{
oversp = false;
}
}
mhandler is what I named my MouseHandler class. then i have my render method
public void render(){
repaint();
}
and then my paint method
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
if(oversp){
System.out.println("Is over button");
g2d.setColor(Color.RED);
g2d.fillRect(144, 784, 300, 169);
}else{
System.out.println("Not over button");
g2d.setColor(Color.BLACK);
g2d.fillRect(144, 784, 300, 169);
}
}
When ever i run the program it only prints out not over button twice even when i am constantly calling render() in my game loop. I really do not know why it is not repainting. any help is very appriciated!
This is how I detect my coordinates of mouse
private int x,y;
public MouseHandler(){
x = 0;
y = 0;
}
public void updateCoordinates(){
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
x = (int) b.getX();
y = (int) b.getY();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
Game loop code
public static void MenuLoop() {
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastFpsTime = 0;
int fps = 0;
while (isrunning) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
fps++;
if (lastFpsTime >= 1000000000) {
System.out.println("(FPS: " + fps + ")");
lastFpsTime = 0;
fps = 0;
}
menu.render();
menu.updateManager(delta);
try {
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
} catch (Exception e) {
}
}
}public static void MenuLoop() {
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastFpsTime = 0;
int fps = 0;
while (isrunning) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
fps++;
if (lastFpsTime >= 1000000000) {
System.out.println("(FPS: " + fps + ")");
lastFpsTime = 0;
fps = 0;
}
menu.render();
menu.updateManager(delta);
try {
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
} catch (Exception e) {
}
}
}