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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:57:34+00:00 2026-05-12T20:57:34+00:00

Here are my for() loops : public void showMovementCase(){ int movePlusAttack = moveAllowed+attackDistance; int

  • 0

Here are my for() loops :

public void showMovementCase(){
    int movePlusAttack = moveAllowed+attackDistance;
    int twiceMoveAllowed = (moveAllowed)*2;
    for(int i = 0; i <= movePlusAttack*2; i++){
        for(int j = 0; j <= movePlusAttack*2;j++){
            boolean a = movePlusAttack <= j+i && movePlusAttack >= j-i && i <= movePlusAttack;
            boolean b = movePlusAttack <= j+i && movePlusAttack >= i-j && i > movePlusAttack && j <= movePlusAttack;
            boolean c = movePlusAttack*3 >= j+i && movePlusAttack >= j-i && i > movePlusAttack &&  j >= movePlusAttack;
            if(a || b || c){
                try{
                    actionSquare[i][j] = new JLabel();
                    actionSquare[i][j].setIcon(redsquare);
                    actionSquare[i][j].setBounds(sprite.getX()+(i-movePlusAttack)*16,sprite.getY()+(j-movePlusAttack)*16, 16, 16);
                    panel.add(actionSquare[i][j], new Integer(1));
                }
                catch(ArrayIndexOutOfBoundsException e){System.out.println("red :" + e);}
            }
        }
    }
    for(int x = 0; x <= twiceMoveAllowed; x++){
        for(int y = 0; y <= twiceMoveAllowed;y++){
            boolean a = moveAllowed <= y+x && moveAllowed >= y-x && x <= moveAllowed;
            boolean b = moveAllowed <= y+x && moveAllowed >= x-y && x > moveAllowed && y <= moveAllowed;
            boolean c = moveAllowed*3 >= y+x && moveAllowed >= y-x && x > moveAllowed &&  y >= moveAllowed;
            if(a || b || c){
                try{
                    actionSquare[x][y].setIcon(bluesquare);
                    System.out.println("Coucou !");
                    actionSquare[x][y].addMouseListener(mouse);
                    panel.repaint();
                    panel.revalidate();
                }
                catch(ArrayIndexOutOfBoundsException e){System.out.println("blue :" + e); }
            }
        }
    }
}

if this.attackDistance is different of 0, then the second loop doesn’t work (it seems to stop at the .setIcon() command).

Do you know a way to fix this ?

Thanks for reading.

Edit:

with :

                try{
                    actionSquare[x][y].setIcon(bluesquare);
                    System.out.println("Coucou !");
[...]
                }

On the second loop, nothing is printed.

but with :

            try{
                System.out.println("Coucou !");
                actionSquare[x][y].setIcon(bluesquare);

[…]
}

“Coucou !” is printed once.
That’s why I said that “it seems to stop at the .setIcon() command” I should have said that sooner, sorry.

  • 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-12T20:57:35+00:00Added an answer on May 12, 2026 at 8:57 pm

    I cleaned up your code for you. Generally, when you have two sections of code that are supposed to be doing the exact same thing, but are not, then rolling them into one method can eliminate that possibility.

    public void showMovementCase(){
        // probably want to remove anything left over from the last invocation
        panel.removeAll();
        for (JLabel[] array : actionSquare) Arrays.fill(array, null);
         
        colorSquares(moveAllowed + attackDistance, redsquare, null);
        colorSquares(moveAllowed * 2, bluesquare, mouse);
         
        for (int x = 0; x < actionSquare.length; x++)
            for (int y = 0; y < actionSquare[x].length; y++)
                if (actionSquare[x][y] != null) panel.add(actionSquare[x][y], 1);
    }
     
    private void colorSquares(int move, Icon color, MouseListener mouse) {
        int xMax = Math.min(2 * move, actionSquare.length);
        int yMax = Math.min(2 * move, actionSquare[0].length);
        for (int x = 0; x < xMax; x++) {
            for (int y = 0; y < yMax; y++) {
                if (isLegal(x, y, move)) {
                    if (actionSquare[x][y] == null)
                        actionSquare[x][y] = new JLabel();
                    actionSquare[x][y].setIcon(color);
                    actionSquare[x][y].setBounds(
                            sprite.getX() + (x - move) * 16,
                            sprite.getY() + (y - move) * 16, 16, 16 );
                    if (mouse != null) actionSquare[x][y].addMouseListener(mouse);
                }
            }
        }
    }
     
    private static boolean isLegal(int x, int y, int move) {
        // informative comment explaining why this mess makes sense
        if (move <= y+x && move >= y-x && x <= move) return true;
        // informative comment explaining why this mess makes sense
        if (move <= y+x && move >= x-y && x > move && y <= move) return true;
        // informative comment explaining why this mess makes sense
        if (move * 3 >= y+x && move >= y-x && x > move &&  y >= move) return true;
         
        return false;
    }

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

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What programming paradigm Message passing. and to what particular practical… May 13, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer module-starter can be invoked thus, to create all the modules… May 13, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer For a native date picker, you can use the DateField,… May 13, 2026 at 1:15 am

Related Questions

I have two classes, one for articles and another for the cart. The cart
Hey, I'm currently trying to send an image file to a web server using
What I'm trying to do is to write a dedicated method for my StreamWriter
I've read some of the questions regarding anemic domain models and separation of concerns.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.