Hi,
I am making a game I added a mouselistener in my game class but when i add a mouse listener in my class to make a custom button it doesn't work, so i had to make a variables mousePressed and variables for the mouse location mX and mY but this is a hacky method. How would i add a mouselistener to my button class? Here is my project

Button.java

package com.ymail.sanchixx.Platform;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Button extends JPanel
{
    int xPos;
    int yPos;
    int width;
    int height;
    int textSize;
    String text;
    Graphics g;

    public Button(int m_xPos, int m_yPos, int m_height, int m_width, int m_textSize, String m_text, Graphics m_g)
    {
        super();
        xPos = m_xPos;
        yPos = m_yPos;
        text = m_text;
        Graphics g = m_g;
        width = m_width;
        height = m_height;
        textSize = m_textSize;

        g.drawRect(xPos, yPos, width, height);
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(xPos, yPos, width, height);
        g.setColor(Color.BLACK);
        g.setFont(new Font("Droid Sans Mono", Font.PLAIN, textSize));
        g.drawString(text, xPos+10, yPos+ (height - textSize/2));
        if(buttonPressed())
        {
            g.setColor(Color.BLACK);
            g.setFont(new Font("Droid Sans Mono", Font.ITALIC, textSize));
            g.drawString(text, xPos+10, yPos+ (height - textSize/2));
        }
    }

    boolean buttonPressed()
    {
        if((Game.mX >= xPos) && (Game.mX <= xPos+width) && (Game.mY >= yPos) && (Game.mY <= yPos+height))
        {
            return true;
        }
        return false;
    }
}

Parts of Game.java

    @Override
    public void mousePressed(MouseEvent me) 
    {
        mousePressed = true;
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        mousePressed = false;
        repaint();
    }

Thanks in advance this problem is really frustrating me!

Solved ^^ i love daniweb

Member Avatar for iamthwee

Thanks glad you solved it, I guess the chat is useful.

But like SOS said best to post the solution or a link to it so others might find it helpful in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.