Reputation: 41
So I've made custom buttons (yes, there is a reason I'm not using JButtons) and for some reason they're not working. I believe it's to do with the MouseAdapter I'm using, but I can't say for certain. To clarify, I've created the visual aspect of the buttons, and that works, but for some reason the clicking doesn't. I've put in debug code, as you can see, but it's not printing that either. Here's my code:
JPanel:
package com.kraken.towerdefense.graphics;
import com.kraken.towerdefense.TowerDefense;
import com.kraken.towerdefense.scene.Scene;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
public class Screen extends JPanel implements Runnable {
Thread thread = new Thread(this);
private int FPS = 0;
public Scene scene;
TowerDefense tD;
private boolean running = false;
public RoundRectangle2D.Float playGame, quitGame;
public boolean playGameHighlighted, quitGameHighlighted;
@Override
public void run() {
long lastFrame = System.currentTimeMillis();
int frames = 0;
running = true;
while (running) {
repaint();
frames++;
if (System.currentTimeMillis() - 1000 >= lastFrame) {
FPS = frames;
frames = 0;
lastFrame = System.currentTimeMillis();
}
}
System.exit(0);
}
public Screen(TowerDefense tD) {
thread.start();
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
playGameHighlighted = playGame.contains(e.getPoint());
quitGameHighlighted = quitGame.contains(e.getPoint());
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
if (playGameHighlighted) {
scene = Scene.GAME;
repaint();
System.out.println("playGameHighlighted and mouse clicked");
}
if (quitGameHighlighted) {
running = false;
System.out.println("quitGameHighlighted and mouse clicked");
}
System.out.println("mouse clicked");
}
});
this.tD = tD;
scene = Scene.MENU;
setBackground(new Color(217, 217, 217));
}
@Override
public void paintComponent(Graphics g2) {
super.paintComponent(g2);
playGame = new RoundRectangle2D.Float((getWidth() / 2) - 200, (getHeight() / 2) - 100, 400, 100, 10, 10);
quitGame = new RoundRectangle2D.Float((getWidth() / 2) - 200, (getHeight() / 2) + 20, 400, 100, 10, 10);
Graphics2D g = (Graphics2D) g2.create();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.clearRect(0, 0, getWidth(), getHeight());
g.drawString("FPS: " + FPS, 10, 10);
if (scene == Scene.MENU) {
if (playGameHighlighted) {
g.setColor(new Color(255, 152, 56));
} else {
g.setColor(new Color(4, 47, 61));
}
g.fill(playGame);
if (quitGameHighlighted) {
g.setColor(new Color(255, 152, 56));
} else {
g.setColor(new Color(4, 47, 61));
}
g.fill(quitGame);
g.setColor(Color.WHITE);
g.setFont(new Font("Gisha", Font.PLAIN, 20));
g.drawString("Play", (getWidth() / 2) - (g.getFontMetrics().stringWidth("Play") / 2), (getHeight() / 2) - 45);
g.drawString("Quit", (getWidth() / 2) - (g.getFontMetrics().stringWidth("Quit") / 2), (getHeight() / 2) + 75);
g.setColor(Color.BLACK);
g.setFont(new Font("Gisha", Font.PLAIN, 30));
g.drawString("Tower Defense Menu", (getWidth() / 2) - (g.getFontMetrics().stringWidth("Tower Defense Menu") / 2), (getHeight() / 4) - 15);
g.draw(playGame);
g.draw(quitGame);
}
}
}
JFrame:
package com.kraken.towerdefense;
import com.kraken.towerdefense.graphics.Screen;
import javax.swing.*;
public class TowerDefense extends JFrame {
public static void main(String[] args) {
new TowerDefense();
}
public TowerDefense() {
setExtendedState(MAXIMIZED_BOTH);
setUndecorated(true);
setTitle("Tower Defense");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
Screen screen = new Screen(this);
this.add(screen);
setVisible(true);
}
}
Scene Enum:
package com.kraken.towerdefense.scene;
public enum Scene {
MENU,
GAME
}
So that's my code, any help would be greatly appreciated. Thanks!
Upvotes: 0
Views: 46
Reputation: 347244
MouseMotionListener
will only monitor a certain set of mouse events, in order to be notified about mouse click events, you need to use a MouseListener
.
Luckily for you, it's really easy to use the MouseAdapter
for both...
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
playGameHighlighted = playGame.contains(e.getPoint());
quitGameHighlighted = quitGame.contains(e.getPoint());
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
if (playGameHighlighted) {
scene = Scene.GAME;
repaint();
System.out.println("playGameHighlighted and mouse clicked");
}
if (quitGameHighlighted) {
running = false;
System.out.println("quitGameHighlighted and mouse clicked");
}
System.out.println("mouse clicked");
}
};
addMouseMotionListener(ma);
addMouseListener(ma);
Take a closer look at How to Write a Mouse Listener for more details
Upvotes: 2