Swap JButton Icons On MouseClicks - java

I have a JButton[][] array that stores every button on a grid.
What I want to do is :
1) click the jbutton(icon) that I want to move on the grid.
2) click on the jbutton that I want the previous selected jbutton(icon) to move to.
private class BListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
JButton but = ((JButton) e.getSource());
if(iconSelected && !but.equals(selectedButton)){ // move(swap) buttons
but.setIcon(selectedButton.getIcon());
selectedButton.setBorder(BorderFactory.createLineBorder(Color.black));
selectedButton.setName(null);
selectedButton=but;
iconSelected=false;
}else if(!iconSelected && but.getName()!=null){
iconSelected=true;
selectedButton=but;
but.setBorder(BorderFactory.createLineBorder(Color.YELLOW,3));
}else{
if(iconSelected){
System.out.println("Already Selected");
}else{
System.out.println("Not selected");
}
}
}
I have tried some things that didnt work ( this moves the icon but the icon also remains at the starting location). Any insight would be helpfull.

That is because you never change the selectedButton's icon, Try this:
if(iconSelected && !but.equals(selectedButton)){ // move(swap) buttons
Icon bIcon = but.getIcon();
but.setIcon(selectedButton.getIcon());
selectedButton.setIcon(bIcon);
...
}

Related

Icon of Button does not Update upon clicking

Im trying to make a simple Tic Tac Toe game right now, i created a JFrame with the Grid Layout and added 9 buttons to it. Now uppon clicking one of those buttons i want to change the Icon of it to be either a cross or a circle, but it just doesnt do anything on click
#Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == this)
{
if (counter%2 == 0)
{
ImageIcon cross = new ImageIcon("Cross.png");
this.setIcon(cross);
}
if (counter%2 == 1)
{
ImageIcon circle = new ImageIcon("Circle.png");
this.setIcon(circle);
}
counter++;
}
}
Project Structure here
The actionPerformed method does work with a simple System.out.println() statement
Thanks in Advance!
You have to understand the meaning of e.getSource(), it's return all properties of button. Your condition e.getSource() == this return false that is a problem. You have to set image icon like new javax.swing.ImageIcon(getClass().getResource("path/img_name")).
Here down is generic code implementation so the logic will work on any of the nine buttons:
public void jButton1ActionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(counter % 2 == 0)
{
button.setIcon(new javax.swing.ImageIcon(getClass().getResource("Cross.png")));
}
else
{
button.setIcon(new javax.swing.ImageIcon(getClass().getResource("Circle.png")));
}
counter++;
}

Stop Jbutton from making a change if already clicked

Novice programmer here trying to make a tic tac toe GUI game. I'm stuck with my program though. I'm not sure how to place a check on hitting the same square twice. I was thinking an if statement inside my actionListener that said
if(button clicked = True)
{
JOptionPane.showMessageDialog((null, "ERROR", "Button already used.
Please hit again to change back", JOptionPane.ERROR_MESSAGE);
// STOP something along those lines
}
else
{
//Do nothing
}
would work but I can't get the program to work properly. I tried newTurn.getmodel().isPressed() and that didn't work and now with my current code the program outputs the error message after each move and the changes still appear on the board. Here's my code for this method. Any help is appreciated.
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton newTurn = (JButton)e.getSource(); //get the particular button that was clicked
if(switchMove%2 == 0)
newTurn.setText("X");
else
newTurn.setText("O");
if(newTurn.isEnabled())
JOptionPane.showMessageDialog(null, "ERROR", "Button already used. Please hit again to change back", JOptionPane.ERROR_MESSAGE);
if(checkForWin() == true)
{
JOptionPane.showConfirmDialog(null, "Game Over.");
resetButtons();
}
switchMove++;
}
switch move is just an int set to 0 so evens are X and O's are odd. My if (newTurn.isEnabled()) is my issue
Here is my resolved code.
public void resetButtons()
{
for(int i = 0; i <= 8; i++)
{
buttons[i].setText("");
buttons[i].setEnabled(true);
}
}
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton newTurn = (JButton)e.getSource();
if(switchMove%2 == 0)
newTurn.setText("X");
else
newTurn.setText("O");
if(newTurn.isEnabled())
newTurn.setEnabled(false);
if(checkForWin() == true)
{
JOptionPane.showConfirmDialog(null, "Game Over.");
resetButtons();
}
switchMove++;
}
In the actionPerformed() method I set the buttons to setEnabled(false) after a button was clicked. Then when a game is over the buttons that were previously disabled will be set to setEnabled(true) via the resetButtons method.

Minesweeper Object GUI

