Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9176129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:02:06+00:00 2026-06-17T17:02:06+00:00

I am coding an image puzzle game and one part of the code is

  • 0

I am coding an image puzzle game and one part of the code is to compare the pieces the user has selected to the pieces of the correct image.

Each image piece is already added to a JButton as an ImageIcon.

An identifier is required to differentiate each image piece apart and also for comparision.

I am setting a setName() for each JButton created as the identifier.

The comparison starts when the user releases the mouse after he drags the puzzle pieces from the original 3×3 grid where the shuffled pieces are to the other 3x grid for matching.

I have problems removing the error from the comparison if statement.

I got the comparison idea from this SO thread – link

    private JButton[] button = new JButton[9];
    private JButton[] waa = new JButton[9];

    private String id;
    private int cc;
    private String id2;
    private int cc2;

    // setName for each of the 9 buttons in the original 3x3 grid being created 
    // which stores the shuffled puzzle pieces
    for(int a=0; a<9; a++){
        button[a] = new JButton(new ImageIcon());
        id += Integer.toString(++cc);
        button[a].setName(id); 
    }

    // setName for each of the 9 buttons in the other 3x3 grid  
    // where the images will be dragged to by the user
        for(int b=0; b<9; b++){
        waa[b] = new JButton();
        id2 += Integer.toString(++cc2);
        waa[b].setName(id2); 
    }

    // check if puzzle pieces are matched in the correct place
    // compare name of original 'button' array button with the name of 'waa' array buttons 
        button[a].addMouseListener(new MouseAdapter(){

            public void mouseReleased(MouseEvent m){
                if(m.getbutton().getName().equals (waa.getName())){

                    }
                    else{
                         JOptionPane.showMessageDialog(null,"Wrong! Try Again.");
                    }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:02:07+00:00Added an answer on June 17, 2026 at 5:02 pm

    In your mouseReleased event m.getButton() is returning the mouse button that was clicked. You’ll want to do something more like this that will get you closer:

    if (m.getComponent().getName().equals(waa.getName())) {
    

    m.getComponent() returns the Component object (your JButton) that the event was fired from. From there you can do the comparison with the getName approach you are using.

    There’s an additional issue in that your waa variable is an array. I’m not sure how you want to compare them, whether running through the arrays and making sure the index and names match, but that’s an additional issue you need to look into.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coding an image puzzle game and one part of the code is
I'm coding a Qt 4.8 program ( http://code.google.com/p/image-feature-detector/ ) with NetBeans 7.1 in Kubuntu
I was coding a snake game, and i got an apple image to use
I am doing a image processing project(small one). I am coding algorithms in matlab
i have following coding to resize image and than save it my virtual folder
I have this peice of coding which simply swaps an image when a link
i m really very fed up coding translate animation. actually my image is moving
I'm coding in c++, and I'm trying to load an image file asynchronously. After
I am coding a galery in jquery. I have an image zoom tool that
I am coding an iPhone application where images are transferred from one iPhone to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.