logo
down
shadow

Matching Card Game, in Java The JButton won't show the Icon before removing it or changing Icon to null


Matching Card Game, in Java The JButton won't show the Icon before removing it or changing Icon to null

By : Azazavr
Date : November 21 2020, 09:01 AM
it helps some times
instead of showing the second card then a delay and then removing both cards or just removing the icon... the delay holds the button in the pressed down position and never shows the second icon
code :
Thread.sleep (1000);


Share : facebook icon twitter icon
JButton's icon isn't changing (UNO card game)

JButton's icon isn't changing (UNO card game)


By : Nam Vu
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Two things to check:
Make sure you're calling setBtnIcon inside some kind of event listener such that it fires when you want it to. I couldn't tell from your original question whether "it just sets the icon at the beginning of execution" meant the method was only being called once. Run through your code with a debugger (or add a print statement or two) to make sure that the result of getClass().getResource(...) isn't null. Getting co-located resources can be a tricky thing, especially if you're distributing your app in a JAR file.
Changing the icon of a JButton

Changing the icon of a JButton


By : seema
Date : March 29 2020, 07:55 AM
I wish this help you This output definitely shows a problem with the logic of the game.
code :
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class TicTacToe extends JFrame {

private static final String TITLE = "Tic Tac Toe";
private static final int width = 600;
private static final int height = 800;

private Container content;
private JButton exitbutton;
private JButton initbutton;
private JButton[] spaces = new JButton[9];
private CellButtonHandler[] cellHandler = new CellButtonHandler[9];
private InitButtonHandler inithandler;
private ExitButtonHandler exithandler;
private JLabel turn;
private URL base;
private Image o, x, blank;
private Font myfont = new Font("Times New Roman", Font.BOLD, 78);
private ImageIcon icon;
private ImageIcon iconx;
private ImageIcon icono;

private boolean gameover;
private boolean turnX;

public TicTacToe() {
    setTitle(TITLE);
    try {
        URL url = new URL("http://i.stack.imgur.com/F0JHK.png");
        icon = new ImageIcon(url);
        URL urlx = new URL("http://i.stack.imgur.com/T5uTa.png");
        iconx = new ImageIcon(urlx);
        URL urlo = new URL("http://i.stack.imgur.com/yoKxT.png");
        icono = new ImageIcon(urlo);
    } catch (Exception e) {
        e.printStackTrace();
    }
    setSize(width, height);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    content = getContentPane();
    content.setBackground(Color.black);

    content.setLayout(new GridLayout(4, 3));

    for (int i = 0; i < spaces.length; i++) {
        spaces[i] = new JButton();
        cellHandler[i] = new CellButtonHandler();
        spaces[i].addActionListener(cellHandler[i]);
    }

    exitbutton = new JButton("Exit");
    exithandler = new ExitButtonHandler();
    exitbutton.addActionListener(exithandler);
    initbutton = new JButton("Clear");
    inithandler = new InitButtonHandler();
    initbutton.addActionListener(inithandler);

    turn = new JLabel("X's turn", SwingConstants.CENTER);
    turn.setForeground(Color.white);

    for (int i = 0; i < spaces.length; i++) {
        spaces[i].setBackground(Color.white);
        content.add(spaces[i]);
    }

    content.add(initbutton);
    content.add(turn);
    content.add(exitbutton);

    init();
}

public void init() {
    turnX = true;
    gameover = false;
    spaces[0].setIcon(icon);
    spaces[1].setIcon(icon);
    spaces[2].setIcon(icon);
    spaces[3].setIcon(icon);
    spaces[4].setIcon(icon);
    spaces[5].setIcon(icon);
    spaces[6].setIcon(icon);
    spaces[7].setIcon(icon);
    spaces[8].setIcon(icon);

    turn.setText("X's turn");

    setVisible(true);

}

public static void main(String[] args) {
    TicTacToe game = new TicTacToe();
}

private class CellButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if (gameover) {
            return;
        }

        JButton pressed = (JButton) (e.getSource());

        if (pressed.getIcon().equals(icono) || pressed.getIcon().equals(iconx)) {
            return;
        }

        if (turnX) {
            pressed.setIcon(iconx);
        } else {
            pressed.setIcon(icono);
        }

        if (checkwinnerO()) {
            gameover = true;
            turn.setText("O Wins!");
            }

        if(checkwinnerX()){
            gameover = true;
            turn.setText("X Wins!");
        }
    }
}

private class ExitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

private class InitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        init();
    }
}

public boolean checkwinnerO() {
    if (spaces[0].getIcon().equals(icono)
            && spaces[1].getIcon().equals(icono) && spaces[2].getIcon().equals(icono)) {
        return true;
    } else if (spaces[3].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[5].getIcon().equals(icono)) {
        return true;
    } else if (spaces[6].getIcon().equals(icono)
            && spaces[7].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[0].getIcon().equals(icono)
            && spaces[3].getIcon().equals(icono) && spaces[6].getIcon().equals(icono)) {
        return true;
    } else if (spaces[1].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[7].getIcon().equals(icono)) {
        return true;
    } else if (spaces[2].getIcon().equals(icono)
            && spaces[5].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[0].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[2].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[6].getIcon().equals(icono)) {
        return true;
    } else

        return false;
}

public boolean checkwinnerX() {
    if (spaces[0].getIcon().equals(iconx)
            && spaces[1].getIcon().equals(iconx) && spaces[2].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[3].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[5].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[6].getIcon().equals(iconx)
            && spaces[7].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[0].getIcon().equals(iconx)
            && spaces[3].getIcon().equals(iconx) && spaces[6].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[1].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[7].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[2].getIcon().equals(iconx)
            && spaces[5].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[0].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[2].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[6].getIcon().equals(iconx)) {
        return true;
    } else

        return false;
}
}
JButton does not change icon when pressed memory game

