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 6049199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:32:55+00:00 2026-05-23T07:32:55+00:00

Okay so I am pretty much doing a crash course through creating a java

  • 0

Okay so I am pretty much doing a crash course through creating a java application and am having an issue with executing an action. The program is the 15 puzzle, the game where you slide one piece at a time and try to get all numbers in order, so I allow for an ‘Auto’ mode option that will solve the board for the user once clicked. So my code reads the solution from a text file which is working fine just none of the ‘squares’ (JButtons) move when I click the auto button. So i am not sure if I just don’t understand the action even process completely or not. heres my code, I can supply more of it if necessary.

if (e.getSource() == ctrButtons[0]) {
      System.out.println("Auto Mode started\n");
      Scanner s = null;

      try {
        s = new Scanner(new BufferedReader(new FileReader("move_list.txt")));
        int count = 0;
        while (s.hasNext()) {
          //Cycle through to move in move_list
          if (count != 18) {
            s.next();
            count+=1;
          }
          else {
            int cur_move = Integer.parseInt(s.next());
            count = 0;
            /*Use cur_move to move blank space accordingly
             *UP------------3
             *LEFT----------2
             *RIGHT---------1
             *DOWN----------0
            */
            int zero_index = -1;
            for (int j=0; j<jbnButtons.length; j++) {
              if (Integer.parseInt(jbnButtons[j].getText()) == 0) {
                zero_index = j;
                break;
              }
            }

            Point zero = jbnButtons[zero_index].getLocation();

            //Check if move is up
            if (cur_move == 3) {
              Point next = jbnButtons[zero_index-4].getLocation();
              jbnButtons[zero_index].setLocation(next);
              jbnButtons[zero_index-4].setLocation(zero);
            }
//Check if move is left
                else if (cur_move == 2) {
                  Point next = jbnButtons[zero_index-1].getLocation();
                  jbnButtons[zero_index].setLocation(next);
                  jbnButtons[zero_index-1].setLocation(zero);
                }

            //Check if move is right
            else if (cur_move == 1) {
              Point next = jbnButtons[zero_index+1].getLocation();
              jbnButtons[zero_index].setLocation(next);
              jbnButtons[zero_index+1].setLocation(zero);
            }

            //Check if move is down
            else {
              System.out.println("Current move = 0");
              Point next = jbnButtons[zero_index+4].getLocation();
              jbnButtons[zero_index].setLocation(next);
              jbnButtons[zero_index+4].setLocation(zero);
            }

          }
        }
      }

So my code executes when I click the ‘Auto’ button and I was printing output to the screen to see if it was looping through the code which it was, just none of the buttons move each time through the loop. Any ideas??

  • 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-05-23T07:32:55+00:00Added an answer on May 23, 2026 at 7:32 am

    You code is executing on the Event Dispatch Thread. The GUI can’t repaint itself until the code finishes executing, so you won’t see the intermediate steps only the final location of each component.

    Read tje section from the Swing tutorial on Concurrency for a more complete explanation.

    Maybe you should use a Swing Timer (the tutorial also has a section on this). Each time the Timer fire you do the next move.

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

Sidebar

Related Questions

No related questions found

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.