-2

I want to change the icon used by imageicon in this code to another one by clicking in that imageicon. Can anybody help me please? what can i do in this code?? and i want it to also make fall down if the bottom is filled with white color.If one user click at the white point the color changes to yellow then after, if again click on another white one the color changes to red.

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


class Connect4Games extends JFrame implements ActionListener, MouseListener 
{
JFrame frame;
JPanel pane;
JLabel insertaxis[][];
ImageIcon EmptySpace, circleYellow, circleRed ;
BufferedImage bufferedImage;
public Connect4Games() {
    LookAndFeel.setLookAndFeel();
    pane = new JPanel();
    frame = new JFrame();
    insertaxis = new JLabel[6][7];
    EmptySpace = new ImageIcon("image/Circle.png");
    circleYellow = new ImageIcon("image/Circle2.png");
    circleRed = new ImageIcon("image/Circle3.png");
    pane.setLayout(new GridLayout(6, 7));
    pane.setBackground(Color.blue);
    add(pane);
    addMouseListener(this);
    setTitle("Connect 4");
    setVisible(true);
    setSize(670, 590);
    frame.pack();
    setLocationRelativeTo(null);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            insertaxis[i][j] = new JLabel();
            pane.add(insertaxis[i][j]);
            insertaxis[i][j].setIcon(EmptySpace);
        }
    }
}

@Override
public void actionPerformed(ActionEvent e) {
}

public static void main(String[] args) {
    new Connect4Games();
}

@Override
public void mouseClicked(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    if(e.getPoint() == null){
        insertaxis[x][y].setIcon(circleRed);
    }
}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50

1 Answers1

1

Personnally, I prefer to use a JButton for this kind of thing, although it's also feasible with a JLabel. The main advantage of JButton over JLabel is that it is made "to be clicked", so you can directly take advantage of the method addActionListener.

Now you will need to do a more things than just setting the icon color on the button, but that's a start.

I also slightly refactored your code because there were some mistakes in it:

  • You create a JFrame that you actually never used. It was actually a good idea to use that JFrame instead of extending a JFrame (no need to extend JFrame if you don't add any specific behaviour to it)
  • Calls to pack() and setVisible(true) should be one of the last calls you do
  • All changes to Swing UI should be done on the EDT (Event Dispatching Thread) and the UI should be started within an invokeLater block, ensuring that it runs on the EDT.

Here is a small implementation showing how you can easily achieve this with JButton.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Connect4Games {
    JFrame frame;
    JPanel pane;
    JButton insertaxis[][];
    ImageIcon emptySpace, circleYellow, circleRed;
    BufferedImage bufferedImage;

    private boolean red = true;

    public Connect4Games() {
        frame = new JFrame();
        pane = new JPanel();
        insertaxis = new JButton[6][7];
        emptySpace = new ImageIcon(getCircle(Color.GRAY));
        circleYellow = new ImageIcon(getCircle(Color.YELLOW));
        circleRed = new ImageIcon(getCircle(Color.RED));
        pane.setLayout(new GridLayout(6, 7));
        pane.setBackground(Color.blue);
        frame.add(pane);
        frame.setTitle("Connect 4");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                final JButton button = new JButton();
                button.setBorderPainted(false);
                button.setContentAreaFilled(false);
                button.setFocusPainted(false);
                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (button.getIcon() == emptySpace) {
                            if (red) {
                                button.setIcon(circleRed);
                            } else {
                                button.setIcon(circleYellow);
                            }
                            red = !red;
                        } else {
                            JOptionPane.showMessageDialog(button, "Sorry, you cannot change the color of this place");
                        }
                    }
                });
                insertaxis[i][j] = button;
                pane.add(insertaxis[i][j]);
                insertaxis[i][j].setIcon(emptySpace);
            }
        }
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);

        frame.setVisible(true);
    }

    private static BufferedImage getCircle(Color color) {
        BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(color);
        g.fillArc(0, 0, 32, 32, 0, 360);
        return image;
    }

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

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Can you please make that circles a little bit close...... i cannot get any idea to get them closer. cuz m just a rookie in java. Please help me – Jack Rourke Sep 15 '14 at 03:42
  • @JackRourke on each button set the border to a custom value. Something like: `button.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));`. You can adjust the values to your requirements (it can even be zero if you want). – Guillaume Polet Sep 15 '14 at 07:35
  • i also want to make that red and yellow circle should fall into the bottom if there is white space and no color circle. i don't have any idea about this. can you please give me any idea about that? and also if 4 circle with same color are in order horizontally, vertically or diagonally will show a message to win that game. – Jack Rourke Sep 15 '14 at 07:48
  • @JackRourke That's common logic that you should implement and starts to get off-topic. I am not an answering machine that will write code for you. If you have a specific question, then post a new question on SO. – Guillaume Polet Sep 15 '14 at 09:51