3

I wanted to paint my own version of JButton, so I have overridden the paintComponent() method, and drew a gradient roundRect. This works, but after that, I want to draw the String of the Button over it, and at compile-time, I got no error messages. But at runtime, I only see the roundRect, gradient, just as I intended it to be (I can click on it too), but the String is invisible...

Here's my code:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class JIconButton extends JButton implements MouseListener
{
    private boolean mouseInside;
    public JIconButton(String file, String text)
    {
        super(text, new ImageIcon(file));
        setBorder(new LineBorder(Color.LIGHT_GRAY, 0, true));
        setContentAreaFilled(false);
        setFocusPainted(false);
        addMouseListener(this);
        setVisible(true);
    }

    public void mouseClicked(MouseEvent e)
    {

    }

    public void mouseEntered(MouseEvent e)
    {

    }

    public void mouseExited(MouseEvent e)
    {

    }

    public void mousePressed(MouseEvent e)
    {

    }

    public void mouseReleased(MouseEvent e) 
    {

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setPaint(Color.BLACK);
        g2.drawString(getText(), 0, 0);
        g2.setPaint(new GradientPaint(
                new Point(0, 0), 
                Color.WHITE, 
                new Point(0, getHeight()), 
                Color.PINK.darker()));
        g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);
        g2.dispose();

        //super.paintComponent(g);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
11684
  • 7,356
  • 12
  • 48
  • 71
  • What if you draw your String *after* you paint your gradient? Though a better way may be to simply create a BufferedImage with your button's image, make an ImageIcon out of it and set the Button's icon. Though I think that you'll need to get rid of margins and borders to succeed with this. – Hovercraft Full Of Eels Feb 05 '12 at 14:31
  • I wanted the mouseListener for animating mouseIn, mousePressed etc., but that worked, now I just want to draw that String. – 11684 Feb 05 '12 at 14:33
  • @HovercraftFullOfEels I tried that first, but that didn't work to... – 11684 Feb 05 '12 at 14:34
  • 2
    it works for me. Please see example posted below. Note that you can't use 0, 0 as the location for your text – Hovercraft Full Of Eels Feb 05 '12 at 14:36
  • why not? @HovercraftFullOfEels – 11684 Feb 05 '12 at 14:38
  • Re "why not" if you mean not using 0,0 -- I'm not sure, but through experience I know that it's so. – Hovercraft Full Of Eels Feb 05 '12 at 14:39
  • @HovercraftFullOfEels You were right, 0,0 is a point somewhere above my Button. I fixed it now, but you said the other answer was better, but I didn't completely understand it... Does JButton have paintButtonPressed() and paintFocus etc. etc. (inherited or not) and can I override them to repaint if my button is pressed, focussed etc.? – 11684 Feb 05 '12 at 14:45
  • @11684 right these methods are accesible from JButton's API http://docs.oracle.com/javase/6/docs/api/javax/swing/JButton.html – mKorbel Feb 05 '12 at 14:47
  • I can't find paintFocus there... You sure? @mKorbel – 11684 Feb 05 '12 at 14:50
  • I tried to override it, got this: `JIconButton.java:61: method does not override a method from its superclass` @mKorbel – 11684 Feb 05 '12 at 14:54
  • You extended MetalButtonUI, not JButton... – 11684 Feb 05 '12 at 14:55
  • I'll use HovercraftFullOfEels' method, with if statement, or something like that... – 11684 Feb 05 '12 at 14:56
  • @11684 again JButton's API have got implemented all these methods, and overrode paintComponent is last property how to do it, – mKorbel Feb 05 '12 at 15:31

4 Answers4

6

As per my comment, "it worked for me...."
For example:

   @Override
   protected void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE, new Point(0,
            getHeight()), Color.PINK.darker()));
      g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);
      g2.setPaint(Color.BLACK);
      g2.drawString(getText(), 30, 12);
      g2.dispose();

      // super.paintComponent(g);
   }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3

You have to do:

g2.drawString(getText(), 0, 10);

the y of the string coordinate must be greater than 0, because is the starting point of the baseline and not the point of the upper left corner of a box. The final code:

@Override
protected void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D) g.create();
  g2.setPaint(new GradientPaint(
  new Point(0, 0),
    Color.WHITE,
    new Point(0, getHeight()),
    color.PINK.darker()));
  g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);
  // The drawString(string) must be put after the setPaint(gradient)
  g2.setPaint(Color.BLACK);
  g2.drawString(getText(), 0, 10);
  g2.dispose();
}
Alberto
  • 1,569
  • 1
  • 22
  • 41
2

1) easiest way would be JButton's methods JButton(String text, Icon icon) example here

2) you can override XxxButtonUI, or change GradientButton

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Just call setContentAreaFilled(false) for your JButton, override paint() method and at last of paint() method call super.paint().

HomDhi
  • 31
  • 1