Getting the source of a mouse click

I have a Battleship game I am programming and have only one problem. What the program does is to have one array that is visible that will look like the ocean. The array consists of JLabels with icons on them. I have a second array that is not visible that is JLabels with icons that has all hits and then I place the ship icons over them where the ships are located. I am having trouble with comparing the JLabel that was clicked on with the location of that label so I can access the hidden array to get the icon at that location and place it in the location of the JLabel that was clicked on. The code seems to work down to when I click on a JLabel the mouseclicked method is called and I see that it is going through the loops fine, but the if statement for comparison never equates to true. Here is my code and the code I am referencing is in the mouseclicked method:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Battleship extends JApplet implements MouseListener
     private static final int MAXSHIPS = 14;
     private static final int GRIDSIZE = 16;
     private JPanel pnlPlayer = new JPanel();
     private JLabel[][] lblPlayer = new JLabel[16][16];
     private ImageIcon[] imgShips = new ImageIcon[10];
     private JLabel[][] hImgShips = new JLabel[16][16];
     private ShipInfo[] shipInfo = new ShipInfo[8];
     private char[][] ocean = new char[16][16];
     Container content = this.getContentPane();
     public void init()
          initOcean();
          initHidShips();
          createPlayerPanel();
          createShips();
          content.add(pnlPlayer);
          this.setSize(310, 310);
          placeShips();
     private void createPlayerPanel()
          pnlPlayer.setLayout(new GridLayout(16,16,3,3));
          pnlPlayer.setBackground(Color.blue);
          pnlPlayer.setSize(301,301);
          for(int row = 0; row < 16; row++)
               for(int col = 0; col < 16; col++)
                    lblPlayer[row][col] = new JLabel(new ImageIcon("Images/batt100.gif"));
                    lblPlayer[row][col].setOpaque(true);
                    lblPlayer[row][col].setSize(16,16);
                    pnlPlayer.add(lblPlayer[row][col]);
                    addMouseListener(this);
     private void createShips()
          loadShipImages();
          createShipInfo();
     private void loadShipImages()
          for(int i = 0; i < 10 ; i++)
               imgShips[i] = new ImageIcon("Images/batt" + (i + 1) + ".gif");               
     private void createShipInfo()
//          Start with the frigate, we create 2 of them here but will place 3 total randomly it as two images
          int[] frigateH = {0,4};
          shipInfo[0] = new ShipInfo("Frigate", frigateH, 'H');
          int[] frigateV = {5,9};
          shipInfo[1] = new ShipInfo("Frigate", frigateV, 'V');
