Roshnal
Roshnal

Reputation: 1314

How to paint corners of a JButton with numbers?

I'm making a simple Kakuro application in Java Swing and I have used JButtons as cells. I've done everything from generating the grid (setBackground(Color.BLACK) and setBackground(Color.WHITE)) to filling with unique numbers.

But the problem is, I don't know how to paint the "clues" at the ends of JButtons. What I want is similar to:

enter image description here

Sometimes the numbers may appear on only 3 sides, 2 sides or even 1 side.

I thought of setting background images, but its not possible as the numbers are sums of dynamically generated numbers (the grid is dynamic).

So any idea how to get this kind of JButton? Or if its not possible, what other options do I have?

Thanks a lot in advance (I'm really stuck).

Upvotes: 2

Views: 283

Answers (4)

mKorbel
mKorbel

Reputation: 109815

very simple and confortable way is add JLabels to the JButton by using BorderLayout,

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class FourLabelsInButton {

    private JFrame frame;
    private JButton myButton1;
    private JLabel myButton1_Label_N;
    private JLabel myButton1_Label_E;
    private JLabel myButton1_Label_W;
    private JLabel myButton1_Label_S = new JLabel();

    public FourLabelsInButton() {
        myButton1_Label_N = new JLabel("45");
        myButton1_Label_N.setHorizontalAlignment(JLabel.CENTER);
        myButton1_Label_N.setForeground(Color.red);

        myButton1_Label_E = new JLabel("1");
        myButton1_Label_E.setHorizontalAlignment(JLabel.CENTER);
        myButton1_Label_E.setForeground(Color.red);

        myButton1_Label_W = new JLabel("9");
        myButton1_Label_W.setHorizontalAlignment(JLabel.CENTER);
        myButton1_Label_W.setForeground(Color.red);

        myButton1_Label_S = new JLabel("21");
        myButton1_Label_S.setHorizontalAlignment(JLabel.CENTER);
        myButton1_Label_S.setForeground(Color.red);

        myButton1 = new JButton();
        myButton1.setBackground(Color.black);
        myButton1.setLayout(new BorderLayout());
        myButton1.add(myButton1_Label_N, BorderLayout.NORTH);
        myButton1.add(myButton1_Label_E, BorderLayout.EAST);
        myButton1.add(myButton1_Label_W, BorderLayout.WEST);
        myButton1.add(myButton1_Label_S, BorderLayout.SOUTH);

        frame = new JFrame();
        frame.add(myButton1);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                FourLabelsInButton ggg = new FourLabelsInButton();
            }
        });
    }
}

Upvotes: 3

utopianheaven
utopianheaven

Reputation: 1277

There shouldn't be any reason you can't simply draw anything you want in the JButton by overwriting the paint() method.

public class KakuroSquare extends JButton
{
   /* ... */

   @Override
   public void paintComponent(Graphics g)
   {
       super.paintComponent(g);

       /* Your paint logic. */
   }
}

Upvotes: 0

Howard
Howard

Reputation: 39177

You may also use a custom component. For this case the painting can be implemented quite straightforward:

class KakuroComponent extends JComponent {

    private final int[] numbers;

    public KakuroComponent(int... numbers) {
        this.numbers = numbers;
    }

    @Override
    public void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getWidth();

        g.setColor(Color.black);
        g.fillRect(0, 0, w, h);

        g.setColor(Color.white);
        g.drawLine(0, 0, w, h);
        g.drawLine(w - 1, 0, 0, h - 1);

        if (numbers[0] > 0) // if there is a top number
            drawStringCentered(g, String.valueOf(numbers[0]), w / 2, h / 6);
        if (numbers[1] > 0) // if there is a left number
            drawStringCentered(g, String.valueOf(numbers[1]), w / 6, h / 2);
        if (numbers[2] > 0) // if there is a right number
            drawStringCentered(g, String.valueOf(numbers[2]), w * 5 / 6, h / 2);
        if (numbers[3] > 0) // if there is a bottom number
            drawStringCentered(g, String.valueOf(numbers[3]), w / 2, h * 5 / 6);
    }

    void drawStringCentered(Graphics g, String s, int x, int y) {
        Rectangle2D bounds = g.getFontMetrics().getStringBounds(s, g);
        g.drawString(s, (int) (x - bounds.getCenterX()), (int) (y - bounds.getCenterY()));
    }
}

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168815

I thought of setting background images, but its not possible as the numbers are sums of dynamically generated numbers (the grid is dynamic).

The images for the buttons can also be generated dynamically.

Upvotes: 2

Related Questions