1

I'm writing minesweeper and I want to put ovals on mine cells after a mine is clicked. But for now, I just want to put ovals on all cells just to check. I have written the code below. But when I run the program there is no ovals on buttons. I can not see the reason. I would be grateful if I get some suggestions.

public class Cell extends JButton{
...
  public void painComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.orange);
    g.drawOval(0,0,25,25);

}

public void draw() {
    repaint();
}
...
}



public class Grid extends JPanel implements MouseListener{
...
public Grid(){
    this.setLayout(new GridLayout(20,20));
    cells = new Cell[20][20];
    for(int i=0; i<20; i++) {
        for(int j=0; j<20; j++) {
            cells[i][j] = new Cell(i,j);
            cells[i][j].addMouseListener(this);
            cells[i][j].draw();
            this.add(cells[i][j]);
        }
    }
    plantMines();
    setVisible(true);

}
...
}
  • Did you add any System.out.println(...) statements in the method to see if it executes? Don't assume the code executes! – camickr Nov 26 '18 at 00:54
  • 1
    Why not simply use images? – MadProgrammer Nov 26 '18 at 00:57
  • @MadProgrammer *"Why not simply use images?"* Even simpler is to use text. [This example](https://stackoverflow.com/a/41798128/418556) uses text to represent the state of play (number of mines, identified mines etc.). – Andrew Thompson Nov 26 '18 at 05:55

1 Answers1

0

but when I run the program there is no ovals on buttons.

public void painComponent(Graphics g) {

You made a typo. You spelt "paint" wrong.

When you override a method you should use:

@Override
protected void paintComponent(Graphics g)

This way if you make a typo the compiler will tell you that you are not overriding a method of the class.

Also, don't attempt to draw the oval by using custom painting. Instead you should be adding an Icon to the button. Then you can just change the Icon as required.

You can easily create an simple Icon to use. Here is an example of creating a square Icon:

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

public class ColorIcon implements Icon
{
    private Color color;
    private int width;
    private int height;

    public ColorIcon(Color color, int width, int height)
    {
        this.color = color;
        this.width = width;
        this.height = height;
    }

    public int getIconWidth()
    {
        return width;
    }

    public int getIconHeight()
    {
        return height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }

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

    public static void createAndShowGUI()
    {
        JPanel panel = new JPanel( new GridLayout(2, 2) );

        for (int i = 0; i < 4; i++)
        {
            Icon icon = new ColorIcon(Color.RED, 50, 50);
            JLabel label = new JLabel( icon );
            label.setText("" + i);
            label.setHorizontalTextPosition(JLabel.CENTER);
            label.setVerticalTextPosition(JLabel.CENTER);
            panel.add(label);
        }

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        f.setSize(200, 200);
        f.setLocationRelativeTo( null );
        f.setVisible(true);
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288