// Create the mine Sweep it has 3 pieces
          int[] mineSweepH = {0,1,4};
          shipInfo[2] = new ShipInfo("Minesweep", mineSweepH, 'H');
          int[] mineSweepV = {5,6,9};
          shipInfo[3] = new ShipInfo("Minesweep", mineSweepV, 'V');
          int[] cruiserH = {0,1,2,4};
          shipInfo[4] = new ShipInfo("Cruiser", cruiserH, 'H');
          int[] cruiserV = {5,6,7,9};
          shipInfo[5] = new ShipInfo("Cruiser", cruiserV, 'V');
          int[] battleShipH = {0,1,2,3,4};
          shipInfo[6] = new ShipInfo("Battleship", battleShipH, 'H');
          int[] battleShipV = {5,6,7,8,9};
          shipInfo[7] = new ShipInfo("Battleship", battleShipV, 'V');
     private void initOcean()
          for(int row = 0; row < 16; row++)
               for(int col = 0; col < 16; col++)
                    ocean[row][col] = 'O';
     private void initHidShips()
          for(int row = 0; row < 16; row++)
               for(int col = 0; col < 16; col++)
                    hImgShips[row][col] = new JLabel(new ImageIcon("Images/batt102.gif"));
                    //addMouseListener(this);
          //placeShips();
     private void placeShips()
          // Create a Random object to select ships
          Random r = new Random();
          // Create random objects to place the ship at a row and a column
          Random randCol = new Random();
          Random randRow = new Random();
          //Place the ships, typically there are 14
          for(int ships = 0; ships < MAXSHIPS; ships++)
               //Get a random ship
               ShipInfo si = shipInfo[r.nextInt(8)];
               int row = randRow.nextInt(16);
               int col = randCol.nextInt(16);
               int direction = checkDirection(si, row, col);
               while(direction == 0) // 0 direction says that we can not place the ship
                    row = randRow.nextInt(16);
                    col = randCol.nextInt(16);
                    direction = checkDirection(si, row, col);
               // got a clear path, let put the ship on the ocean
               int shipPieces[] = si.getShipPieces();
               if(si.Direction == 'H') // place horizontal
                    if(direction == 1)
                         for(int i = col, j = 0; i < col + si.length(); i++, j++)
                              hImgShips[row].setIcon(imgShips[shipPieces[j]]);
                              String name = si.getName();
                              ocean[row][i] = name.charAt(0);
                    else
                         for(int i = col + si.length(), j = 0 ; i > col; i--, j++)
                              hImgShips[row][i].setIcon(imgShips[shipPieces[j]]);     
                              String name = si.getName();
                              ocean[row][i] = name.charAt(0);
               else // Must be vertical direction
                    if(direction == 1) // place pieces in positive direction
                         for(int i = row, j = 0; i < row + si.length(); i++, j++)
                              hImgShips[i][col].setIcon(imgShips[shipPieces[j]]);     
                              String name = si.getName();
                              ocean[i][col] = name.charAt(0);
                    else
                         for(int i = row + si.length(), j = 0; i > row; i--, j++)
                              hImgShips[i][col].setIcon(imgShips[shipPieces[j]]);     
                              String name = si.getName();
                              ocean[i][col] = name.charAt(0);
     int checkDirection(ShipInfo si, int row, int col)
          if(si.Direction == 'H')
               return checkHorizontal(si, row, col);
          else
               return checkVertical(si, row, col);
     int checkHorizontal(ShipInfo si,int row, int col)
          boolean clearPath = true;
          int len = si.length();
          System.out.println(len);
          for(int i = col; i < (col + si.length()); i++)
               if(i >= GRIDSIZE) //This would put us outside the ocean
                    clearPath = false;
                    break;
               if(ocean[row][i] != 'O') // Ship already exists in this spot
                    clearPath = false;
                    break;
          if(clearPath == true) // ok to move in the positive direction
               return 1;
          //Next Chec the negative direction
          for(int i = col; i > (col - si.length()); i--)
               if(i < 0) //This would put us outside the ocean
                    clearPath = false;
                    break;
               if(ocean[row][i] != 'O') // Ship already exists in this spot
                    clearPath = false;
                    break;
          if(clearPath == true) //Ok to move in negative direction
               return -1;
          else
               return 0; // No place to move               
     int checkVertical(ShipInfo si,int row, int col)
          boolean clearPath = true;
          int len = si.length();
          System.out.println(len);
          for(int i = row; i < (row + si.length()); i++)
               if(i >= GRIDSIZE) //This would put us outside the ocean
                    clearPath = false;
                    break;
               if(ocean[i][col] != 'O') // Ship already exists in this spot
                    clearPath = false;
                    break;
          if(clearPath == true) // ok to move in the positive direction
               return 1;
          //Next Check the negative direction
          for(int i = row; i > (row - si.length() ); i--)
               if(i < 0) //This would put us outside the ocean
                    clearPath = false;
                    break;
               if(ocean[i][col] != 'O') // Ship already exists in this spot
                    clearPath = false;
                    break;
          if(clearPath == true) //Ok to move in negative direction
               return -1;
          else
               return 0; // No place to move               
     public void mouseClicked(MouseEvent e)
          for(int row=0; row<16; row++)
               for(int col=0; col<16; col++)
                    //if(lblPlayer.equals(e.getSource()))
                    if(e.getSource() == lblPlayer[row][col])
                         lblPlayer[row][col].setIcon(hImgShips[row][col].getIcon());
     public void mouseEntered(MouseEvent e){}
     public void mouseExited(MouseEvent e){}
     public void mousePressed(MouseEvent e){}
     public void mouseReleased(MouseEvent e){}
