Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JButton gradient change gradientpaint on mouseEnter ?

Status
Not open for further replies.

GrizzLyCRO

Programmer
Sep 15, 2007
2
HR
I want button to animate when mouse is over it.
i will make animation myself but i need someone to explain wheri am wrong at changing background gradient paint


[tt]
package tryout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class ShadowButton extends JButton implements MouseListener {

public ShadowButton(String text) {
super(text);
setContentAreaFilled(false);
setFocusPainted(false);
setForeground(new Color(0xB0C4DE));
Font buttonFont = new Font("Monospaced", Font.BOLD, 20);
setFont(buttonFont);
Dimension d = new Dimension(150, 30);
setPreferredSize(d);
setMinimumSize(d);
setMaximumSize(d);
addMouseListener(this);
}

@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;

GradientPaint p;
p = new GradientPaint(0, 0, new Color(0x00009F), 0, getHeight(),
new Color(0x700000));

Paint oldPaint = g2.getPaint();
g2.setPaint(p);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setPaint(oldPaint);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

super.paintComponent(g);
}

public void mouseEntered(MouseEvent e) {
System.out.println("Mouse enters");
animateButton();
}

public void mouseExited(MouseEvent e) {
System.out.println("Mouse leaves");
}

public void mousePressed(MouseEvent e) {
System.out.println("Mouse press");
}

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

public void mouseReleased(MouseEvent e) {
System.out.println("Mouse release");
}

public void animateButton() {
System.out.println("Animation should begin now");
GradientPaint p;
p = new GradientPaint(0, 0, new Color(0xFFFFFF), 0, getHeight(),
new Color(0x777777));
Graphics g = getGraphics();
Graphics2D g2 = (Graphics2D) g;
Paint oldPaint = g2.getPaint();
g2.setPaint(p);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setPaint(oldPaint);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

repaint();

}

}
[/tt]
 
Maybe if you explain what's happening and where are you getting the error ...

Cheers,
Dian
 
GrizzlyCRO,

I like the concept. Would you mind posting your modified code so I can use it?

Thanks...
Steve
 
You implemented the MouseListener interface, so is it working? I saw you just added lines like this one:

System.out.println("Mouse leaves");

to it methods. Are you getting the output when the mouse leaves the button? If not, then its beacuse you need to add the mouse listener to the button. I wrote code that does something similar, it just changes the color of the background.

With this code you don't need to implement the MouseListener interface. This is what is called an anonymous class.

myButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
JButton boton = (JButton)e.getComponent(); boton.setBackground(Color.DARK_GRAY);
}
@Override
public void mouseExited(MouseEvent e) {
JButton boton = (JButton)e.getComponent();
boton.setBackground(Color.BLACK); }
});
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top