I tried to override the paintComponent() method in JButton to give it a different look . But the program is not working , simple button is showing with the image I have provided as argument .Nothing is happening due to overrided paintComponent() method. Please help me understand where I am wrong.
I have a program where I have used:
CODE
myButton stop= new myButton("stop.png");
and the coding for this buttons is:
CODE
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.geom.RoundRectangle2D;
import java.awt.Color;
import java.awt.Image;
import java.awt.GradientPaint;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.*;
//import javax.swing.*;
@SuppressWarnings("serial")
class myButton extends JButton
{
public myButton(String txt)
{
super(new ImageIcon(txt));
setPreferredSize(new Dimension(50, 30));
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
GradientPaint p;
p = new GradientPaint(0, 0, Color.RED, 0, getHeight(),Color.BLUE);
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);
}
}