1

i want to overlap image on button(when it is clicked)....but on clicking it is not overlapping....please guide me where i am wrong....is it not possible to do so??? i am using frame to add buttons....

import java.awt.*;
import java.awt.event.*;

public class d extends Frame implements ActionListener {
    Image img, i1, i2;
    int x, y;
    String msg;
    Button one, two;

    d() {

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        setSize(1000, 500);
        setVisible(true);
        setLayout(null);
        one = new Button("1");
        two = new Button("2");
        add(one);
        add(two);
        one.addActionListener(this);
        two.addActionListener(this);

        one.setBounds(200, 100, 100, 100);
        two.setBounds(300, 100, 100, 100);

    }

    public void actionPerformed(ActionEvent e) {
        msg = e.getActionCommand();
        if (msg.equals("1")) {
            msg = "Pressed 1";
            img = i1;
            x = 200;
            y = 100;
        } else {
            msg = "Pressed 2";
            img = i2;
            x = 300;
            y = 100;
        }
        repaint();
    }

    public void paint(Graphics g) {
        Toolkit tool = Toolkit.getDefaultToolkit();
        i1 = tool.getImage("F:/Memories/rawk garden/a.jpg");
        i2 = tool.getImage("F:/Memories/rawk garden/b.jpg");
        g.drawImage(img, x, y, 100, 100, this);
        g.drawString(msg, 100, 300);
    }

    public static void main(String s[]) {
        new d();
    }
}
Braj
  • 46,415
  • 5
  • 60
  • 76
Codeluv
  • 35
  • 6
  • what you want to do? do you want an image button or want to paint a image when button is clicked and hide the button? – Braj Jul 13 '14 at 11:34
  • i want to paint a image when button is clicked and hide the button...plz help....!!! @Braj – Codeluv Jul 13 '14 at 13:43
  • you want to hide both the button or just the clicked one. – Braj Jul 13 '14 at 13:45
  • just the clicked one...other one should remain button only and replaced by image when clicked @Braj – Codeluv Jul 13 '14 at 13:47

2 Answers2

0

Setting an image on a button can be done as follows:

Button myButton = new Button(new ImageIcon("image_path"));
indivisible
  • 4,892
  • 4
  • 31
  • 50
Anshita
  • 11
  • 1
0

I have already shared you code for this in your last post where I suggested you some points with the sample code to achieve it using JLabel.

Read more points...


Don't use null layout. There are lots of Layout Managers choose any one that suits as per your application design.

How to Use Various Layout Managers

From comments below:

actually I am making minesweeper type game...i want button to change into image and once one button is converted to image...that image remains there.

Use JButton#setIcon() method to set the icon of the button.

Here is the sample code where

  • I have created a 9x9 grid that contains buttons.
  • Each cell has a random boolean flag whether it has mine or not.
  • When button is clicked I am setting its icon if it has mine.
  • Simply set the text empty and make it disabled to stop further clicks on already clicked button.

sample code:

final Image mine = ImageIO.read(new File("resources/mine.png"));
final boolean[][] showMine = new boolean[9][9];
Random random = new Random();
JPanel panel = new JPanel(new GridLayout(9, 9));
for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {

        showMine[i][j] = random.nextBoolean();

        final int x = i;
        final int y = j;
        final JButton button = new JButton();
        button.setText(String.valueOf(i * 9 + j));
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.setEnabled(false);
                button.setText("");
                if (showMine[x][y]) {
                    button.setDisabledIcon(new ImageIcon(mine));
                }
            }
        });

        panel.add(button);
    }
}
add(panel);

snapshot:

enter image description here

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • iam unable to run this code...line second"add(jlabel)" is showing error that "jlabel cannot be resolved to type"...please guide..!! – Codeluv Jul 13 '14 at 14:16
  • It's not a complete code. just paste the code in your class constructor. – Braj Jul 13 '14 at 14:18
  • thanks...it's working....but image is not overlapping button...i want image to overlap button...that is,like i have button of size 100-height 100-width....i want a 100-height and 100-width image cover that button on clicking that button – Codeluv Jul 13 '14 at 14:43
  • actually iam making minesweeper type game...i want button to change into image and once one button is converted to image...that image remains there – Codeluv Jul 13 '14 at 15:04
  • Sounds Good. You're welcome. If the problem is solved then please close the thread by accepting the post by ticking green right mark. – Braj Jul 14 '14 at 22:28