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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:52:06+00:00 2026-05-22T15:52:06+00:00

This is fragment of class in my program (I deleted some not important code).

  • 0

This is fragment of class in my program (I deleted some not important code). At First, program always calls method firstSet() which sets all JLabels(players) on JPanel(board). Then when I click on JLabel, mouseClicked() runs method findPlayer(), so I know which JLabel whose chose. To this moment everything is OK.

Problem begins when I start simulation – method simulationStart() run 10 times. After this when I click player, cmd line shows: I can't find it :(. I wrote some system.out to find out what is wrong. Problem is in fields locationX and locationY, I don’t know why they are different in findPlayer() and simulationStart().

public class setBoard extends JFrame implements MouseListener {

    int[] locationX = new int[100];
    int[] locationY = new int[100];

    public void setLocation() {
        for (int i = 0; i < 100; i++) {
            locationX[i] = (int) (random() * (sizeX - xy));
            locationY[i] = (int) (random() * (sizeY - xy));
        }
    }

    public ArrayList<JLabel> firstSet() {
        setLocation();

        for (int i = 0; i < 100; i++) {
            player = new JLabel();

            //
            //some code to JLabel set
            //

            playerList.add(player);
            player.setBounds(locationX[i], locationY[i]);
            player.addMouseListener(this);
        }

        return playerList;
    }

    public ArrayList<JLabel> simulationStart(ArrayList<JLabel> playerList) {
        this.playerList = playerList;

        setLocation();

        for (int i = 0; i < 100; i++) {
            playerList.get(i).setBounds(locationX[i], locationY[i]);
            playerList.add(playerList.get(i));
        }
        return playerList;
    }

    public void mouseClicked(MouseEvent e) {     
        //
        //a lot of code to  search coursorX and coursorY, this works good
        //

        //if left mouse button 
        if (e.getButton() == 1) {
            final int playerNr = findPlayer(coursorX, coursorY);
            if (playerNr == -1) {
                System.out.println("I can`t find it :( ");
            }
            else{
                //
                //code to do when it find player
                //
            }
        }
    }

    //
    //
    //

    private int findPlayer(int x, int y) {
        int playerNr = -1;
        for (int i = 0; i < 100; i++) {
            if (x == locationX[i] && y == locationY[i]) {
                playerNr = i;
            }
        }
        return playerNr;
    }

}
  • 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-22T15:52:07+00:00Added an answer on May 22, 2026 at 3:52 pm

    Whenever you call simulationStart you are adding players to the playerList again. So the number of players goes above 100. Why don’t you try printing out the size of playerList and check if it is what you expect?

    I think you may need to remove the statement: playerList.add(playerList.get(i));

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

Sidebar

Related Questions

For the following code fragment. /*This program demonstartes how a virtual table pointer *
In a code review, I stumbled over this (simplified) code fragment to unregister an
This is my first post here and I wanted to get some input from
Consider the following code: class Program { static void Main(string[] args) { try {
I am trying to understand a code, here is fragment which is causing confusion:
I have the following C# code fragment: using System; class count { public static
This is a fragment from Exceptional C++ Item 24, Solution, first bullet from the
I've got a vanilla .net class library which contains some types which are [ComVisible]
Considering this code fragment: struct My { operator const char*()const{ return my; } }
I have this code. class MyParser < Nokogiri::XML::SAX::Document def characters(string) LOG.debug(characters #{string}) end def

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.