Question

I've started working on a project for my Java class - LAN gomoku/five in a row. The game board is represented by a 2-dimensional array filled with buttons (JButton). With the event handler (class clickHandler) I want to draw an oval on the button that I click (the parameter of a clickHandler object). My following code hasn't worked though (I don't know how to get rid of the null-value of variable g)... I'd appreciate any piece of advice. Thank You a lot.

    class clickHandler implements ActionListener {

        JButton button;
        Dimension size;
        Graphics g;

        public clickHandler(JButton button) {
            this.button = button;
            this.size = this.button.getPreferredSize();
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
                this.g.setColor(Color.BLUE);
                this.g.fillOval(this.button.getHorizontalAlignment(), this.button.getVerticalAlignment(), this.size.width, this.size.height);

                this.button.paint(this.g);
                this.button.setEnabled(false);
        }
    }

(In a class that creates the GUI - the game board full of buttons - I assign each button a new Action Listener - an instance of clickHandler) this way:

    gButton.addActionListener(new clickHandler(gButton));

No correct solution

OTHER TIPS

You have to:

  • Extends the JButton class, and override the paintComponent(Graphics g) method.
  • Do override getPreferredSize() method, which will return on Dimension object and will help the Layout Manager in placing your JButton on the Container/Component, by providing it one appropriate size.

  • Make your circle code there.

  • add an onClickListener, and set a flag on the clicked button if it is clicked, and call it to repaint.


About the Graphics object: it's best to keep it in it's paintComponent method, and to use it only there. It will always get passed in on a repaint, and if you save it for other moments, strange things can happen (happy experimenting :) ).

A small Example :

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

public class ButtonExample
{
    private MyButton customButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Custom Button Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        customButton = new MyButton();
        customButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                MyButton.isClicked = true;
                customButton.repaint();
            }
        });

        frame.getContentPane().add(customButton, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonExample().displayGUI();
            }
        });
    }
}

class MyButton extends JButton
{
    public static boolean isClicked = false;

    public Dimension getPreferredSize()
    {
        return (new Dimension(100, 40));
    }

    public void paintComponent(Graphics g)
    {
        if (!isClicked)
            super.paintComponent(g);
        else
        {
             g.setColor(Color.BLUE);
             g.fillOval(getHorizontalAlignment(), getVerticalAlignment(), getWidth(), getHeight());
        }       
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top