Creating a Button.

http://pastebin.java-gaming.org/1b083489a4710

Above is the working code (apart from the parts I can’t test) , for the creation of a button that is drawn using (g.drawImage(but.getSprite(), but.getX(), but.getY(), null):wink: in a different class.

The issue I’m having is setting an area for MouseListener to activate. To what I have read, for MouseListener to work, I need to have a JComponent attached to my Button class. Do I need to put something like a JLabel onto my class to give my button mouse input or is there a better way?

[quote=“CoffeeChemist,post:1,topic:57877”]
It’s actually the reverse of what you are proposing, you need to link your button as an event listener (of any kind) to a JComponent for it to respond to any input. Just use the component you’re drawing the button on as a parameter and use the addMouseListener() method in the constructor. Looking something like this:

public class Button implements MouseListener {
	public Button(JComponent component, all your other parameters) {
		//stuff...
		component.addMouseListener(this); //Include this anywhere.
		//stuff...
	}
}

In the future, you might have better luck if you post an MCVE that includes a main method and all the code we need to copy and paste it to run ourselves.

If you’re going to use a Swing component like JLabel, why not just use a JButton instead of your own custom class?

How you do this depends on how you’re drawing your button. If you’re using a Swing component like JLabel or JButton to draw your button, then you’d just add the listener to that. JButton even has its own ActionListener that you can use instead of handling all the mouse events yourself.

But if you’re manually drawing the button yourself, then the MouseListener needs to be on the component doing the drawing. For example if you’re extending JPanel and overriding the paintComponent() function, then the MouseListener would go on that class.