Results 1 to 10 of 10
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default getting a mouse clicked method to change a previous obects properties?

    Hi, I am having trouble with a mouse click method. In short I have a hexagon grid, and every time I want to click on one particular hexagon I want the color of the hexagon to change to blue(by default the color is grey). This part works, but when I go to click on another hexagon I want the color of the previous hexagon to change back to grey while at the same time changing the color of the current hexagon clicked to blue. So in other words, I only want one hexagon blue at a time. How might I do this?
    I have a hexagon class for one particular hexagon, and a hexmap class for multiple hexagons. I know I should probably be handling mouse clicks in the hexmap since it deals with the whole thing. But I cannot say something like hexagon.setColor within a mouselistener in that class(it gives me a lot of issues). Here is some of my code, any help would be much appreciated:

    Java Code:
    public class Hexagons()
    public Hexagons(HexMap theMap, int mapRow, int mapCol, int width, int height) {
            this.map = theMap;
            this.row = mapRow;
            this.col = mapCol;
            this.width = width;
            this.height = height;
            /*
             * these values are appromimations for the vertical position of 
             * the vertices in the hex.  More exact values would be given
             * by using .23 and .77 as the multiplier for the height
             */
            theHex.addVertex(.5 * width, 0.0);  //middle top
            theHex.addVertex(width, .25 * height);  //right top
            theHex.addVertex(width, .75 * height);  //right bottom
            theHex.addVertex(.5 * width, height);  //middle bottom
            theHex.addVertex(0.0, .75 * height);  //left bottom
            theHex.addVertex(0.0, .25 * height);  //left top
            add(theHex);
            theHex.setFillColor(Color.lightGray);
            theHex.setFilled(true);
            this.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent me) {
                    String message = String.format("System(%d,%d).  Enter System?", row, col);
                    int reply = JOptionPane.showConfirmDialog(null, message, null, JOptionPane.YES_NO_OPTION);
                    //if the hexagon has been entered
                    if (reply == JOptionPane.YES_OPTION && getRow() == 0 && getCol() == 0) {
    
                        theHex.setFillColor(Color.BLUE);
                           
                    }
                    if (reply == JOptionPane.YES_OPTION && getRow() == 0 && getCol() == 1) {
                       //I want to change the color of the last hexagon to grey
                        theHex.setFillColor(Color.blue);
                    }
                    //and so on and so forth..
    
                }
    
    public class HexMap extends GCompound(){
    public HexMap(int rows, int cols, int hexWidth, int hexHeight) {
    
            this.rows = rows;
            this.cols = cols;
            hexmap = new Hexagons[rows][cols];
            blocks=new ArrayList<BlockedHexs>();
            for (int row = 0; row < rows; ++row) {
                for (int col = 0; col < cols; ++col) {
                    Hexagons hex;
                   hex= new BlockedHexs(this, row, col, hexWidth, hexHeight); 
                        hex = new Hexagons(this, row, col, hexWidth, hexHeight);
                    hexmap[row][col] = hex;
                    add(hex, col * hexWidth + (row % 2 == 1 ? .5 * hexWidth : 0.0), row * hexHeight * .75);
    }
    Last edited by Snake006; 05-19-2014 at 10:51 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: getting a mouse clicked method to change a previous obects properties?

    I can only give general guidance since you code is incomplete. Declare a variable called previousHex and set it to null. Then,
    when you change the color to blue, first check to see if previousHex is null. If not, change its color to grey. Then change currentHex to blue and assign currentHex to previousHex.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: getting a mouse clicked method to change a previous obects properties?

    Oh, I left a lot of things out of my code in order to specifically show what I was trying to do(its functional but just doesn't do what I want yet). But thanks though I had not considered doing that yet, though I have tried something similar with Boolean values.

  4. #4
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: getting a mouse clicked method to change a previous obects properties?

    This seems like the right idea, however it still doesn't revert the hexagon back to grey

    prevHex=null;
    if(prevHex!=null){
    theHex.setFillColor(Color.lightGray);

    }
    else if(prevHex==null){
    theHex.setFillColor(Color.BLUE);
    prevHex=theHex;
    }

    }
    p.s you all probably already know this, but theHex is a GPolygon (it is final) and so is the prevHex
    Last edited by Snake006; 05-19-2014 at 11:35 PM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: getting a mouse clicked method to change a previous obects properties?

    When you declare prevHex, you set it to null, only once at the very beginning. And prevHex cannot be final.

    Java Code:
    if(prevHex != null) {
        prevHex.setFillColor(Color.lightgray);
    }
    theHex.setFillColor(Color.BLUE);
    prevHex = theHex;
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: getting a mouse clicked method to change a previous obects properties?

    Java Code:
    if(prevHex != null) {
        prevHex.setFillColor(Color.lightgray);
    }
    theHex.setFillColor(Color.BLUE);
    prevHex = theHex;



    So prevHex declared null in the constructor right at the beginning and is not final(I made those changes), however despite that, the above code unfortunately will not reset the hexagon to display grey. The best I can do is that if the same hexagon is clicked twice then it will change back to grey then but it will not change back to grey if another hexagon is clicked.
    for instance if another hexagon is clicked, for example,then this is the code for the hexagon being clicked
    Java Code:
    if (reply == JOptionPane.YES_OPTION && getRow() == 0 && getCol() == 1)  {
                        if (prevHex != null) {
                            prevHex.setFillColor(Color.lightGray);
    
                        }
                        prevHex = theHex;
                        theHex.setFillColor(Color.YELLOW);
                        prevHex = theHex;
    I'm guessing I can't write the same code twice for two different hexagons?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: getting a mouse clicked method to change a previous obects properties?

    Well, like I said, it's hard to help without seeing all the logic. And you shouldn't post a lot of code here. If you can make an Short, Self Contained, Correct Example that demonstrates the problem, then I may be able to help. But it must be totally compilable and runnable.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: getting a mouse clicked method to change a previous obects properties?

    You added the prevHex variable to the mouaeListener class you generated?
    Each hex has its own instance so each hex has its own prevHex which you do not want.
    You could make it static so all instances share the same variable.

    Or did you try that already?

    Konrad

  9. #9
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: getting a mouse clicked method to change a previous obects properties?

    Actually I haven't tried that yet. I will give that a go

  10. #10
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    127
    Rep Power
    0

    Default Re: getting a mouse clicked method to change a previous obects properties?

    OMG that worked! Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 07-16-2012, 05:08 AM
  2. Replies: 1
    Last Post: 02-11-2012, 04:07 AM
  3. If i change a system properties...
    By Max-Power in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 11:22 PM
  4. Replies: 4
    Last Post: 02-23-2011, 10:25 PM
  5. X & Y of the following event "Mouse clicked"
    By jack DANIEL's in forum Advanced Java
    Replies: 2
    Last Post: 10-12-2010, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •