views:

39

answers:

3

Hi everyone! I'm designing an optimization system for public transport in a big city. So I have a map with some points on it, but don't care about it)
All I need is: my own JButton, which looks like a color-filled circle and a small text tag near it. I got some problems while overriding the paintComponent() method.. the round button is painted correctly, but not the text.
BUT, when i'm resizing the window manually, the text appears for a second, then it gets repainted again and dissapears. Hope you guys understood my needs, thanks for help ;)

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


public class JRoundButton extends JButton { 

String label;
Color color;
int x,y;
public JRoundButton(Color color,int x,int y,String str)
{
    label=str;
    this.x=x;
    this.y=y;   
    this.color=color;       
}

protected void paintComponent(Graphics g)  
    {
    super.paintComponent(g);
    Dimension size = getPreferredSize();
    setPreferredSize(size);
    this.setBounds(0, 0, 10, 10);
    setContentAreaFilled(false);

    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawChars(label.toCharArray(), 0, label.length(), 12,12);
    g.fillOval(0,0,8,8);
}

public void paintBorder(Graphics g)
    {
    g.setColor(Color.white);
    g.drawOval(0,0, 9, 9);
}
public static void main(String[] args)  
    {
    JButton button = new JRoundButton(Color.GRAY,150,150,"Times Square");

    JFrame frame = new JFrame();
    frame.getContentPane().setBackground(Color.black);
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.add(button);
}

}