Sign in to follow this  
Code Sage

Why won't this button work?

Recommended Posts

Can someone please tell me why my button won't "Click..."? It draws to my screen, but it won't do anything when I click it. I for the life of me can't figure out what is wrong.

package com.sage.rpg.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;

import com.sage.rpg.Game;

public class Button extends JButton implements MouseListener {

	private static final long serialVersionUID = 1L;
	
	private BufferedImage sprite;
	private int x, y;
	private int width, height;
	
	public Button(Game game, BufferedImage sprite, int x, int y, int width, int height) {
		
		this.addMouseListener(this);
		
		this.sprite = sprite;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		
		this.setPreferredSize(new Dimension(width, height));
		this.setMaximumSize(new Dimension(width, height));
		this.setMinimumSize(new Dimension(width, height));
		this.setLocation(x, y);
		
		game.getWindow().getFrame().add(this);
	}
	
	public void paintComponent(Graphics g) {
		
		super.paintComponent(g);
		
		g.drawImage(sprite, x, y, width, height, null);
	}

	public void mouseClicked(MouseEvent e) {
		
		System.out.println("Click...");
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
	
	}

}

Share this post


Link to post
Share on other sites

I'm not sure what you're trying to do here.  The button is working just fine, but you are drawing your buffered image over the top of the button, so none of the button press/release animation is shown (you're painting over it).  If you make the image smaller than the button you'll see what I mean.  If you're trying to make your own button you have to do all that stuff yourself.  

 

Take a look at the setIcon() method of the JButton.

 

http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setIcon(javax.swing.Icon)

Edited by Glass_Knife

Share this post


Link to post
Share on other sites

I'm not sure what you're trying to do here.  The button is working just fine, but you are drawing your buffered image over the top of the button, so none of the button press/release animation is shown (you're painting over it).  If you make the image smaller than the button you'll see what I mean.  If you're trying to make your own button you have to do all that stuff yourself.  

 

Take a look at the setIcon() method of the JButton.

 

http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setIcon(javax.swing.Icon)

I am simply trying to create a button with my own image as the button. The problem isn't the fact that the animations won't work, the problem is that when I click the button no action is performed. Are you saying all I have to do is create a new class extending JButton and override the setIcon(), setPressedIcon() etc. methods?

Share this post


Link to post
Share on other sites


I am simply trying to create a button with my own image as the button. The problem isn't the fact that the animations won't work, the problem is that when I click the button no action is performed. Are you saying all I have to do is create a new class extending JButton and override the setIcon(), setPressedIcon() etc. methods?

 

This isn't enough information.  Please add a simple class with a main() method that shows the problem.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this