Jump to content

JButtons Icon

- - - - -

  • Please log in to reply
5 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I have a JFrame->JPanel->JButtons.. with actionListener store the first clicked button to first JButton variable and the second clicked button to another one. My goal is to learn if those two buttons are connected together (connected means there is a "path" of specific ImageIcon on the rest buttons between the first and the second button only horizontal or vertical). I have created a method called
class boolean legalMove(JButton current)
to a second class and i would like to know how is possible to learn if exist a "path" or not between those two. If anyone has better idea, is welcomed

thank you

Edited by toto_7, 13 May 2011 - 02:11 AM.


#2
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Edited

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
What I would do is instead of using JButton, make your own class which extends JButton but which also holds its position:

public class MyButton extends JButton{

  int x;

  int y;


  public MyButton(int x, int y){

    this.x = x;

    this.y = y;

  }


  //getter and setter here

}

If you wanna check if the move is legal you can now use X and Y

MyButton btn1;

MyButton btn2;


public boolean isLegal(){

  int xDifference = btn1.getX() - btn2.getX();

  int yDifference = btn1.getY() - btn2.getY();

  

  return ( xDifference > -1 && xDifference <1 ) ^ ( yDifference > -1 && yDifference <1 )

}


Note the ^ is an exlusive OR operator, like || is OR, and && is AND.
Exclusive OR means it will return true if either the left is true, or the right is true, if both are true it's false.

false ^ false = false

false ^ true = true

true ^ false = true

true ^ true = false



#4
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Thats how im doing it until now.
public BoardPocket(int x, int y, String picture){

		super(new ImageIcon(picture));

		this.row = x;

		this.column = y;

	}
I didnt get your meaning with
( xDifference > -1 && xDifference <1 ) ^ ( yDifference > -1 && yDifference <1 )


#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, that code returns true if the move is legal:

int xDifference = btn1.getX() - btn2.getX();

int yDifference = btn1.getY() - btn2.getY();

  

return ( xDifference > -1 && xDifference <1 ) ^ ( yDifference > -1 && yDifference <1 )

Where btn1 should be the first butotn clicked, and btn2 the 2nd button clicked.

It's true on the Xs when you're at position '+' (eg, if you click first on '+', then 'X' method returns true, otherwise false.

0  X  0

X  +  X

0  X  0



#6
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Limits are from first position + to second position * the path must be with specific ImageIcon and both + and * connected vertical or horizontal
Example


00000000
00XXX*00
00X00000
00+00000
00000000




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users