I am developing a memory match game. When user clicks a button then icon of that button changes. And if two images(icons) are same the both buttons hide. But if not their icons are set to default.
I am facing problem that when two images are same first one is shown but when second button is clicked if icon match with previous button then I want that 2nd button icon should be visible for a while. Means I want to add delay.
Edit :Here is my code:
if (count==1)
{
arr[0]=b;
sscard[0]=c;
}
if(count==2)
{
arr[1]=b;
sscard[1]=c;
if ((arr[0]!=arr[1]) && (sscard[0]==sscard[1]))
{
arr[0].setVisible(false);
arr[1].setVisible(false);
hide+=2;
if (hide==20)
{
t.stop();
JOptionPane.showMessageDialog(this,"YOU WON");
}
count=0;
}
else
{
arr[0].setIcon(icon);
arr[1].setIcon(icon);
count=0;
}
}
Related
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);
...
}
I'm writing a simple paint program with Java. As all paint applications there are buttons for brushTool, sprayTool, sprayTool... This tools have their own class which extends to MouseAdapter. They are working as they should. However, the problem starts when I choose a tool after choose another tool, both buttons and their ActionListeners keep executing and they do what they are written for at the same time. I mean if I choose lineTool(which draws straight line) with rectangleTool I hava a diagonal too. here is example of my two button. What I'm tring to do is stop the current action when I click another button. Can you guys help me
brushBotton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
pen = new PenTool(mainDrawArea);
mainDrawArea.addMouseListener(pen);
mainDrawArea.addMouseMotionListener(pen);
}
});
rectangleButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
shapeToolbar.setVisible(false);
rect = new RectangleTool(mainDrawArea);
rect.setStrokeSize(strokeInt);
mainDrawArea.addMouseListener(rect);
mainDrawArea.addMouseMotionListener(rect);
}
});
You can't keep adding a MouseListener to the drawing area every time you click a button.
Instead you need to keep track of the current MouseListener. Then when you click a button you need to:
remove the current MouseListener
add the new MouseListener
I would replace the button action listener for a set of Toggle Buttons in a group
https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html
Then you move everything in a single mouse listener.
public void mousePressed(MouseEvent e) {
this.drawingState = !this.drawingState
if ( isRightCLick(e) ) resetAllPendingOperation();
if (drawingState) {
this.startPoint = getPointFromEvent(e);
switch(toolbarGetCurrentTool()) {
case "line":
registerMouseLineListener(startPoint);//here you draw live preview
break
case "rectangle":
registerMouseRectangleListener(startPoint); //here you draw live preview
break;
}
} else {
//user clicked the second time, commit changes
//same switch as above
this.endPoint = getPointFromEvent(e);
switch(toolbarGetCurrentTool()) {
case "line":
commitLine(startPoint, endpoint);//here you draw live preview
break
case "rectangle":
commitRectangle(startPoint, endpoint); //here you draw live preview
break;
}
}
}
You are currently binding the listeners to the mainDrawArea, not setting an action for each individual button.
Note that the codes you write within actionPerformed() for each button's actionListener is the action you want to trigger everytime that button is clicked. You do not want to add a new listener to the mainDrawArea everytime we click the buttons.
You can a create a state for your current action, for example:
brushBotton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
state = BRUSH;
}
});
lineBotton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
state = LINE;
}
});
state can be an integer and BRUSH and LINE are constant such as 0 and 1.
Then in the listener (for the mainDrawArea), check the current state
switch (state){
case BRUSH: //trigger action needed for brushing;
break;
case LINE: //trigger action needed for drawing line;
break;
}
currently my program have a selection page which contain 4 action button with one terminate button and after the user click into one of the action button the button will set enabled (false) and i want to set terminate button in false mode when those 4 buttons are enabled but when 4 button is diabled than the terminate button will be enabled
do {
if (SIG.isEnabled() && RG.isEnabled() && AaCG.isEnabled() && SRG.isEnabled()) {
Terminate.setEnabled(false);
} else {
Terminate.setEnabled(true);
}
} while (Terminate.equals(false));
}
try to use do while loop but i dont know how to code it properly
Add an ActionListener to the button. The code it contains will be executed everytime the button is pressed.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Do your check here
if (SIG.isEnabled() && RG.isEnabled() && AaCG.isEnabled() && SRG.isEnabled()){
Terminate.setEnabled(false);
} else {
Terminate.setEnabled(true);
}
}
});
PD: According to the Java Naming Conventions, your variable, fields and method names should start with a lowercase letter, to not confuse them with a Class or an Interface.
In the below code, I have a label named card with a mouse click event. I only want the click event to implement once. Meaning it will implement the first time I click the label, but not the following times. How do I do this? I imagine I must disable its Listener.
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
i++;
card.setText(cardB[i]);
}
I think we all would do the same.
It's really simple. Just declare a boolean then change its status when you click the first time.
boolean labelClicked = false;
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
if(!labelClicked){
i++;
card.setText(cardB[i]);
labelClicked=true;
}
else{
//doNothing
}
}
I am pretty new to programming and am trying to make a Minesweeper GUI. The game worked perfectly right clicking a JToggleButton displayed a "B" for bomb on the button, but when I replaced the setText() with setIcon() in the mouselistener it shows the icon when both left and right clicking occurs. I didn't have this problem when setText().
public void mousePressed(MouseEvent e){
if(e.isMetaDown())
if(btnPresses == 0)
{
startTime = System.currentTimeMillis();
btnPresses++;
}
//if(btn[y][x].getText().equals("B"))
if(btn[y][x].getIcon()==flag)
{
//btn[y][x].setText("");
btn[y][x].setIcon(null);
if(bombs[y][x]!=BOMB)
markers++;
}
else
{
//btn[y][x].setText("B");
btn[y][x].setIcon(flag);
if(bombs[y][x]==BOMB)
markers++;
else
markers--;
}
I added a btn[y][x].setIcon(null) to the actionlistener, which causes the flag icon to appear only briefly when left clicking but I'd rather it not appear at all.
You need to distinguish between a left mouse button click, MouseEvent.BUTTON3, and a right mouse button click, MouseEvent.BUTTON3, and then act accordingly. For example, when I did something like this, I set the "flag" boolean in my model (using MVC) via:
#Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
model.upDateButtonFlag();
}
}
The MouseListener should be used only to set or unset the flag. Otherwise you should have your JButton respond via its ActionListener for left button clicks.
Add a System.err.println("" + System.currentTimeMillis() + " " + e); to the beginning of your handler. I strongly suspect your code is being called more times than you think - as a single click can generate multiple events. Once you know what is going on, it should be easy to fix.