I am trying to do a simple Minesweeper game using JFrame, however I am having troubles with the creation of objects. I am creating 96 buttons, some of which get the property of being wrong ("F") and right ("R"):
public class GUIBase extends JFrame {
private JButton button;
private JButton fButton;
public GUIBase() {
super("Minesweeper");
setLayout(new FlowLayout());
//Fields
int position;
for (int i = 0; i < 96; i++) {
position = (int) (Math.random() * 100);
if (position < 80) {
button = new JButton("R");
button.setToolTipText("Is this the correct one?");
add(button);
} else {
fButton = new JButton("F");
fButton.setToolTipText("Is this the correct one?");
add(fButton);
}
}
I then use ActionListener in order to check whether or not the button is correct. If the button is correct, it will get .setEnabled(false), otherwise the game ends:
//Action
Action action = new Action();
button.addActionListener(action);
fButton.addActionListener(action);
}
private class Action implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Somethin");
if (event.getSource() == button) {
button.setEnabled(false);
} else if (event.getSource() == fButton) {
JOptionPane.showMessageDialog(null, "You lost!");
System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "An error ocurred");
System.exit(0);
}
}
}
Everything in the game turns out as planned, however only the last correct button ("R") and last wrong one ("F") are connected to the ActionListener. The rest of the buttons do not do anything when pressed.
How can I fix this?
The problem is that you only have two variables (attributes of the class GUIBase, specifically), and your are assigning to it each time you create a new button. Hence, you only have a reference to the last buttons.
You need an array of buttons. Let's see:
public class GUIBase extends JFrame {
public final int MAX_BUTTONS = 96;
private JButton[] buttons;
// ...
}
The next step is to create the array itself at the beginning:
public GUIBase() {
super("Minesweeper");
setLayout(new FlowLayout());
this.buttons = new JButton[MAX_BUTTONS];
//Fields
int position;
for (int i = 0; i < buttons.length; i++) {
position = (int) (Math.random() * 100);
this.buttons[ i ] = new JButton("R");
this.buttons[ i ].setToolTipText("Is this the correct one?");
this.add(this.buttons[ i ]);
Action action = new Action();
this.buttons[ i ].addActionListener(action);
}
}
You'll probably need more depth in arrays in order to completely understand the code. Basically, an array is a continuous collection of variables, which you can index by its position, from 0 to n-1, being n the number of positions.
Then you'll probably be able to fill the gaps yourself.
Hope this helps.
One part of your problems is coming from your action listener.
Of course, one part is that your code probably needs a list/array to keep track of all created buttons; but at least right now, you can rework your code without using arrays/list:
private class Action implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Somethin");
if (event.getSource() instanceofJButton) {
JBUtton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("R") ...
else if (buttonText.equals("F")
You see, the whole point here is: as of now, you just need to know what kind of button was created. And your ActionListener knows which button it was clicked on ...

Middle mouse button on java not working properly

When i use mouseListener and I chek for a middle mouse button it does not react properly I don't know why but it looks like i need to scroll while cliking to get the event to occur
some part of my code if it helps
public void mouseClicked(MouseEvent e) {
if(new Rectangle(0,0,1274,30).contains(Screen.mse)){
TopMenu.click();
}else if(new Rectangle(0,31,1100,549).contains(Screen.mse)){
Map.cliked(e.getButton(),0);
System.out.println("mouse:"+e.getButton());
}else if(new Rectangle(1100,30,174,550).contains(Screen.mse)){
//cliked ModeMenu
}else if(new Rectangle(0,580,1100,164).contains(Screen.mse)){
//cliked ToolsMenu
}else{
//cliked mode change
}
switch(e.getModifiers()) {
case InputEvent.BUTTON1_MASK: {
System.out.println("That's the LEFT button");
break;
}
case InputEvent.BUTTON2_MASK: {
System.out.println("That's the MIDDLE button");
break;
}
case InputEvent.BUTTON3_MASK: {
System.out.println("That's the RIGHT button");
break;
}
}
}
If you look at the javadoxs for MouseEvent, you can see that BUTTON1, BUTTON2 and BUTTON3 are not referred to the left, middle and right mouse buttons. It depends on the mouse what BUTTON 1,2 and 3 mean, so it can happen that BUTTON2 does not refer to the middle Button. To see if the middle Button of your mouse is recognized correctly, try the following:
public void mouseClicked(MouseEvent e){
System.out.println(e.getButton());
}
Now press your middle mouse button. If there is no output in the console, your mouse has no middle button (or it is not recognized properly). If there is an output, it corresponds to the button(1=BUTTON1,2=BUTTON2,3=BUTTON3). If the ouput is 0, then the button is MouseEvent.NOBUTTON, which is unlikely to happen.
Another thing: Try using SwingUtilities.isMiddleButton(MouseEvent e). This may fix some problems with your mouse. If you do so, chage your mouseClicked() method to
public void mouseClicked(MouseEvent e)
{
if(SwingUtilities.isLeftMouseButton(e))
{
System.out.println("That's the LEFT button");
}
else if(SwingUtilities.isMiddleMouseButton(e))
{
System.out.println("That's the MIDDLE button");
}
else if(SwingUtilities.isRightMouseButton(e))
{
System.out.println("That's the RIGHT button");
}
}
(of course with all the other code you wrote above the original switch statement)

How to call a function when I click on a jPanel (Java)?

I'm working with Netbeans IDE in Java.
I've a form with one JPanel.
Each JPanel has a gridLayout 3x3 and in each place there is an image representing a number[0,1,2,3,4,5,6,7,8](the image is created used a custom class,not just fitting the image in a lab).
I want to be able to exchange two images in the panel when the user click them (First click: no action , second click: switch the two images fitted in the jPanel Components).
I already created a function exchangeComponents and with a test code (like:
exchangeComponents (0,8,jPanel1)
it exchanges correctly the images located in position1 (1st row,1st column) and in position2 (3rd row,3rd column).
The function a creted is the following:
public void exchangeComponents(int component1,int component2,JPanel jpanel){
try{
Component aux1 = jpanel.getComponent(component1);
Point aux1Loc = aux1.getLocation();
Component aux2 = jpanel.getComponent(component2);
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
}
catch (java.lang.ArrayIndexOutOfBoundsException ex){ /* error! bad input to the function*/
System.exit(1);
}
}
I suppose I neeed to have an event that call the function exchangeComponents() when the user click on one of the images on the jPanel1 but how should I do it? and how to check what components (images) the user has selected?
I just know that when I create a Button if a click on it (from the IDE) an event like
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// some code..
}
is created and the code I fill in is executed.
Thank you in advance for any hint.
You need to add the same mouse listener to all you JLabels or whatever container you have for your images, like:
img1.addMouseListener(this);
img2.addMouseListener(this);
etc., then detect which Jlabel you clicked with MouseEvent.getSource(); , like this
boolean hasclicked1=false;
JLabel click1label=null;
public void mouseClicked(MouseEvent me){
if(!hasclicked1){ //clicked first pic
hasclicked1 = true;
click1label = (JLabel) me.getSource();
} else { //clicked second pic
hasclicked1 = false;
exchangeComponents(click1label, (JLabel) me.getSource(), /*your jpanel here*/);
}
//now change exchangeComponents so it uses JLabels as parameters
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
try{
Component aux1 = component1;
Point aux1Loc = aux1.getLocation();
Component aux2 = component2;
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
} catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
System.exit(1);
}
}
If you are not using JLabels for the images though, replace JLabel in the code with whatever you are using...
EDIT: Sorry, I don't think I made this unclear, but your class with the method exchangeComponents has to implement MouseListener. Then, in the mouseClicked event put the code I gave for it. Make sure to include the variables hasclicked1 and click1label in your class. Make you class something like this
public class ComponentExchanger implements MouseListener {
boolean hasclicked1=false;
JLabel click1label=null;
JPanel mainPanel;
public ComponentExchanger(){
//create JFrame, JPanel, etc.
JFrame f=new JFrame();
//etc.
mainPanel=new JPanel();
f.add(mainPanel);
//set layout of panel, etc.
for(int i=0;i<9;i++){
JLabel l=new JLabel(/*label image here*/);
Point loc=new Point(/*coordinates here*/);
l.setLocation(loc);
mainPanel.add(l);
/*more code*/
f.setVisible(true);
}
}
public static void main(String args[]){
new ComponentExchanger();
}
public void mouseClicked(MouseEvent me){
if(!hasclicked1){ //clicked first pic
hasclicked1 = true;
click1label = (JLabel) me.getSource();
} else { //clicked second pic
hasclicked1 = false;
exchangeComponents(click1label, (JLabel) me.getSource(), mainPanel);
}
//now change exchangeComponents so it uses JLabels as parameters
public void exchangeComponents(JLabel component1, JLabel component2, JPanel jpanel){
try{
Component aux1 = component1;
Point aux1Loc = aux1.getLocation();
Component aux2 = component2;
Point aux2Loc = aux2.getLocation();
aux1.setLocation(aux2Loc);
aux2.setLocation(aux1Loc);
} catch (java.lang.ArrayIndexOutOfBoundsException ex) { /* error! bad input to the function*/
System.exit(1);
}
}
//Also, you will need to include the other mouselistener implemented methods, just
//leave them empty
}
First of all, to be technical it's methods not functions.
There are a couple of ways you could do this. You could go ahead with actionListener, but then you would probably need buttons or something.
Or you could use MouseListener, and detect clicks over a certain region of the panel.
For the switching algorithm, perhaps an array of 2 images. There is a variable that increases by 1 every click. When the variable is 2, it resets back to 0.
clicks++; //every time the mouse is clicked; clicks starts out at 0
if(clicks == 2){
clicks = 0; //at the end of the listener method
}
On the first click the clicked image goes into the first array slot, because the user has clicked once.
clickImage = imageArray[clicks];
On the second click, the other clicked image goes to the second array slot, because 2 clicks have been detected. In this case, your exchangeComponents method would go at the end of the listener method, with the arguments being imageArray[1], imageArray[2], .
You can apply this to ints or whatever, just save the value in an array and use an incrementing and resetting variable.

Categories