I hope someone can help me. Thanks.

in your first post, on the top right, next to the orange envelope, there is a paper with a pencil in it. That is what you click to edit.
And for the code button, you have to highlight everything you want in "code mode" and then press it.
Alternatively, you can just put (code) and the beginning of your code and one at the end, except replace the ( and ) with { and }.

Similar Messages

  • How do I Get the value from a mouse click - on a waveform graph?

    If I have made a plot into a Waveform Graph and later want to do a zoom of my data
    (Not zoom into the Waveform Graph, but regenerate the data). How do I read the mouse
    coordinate if I click on the graph window. I know how to put up the horiz and vert
    cursors but don't know how to just read the mouse click. I would really like to
    follow the windows standard that identifys a rectangle by clicking and draging and then
    be able to read the corners of the rectangle. Thanks, Rick
    PS: Using Labview 6i

    I would recommend to 'translate' your graph in a picture and dislay it in a picture control (see picture examples in LV6).
    Once you did it, pictures have an extremely useful property called Mouse that returns the mouse coordinates and click events when you place the cursor on the picture.
    By this you can re-arrange the graph on picture appearance.
    There are also other methods such as using a Window's API that returns the mouse position referred to the whole screen window, but I believe this would be much more difficult to implement.
    Let me know if this was clear and if you need an example vi.
    Good luck,
    Alberto

  • How to get the source code of UI elements ?

    What is the way to get the source code of the elements used in qUnit Page for sap.m.List and all sap.m List Items ? I am looking to implement the same in my applications ?

    Hi Micheal,
    You can see the source code here for controls in sap.m.
    sap.m Explored
    Also for the link you provided, You can right click on the page and click on the View Page Source will show you the source code.
    Regards,
    KK

  • How can i get the source code from java concurrent program in R12

    Hi 2 all,
    How can i get the source code from java concurrent program in R12? like , "AP Turnover Report" is java concurrent program, i need to get its source code to know its logic. how can i get its source code not the XML template?
    Regards,
    Zulqarnain

    user570667 wrote:
    Hi 2 all,
    How can i get the source code from java concurrent program in R12? like , "AP Turnover Report" is java concurrent program, i need to get its source code to know its logic. how can i get its source code not the XML template?
    Regards,
    ZulqarnainDid you see old threads for similar topic/discussion? -- https://forums.oracle.com/forums/search.jspa?threadID=&q=Java+AND+Concurrent+AND+Source+AND+Code&objID=c3&dateRange=all&userID=&numResults=15&rankBy=10001
    Thanks,
    Hussein

  • I upgraded to OS Yosemite on my MacBook Air...and I cannot upgrade the Adobe Flash download that Adobe keeps asking me to download. I get the screen and when I click on the icon it just creates a new window but not download. Thoughts?

    I upgraded to OS Yosemite on my MacBook Air...and I cannot upgrade the Adobe Flash download that Adobe keeps asking me to download. I get the screen and when I click on the icon it just creates a new window but not download. Thoughts?

    I just tried the link and it is just fine. If the GateKeeper pops up then open Security & Privacy preferences, click on the General tab, set the last radio button to Anywhere.

  • How to programmatically get the source for a class provided the class name?

    Hello,
    As a quick background, I am providing some tools to potential users of an in-house framework. One is the ability to generate quick prototypes from our existing demo applications. Assume a user downloads our jars and uses them in their project (we are using Eclipse, but that detail should not greatly affect my question). Included in the jars is a demos package that contains ready-to-run classes that serve to exhibit certain functionality. Since many users may just need quick extensions of these demos, I am trying to provide a way for them to be able to create a new project that starts with a copy of the demo class.
    So, the user is provided a list of the existing demos (each one uses a single class). When the user makes their selection, with the knowledge of our framework, I can translate that into what demo class they need (returned as a string of format package.subpack1.subpackn.DemoClassName). What I now want to do is to use that complete class name to get the source (look up the file) for the corresponding class, and copy it into to a new file in their project (the copying into the project can be done easily in Eclipse, so what I need help with is the bolded part). Is there a simple way to get the source given a class path for a class as described above? You may assume the source files are included in the jars for the framework.
    Thanks in advance.

    If there's a file named "package.subpack1.subpackn.DemoClassName.java" in a "demos" directory in the jar, then yes. You'd just use
    InputStream code = getResourceAsStream("/demos.package.subpack1.subpackn.DemoClassName.java");Or if those dots in the name actually separate directory names, i.e. you have a "package" directory under "demos" and a "subpack1" director under that and so on, then:
    InputStream code = getResourceAsStream("/demos/package/subpack1/subpackn/DemoClassName.java");

  • Getting the Source File name Info into Target Message

    Hi all,
    I want to get the Source file name Info into Target message of one of the fields.
    i followed Michal BLOG /people/michal.krawczyk2/blog/2005/11/10/xi-the-same-filename-from-a-sender-to-a-receiver-file-adapter--sp14
    Requirement :
    1) I am able to get the Target file name as same as the source file name when i check the ASMA in Sender & Receiver Adapter , with out any UDF...............this thing is OK
    2) I took One field extra in the target structure Like "FileName" & I mapped it Like
                              Constant(" " )--UDF-----FileName
    I Checked the Option ASMA in Both Sender & Receiver Adapters
    Here iam getting the Target File name as same as Source file name + Source File name Info in the Target Field " FileName".
    I Dont want to get the Target File name as same as Source file name. I want like Out.xml as Target file name.
    If i de-select the Option ASMA in Adapters means it is showing " null" value in the target field "FileName".
    Please Provide the Solution for this
    Regards
    Bopanna

    Hi All,
    Iam able to do this by checking the Option ASMA in only sender adapter itself
    Regards
    Bopanna

  • Where to get the source code of the owa_cookie.send procedure?

    I want to know where you get the source code of the owa_cookie.send procedure? I can only get the header of this package from ...\RDBMS\ADMIN\pubcook.sql. Could you paste the whole source code here and then I can modify it to set the 'httponly' attribute? I also have a problem that after changing the package, how to use the package I changed? Does it still a system package here? Or it has been changed to an user-defined package in database and I need to execute the package on the database for the web server?
    Thank you!

    Hi Arun,
    I am unable to see the pcui_gp components in the DTR ,I require this in order to get the source code of one of its component.
    Can you please tell me the step by step procedure getting those pcui_gp components from J2ee engine to the  dtr or  NWDI.
    If there are any documents on pcui_gp components exclusively please do forward to my mail id [email protected]
    Thansk and Regards,
    Anand.

  • How to get the source code of Java Concurrent Program?

    Hi,
    How to get the source code of Java Concurrent Program?
    Example
    Programe Name:Format Payment Instructions
    Executable:Format Payment Instructions
    Execution File Name:FDExtractAndFormatting
    Execution File Path:oracle.apps.iby.scheduler
    Thanks in advance,
    Senthil

    Go on Unix box at $JAVA_TOP/oracle/apps/iby/scheduler
    You will get class file FDExtractAndFormatting.
    Decompile it to get source code.
    Thanks, Avaneesh

  • Is it possible to get the source code of sun package in JDK1.4?

    If i want to review the code of the HTTP protocol handler and some other things.
    how can i get the source. or is it possible?
    thank you.

    See this post if you can figure out something out of it ....
    http://forum.java.sun.com/thread.jsp?forum=31&thread=391451

  • How to get the source code of a PRT application in the portal

    Hi!
    Does anybody know how to get the source code of a PRT application in the portal?
    Thanks in advance,
    Celso

    Celso,
    If its Java-based code have a look at the properties of an iView that belongs to the application in question and copy the value of the Code Link parameter e.g. 'com.sap.pct.hcm.rc_vacancyrequestov.default'.
    Search the Portal installation directory under /j233/cluster/server/ for a .par.bak file of the same name, removing .default from the codelink parameter
    e.g. com.sap.pct.hcm.rc_vacancyrequestov.par.bak
    Copt this locally and import into Netweaver Developer Studio. You will have to decompilte the class files with a decompiler such as DJ Decompiler or Cavaj (search with Google).
    Cheers,
    Steve

  • How to get the source code of an HTML page in Text file Through J2EE

    How to get the source code of an HTML page in Text file Through J2EE?

    Huh? If you want something like your browser's "view source" command, simply use a URLConnection and read in the data from the URL in question. If the HTML page is instead locally on your machine, use a FileInputStream. There's no magic invovled either way.
    - Saish

  • How to get the source code of an HTML page in Text file Through java?

    How to get the source code of an HTML page in Text file Through java?
    I am coding an application.one module of that application is given below:
    The first part of the application is to connect our application to the existing HTML form.
    This module would make a connection with the HTML page. The HTML page contains the coding for the Form with various elements. The form may be a simple form with one or two fields or a complex one like the form for registering for a new Bank Account or new email account.
    The module will first connect through the HTML page and will fetch the HTML code into a Text File so that the code can be further processed.
    Could any body provide coding hint for that

    You're welcome. How about awarding them duke stars?
    edit: cheers!

  • Where to  find the pcui_gp  components ,How to get the source code of those

    Hi All,
    Can anybody tell the exact location wher the pcui_gp components will be stored if they are  not appearing.
    And another question is how to get the source code of the pcui_gp for customization.
    anybody working on these compoents please help me.
    answers will be rewarded.
    thanks and regards,
    anand

    Hi Arun,
    I am unable to see the pcui_gp components in the DTR ,I require this in order to get the source code of one of its component.
    Can you please tell me the step by step procedure getting those pcui_gp components from J2ee engine to the  dtr or  NWDI.
    If there are any documents on pcui_gp components exclusively please do forward to my mail id [email protected]
    Thansk and Regards,
    Anand.

  • How to get the source of a strange posted pic into my camera roll?

    I got a strange pic put into my camera roll, this pic is most likely put by an app which has an access to my photo gallery, all I want to know is how to get the source which put this image into my camera roll, I have a punch of apps that have a photo access grant and really don't know to disable all of these apps because of only one of them.
    p.s: the apps with photo access in my device (ProCam, CameraArtFXFree, Photo Editior-, Instagram, Poster++, Photo Vault, Facebook, Tango, Viber, Y! Messenger, Ipadio, Line, WhatsApp) And the only opened apps when this photo pushed to my Camera Roll were (Viber, Tango, Line, Whatsapp).
    Thanks in advance.

    >
    Nitesh Kumar wrote:
    > Hi,
    >
    > FM to get the program source code: RPY_PROGRAM_READ
    >
    > By using this FM you can get the program name(say report_name) and then you can use
    >
    > READ REPORT report_name INTO itab
    >
    > Thanks
    > Nitesh
    u dont need the last statement the FM itself returns an itab with code in it.

Maybe you are looking for

  • How to create multiple instances within same AS

    Ramesh, I have a unique problem. I might have to create B2B instances for 4 owners. As in BPEL, I can have separate domains for the 4 clients in the same instance; what is the equivalent in B2B? I have a limited number of CPUs and need to install the

  • Problem with jar file

    Hello, I have create successfull an executeable jar and it orks fine on my pc I try to run it on other pc whoe winrar is installed the jar can not run and the jar will be extract who is the problem ??????

  • Upgrading Powerbook G4 from USB 1 to USB 2

    Can someone tell me is it possible to upgrade a Powerbook G4 15" which is fitted with two USB1 ports to USB2, please? I would probably take it to an Apple Store rather than attempt it myself. Thank you.

  • WebDynpro : BIApplicationFrame : Help needed

    Hello Experts, I need your urgent help. I have created a web template in Web Application Designer. In this template I have created a data provider which is based on a query. If I execute this query directly from WAD then it executes successfully. How

  • Portal confused by window.open javascript command.

    To all, I am having difficulty trying to implement a javascript popup calendar. I am calliong a simple html page i have stored in avirtual direcytory on the web server. I have also moved it to the apache default html directory. When i issue the windo