JButton does not change icon when pressed memory game


By : Teszt Elek
Date : March 29 2020, 07:55 AM
With these it helps Use a Swing Timer instead of Thread.sleep(...). Understand that Thread.sleep(...) puts the current thread to sleep, here the Swing Event Dispatch Thread or EDT, and this will put your entire GUI to sleep, freezing your application. A Swing Timer will do one or repeated actions without tying up the EDT. For example:
code :
 public void actionPerformed(ActionEvent e) {

    Tile tile = (Tile) e.getSource();

    if (tilesFlipped < 2) {
       tile.flip();
       flippedTiles[tilesFlipped++] = tile;

       if (tilesFlipped == 1) {
          return;
       }

       if (flippedTiles[0].equals(flippedTiles[1])) {
          tilesFlipped = 0;
          return;

       } else {
          Timer timer = new Timer(1000, new ActionListener() {

             @Override
             public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 2; i++) {
                   flippedTiles[i].flipBack();
                }
                tilesFlipped = 0;
             }
          });
          timer.setRepeats(false);
          timer.start();
       }
    }

 }
Android studio can't show icon after changing icon

Android studio can't show icon after changing icon


By : user2013162
Date : March 29 2020, 07:55 AM
this one helps. When you create a new project there will be a folder with name mipmap-anydpi-v26 . If its there then the icons will be picked from it for devices with Api 26+ . Just delete this folder and app icons will will be picked from mipmap folder .
mipmap-anydpi-v26 folder is for implementation of adaptive icons introduced in Api 26 . If you want to use adaptive icon then create an adaptive icon . If your phone is above version 8.0 , adaptive icon will be picked else normal icon will be picked .
how to set jbutton icon for specific time to show when it is clicked and how to disappear the icon when time finished

how to set jbutton icon for specific time to show when it is clicked and how to disappear the icon when time finished


By : Rehan Khan
Date : March 29 2020, 07:55 AM
Hope that helps Based on my understanding of your problem, your logic is a little skewed, the actions should following along something like
Button Clicked -> Icon Changed -> Timer Started ...(waiting)... -> Timer triggered -> Icon Changed.
Related Posts Related Posts :
  • How to query using objectify in Google App Engine app
  • List value is printing address instead of value
  • StringUtils.isBlank return false for null Strings
  • Sent parameter through PATCH android
  • i am not able to clear the code below given using throwable concept in java
  • How to get System time instead of Server time in java?
  • How to push json data into an arrya object in MONGO DB by java
  • Saving RelationshipEntity not working
  • Error on "instanceOf" when checking a subClass
  • Java - Splitting a image into 4 images
  • "Pinging" a server in Java
  • RoboGuice Proguard CreationException
  • Android Array list returns Null after adding values
  • Jar Executable what am I doing wrong?
  • How do I mitigate Connection leak triggered for connection com.mysql.jdbc.JDBC4Connection@11d08960,
  • How do i iterate values from a 2d array using conditions
  • Hide Button in CustomPagerAdapter
  • Java GC: does it ever make sense to call super.finalize() in a class that only extends Object?
  • 64bit HashMap in Java
  • Hibernate wildcard query on two columns
  • Want to Generate Auto Login Program Using Java
  • Spring security Oauth 2 with ajax login and form login
  • Parse findInBackground doesn't add to global arraylist?
  • Remove random item from ArrayList
  • in SpringMVC getMessage from i18n properties in a function outside the controller
  • How do I add JCheckBox next to each name that has been entered into an arraylist from a JTextField?
  • Return node ID along with complete node
  • InvocationTargetException. Cannot cast class X to class X. When invoked in Scala Imain through spark-submit
  • how to print elements of a 1D array into a N x N box
  • How to map csv file to pojo class in java
  • How, in Java, to encode default vhost %2F in URL/URI so it doesn't get changed to slash and fail to work
  • Tomcat 5.5 https configuration in Windows Server 2008 64bit
  • Enabling FIPS 140-2 compliance mode in MULE ESB
  • How to pass String PackageName to PkgName under parseXML() and run XMLParser class as a new activity when click on list
  • Error while creating spring -hibernate configuration file
  • Hand off control between java threads
  • NoClassDefFoundError: scala/collection/Seq
  • Getting stack overflow error on one machine but not the other on the same code
  • How can I loop AsyncAjaxRequest in GWT?
  • Failed to find provider info for urbanairship.provider
  • Android ListView Adapter Crash issue/Duplicates
  • Displaying Splash screen only once on user preference
  • Loop List<WebElement> doesn't stop despite no more WebElements
  • Check if pattern exists in a String
  • Swing - Key Problems (Arrow Keys, Tab, etc.) in GUIs with TextComponents
  • Why the solution for binary search tree validity is not working?
  • Double encrypting 2048 RSA?
  • create junit test for runnable class
  • Unable to read and sort information from a text file in Java
  • My device isn't getting detected in android studio (driver issue)
  • Representing a Graph in Java
  • How to XML mapping using jxls
  • Quick Sort stackoverflow error for large arrays
  • Parsing Apache Tika XML Output returns Unknown Tag
  • java simple UI loop, can't find bug
  • how to use greater than and less than in a single if statement in java
  • Error:(27, 13) Failed to resolve: com.antonyt.infiniteviewpager:library:1.0.0
  • Gradle dependencies on source set of different projects
  • Session Scope in Restful Spring Services
  • Java: Change a value in an overridden method of a different class to a value that was calculated in the class
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co