I followed a tutorial on creating a Maze Game in java. After completing it, I decided to add a few things (Main menu, more levels, etc). I am currently stuck at the Main Menu because I cannot get mouse input to work. I am trying to switch to screen = 1 when I left click in the start game box. This is my current code for the class.
package Maze;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener{
private Timer timer;
private Map m;
private Player p;
private int screen = 0;
private String Message = "";
private Font font = new Font("Serif", Font.BOLD, 48);
private Font start = new Font("Serif", Font.BOLD, 36);
private Rectangle start2 = new Rectangle(135,350,180,55);
public Board(){
m = new Map();
p = new Player();
addKeyListener(new Al());
addMouseListener(new A2());
setFocusable(true);
timer = new Timer(25,this);
timer.start();
}
public void actionPerformed(ActionEvent e){
if(m.getMap(p.getTileX(),p.getTileY()).equals("f")){
Message = "Winner";
screen = 10;
}
repaint();
}
public void paint(Graphics g){
super.paint(g);
if(screen==0){
g.setColor(Color.BLACK);
g.fillRect(0, 0, 464, 485);
g.setColor(Color.RED);
g.setFont(font);
g.drawString("Maze Game", 100,50);
g.setFont(start);
g.setColor(Color.WHITE);
g.drawString("Created by: ", 135,90);
g.drawString("Chad Jordan", 125,130);
g.setColor(Color.PINK);
g.fillRect(start2.x, start2.y, start2.width, start2.height);
g.setColor(Color.WHITE);
g.drawString("Start Game", start2.x, start2.y+40);
}
if((screen==1)){
for(int y = 0; y<14;y++){
for(int x=0; x<14; x++){
if(m.getMap(x, y).equals("g")){
g.drawImage(m.getGrass(), x*32, y*32, null);
}
if(m.getMap(x, y).equals("w")){
g.drawImage(m.getWall(), x*32, y*32, null);
}
if(m.getMap(x,y).equals("f")){
g.drawImage(m.getFinish(), x*32, y*32, null);
}
}
}
g.drawImage(p.getPlayer(), p.getTileX()*32, p.getTileY()*32, null);
}
if(screen==10){
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(Message, 150,200);
}
}
public class A2 extends MouseAdapter{
public void MousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
System.out.println("2");
if((screen ==0) && start2.contains(mx,my)){
screen=1;
System.out.println("1");
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
public class Al extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keycode = e.getKeyCode();
if(( keycode == KeyEvent.VK_W) || (keycode == KeyEvent.VK_UP)){
if(!m.getMap(p.getTileX(),p.getTileY() -1).equals("w")){
p.move(0, -1);
}
}
if(( keycode == KeyEvent.VK_A) || (keycode == KeyEvent.VK_LEFT)){
if(!m.getMap(p.getTileX()-1,p.getTileY()).equals("w")){
p.move(-1, 0);
}
}
if(( keycode == KeyEvent.VK_D) || (keycode == KeyEvent.VK_RIGHT)){
if(!m.getMap(p.getTileX()+1,p.getTileY()).equals("w")){
p.move(1, 0);
}
}
if(( keycode == KeyEvent.VK_S) || (keycode == KeyEvent.VK_DOWN)){
if(!m.getMap(p.getTileX(),p.getTileY() +1).equals("w")){
p.move(0, 1);
}
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
}
I used "System.out.println("1");" to see if it registers a mouse press but it does not print.
Your issue is capitalization from what I see
you have implemented MousePressed with a capital M
public void MousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
System.out.println("2");
if((screen ==0) && start2.contains(mx,my)){
screen=1;
System.out.println("1");
}
}
and also with a lowercase m:
public void mousePressed(MouseEvent e) {
}
simply change the capital M to lowercase m and remove the redundant mousePressed call
e.g.
public class A2 extends MouseAdapter{
public void mousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
System.out.println("2");
if((screen ==0) && start2.contains(mx,my)){
screen=1;
System.out.println("1");
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}