GrizzLyCRO
Programmer
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]
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]