Java Reference
In-Depth Information
import
java .awt.geom.
∗
;
class
Square
extends
Rectangle2D .Double
{
private boolean
isX =
false
;
private boolean
isO =
false
;
public boolean
isCharacter(
char
c)
{
if
(c ==
'x'
)
{
return
isX;
if
(c ==
'o'
)
{
return
isO;
return false
;
}
public void
placeCharacter(
char
c)
{
if
(c ==
'x'
)
{
isX =
true
;
isO =
false
;
if
(c ==
'o'
)
{
isO =
true
;
isX =
false
;
}
}
public void
clear()
{
isX =
false
;
isO =
false
;
}
public boolean
hasValue()
{
return
(isX
||
isO) ;
}
{
public
Square(
double
x,
double
y,
double
dx ,
double
dy)
super
(x, y, dx, dy) ;
}
public void
draw(Graphics2D g2 , Image xImage , Image oImage)
{
g2 . draw(
super
. getBounds2D() ) ;
if
(isX)
{
g2 . drawImage(xImage , (
int
)getX()+1,(
int
)getY()+1,(
int
)
getWidth()
−
2, (
int
)getHeight()
−
2,
null
);
if
(isO)
{
g2 . drawImage(oImage , (
int
)getX()+1,(
int
)getY()+1,(
int
)
getWidth()
−
2, (
int
)getHeight()
−
2,
null
);
}
}
}
The class keeps track of two Boolean variables that determine if the square is an
X
and
if the square is an
O
. Note that we could not have created just a single Boolean variable
because we have three states: the square is an
X
,itisan
O
, and it is empty. The
isCharacter