Message Boards Message Boards

0
|
5148 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Image Button[] triggering text in a background image?

Hello community.

I created a Button[] on top of a small image that is placed randomly on a larger image, which is the background. The button triggers text in the center of the background image.

However, there is still a small error that I don't know how to get around.

Before pressing the button, the resulting image is in an error state. Take a look at the code (and result):

i1

z = ImageCompose[background, image, Scaled[a = RandomReal[{0, 1}, 2]]];
c = ImageDimensions[background]; Show[{z, Graphics@Dynamic[b], 
  Graphics[{Transparent, 
    Button[Rectangle[{a[[1]]*c[[1]] - 25, 
       a[[2]]*c[[2]] - 35}, {a[[1]]*c[[1]] + 25, a[[2]]*c[[2]] + 35}],
      b = Text[
       Style["Text", Bold, Purple, FontSize -> 40], {0.5*c[[1]], 
        0.5*c[[2]]}], Appearance -> "Frameless"]}]}, 
 ImageSize -> Large]

i2

But, after pressing the button (on the small image), the error no longer exists and the function did what it was supposed to do.

i3

I suppose the error occurs because Graphics[] tries to evaluate "b" before pressing the button.

How do I fix this?? Maybe it's simple, but clearly I missed something… How do I fix my code to prevent this error from occurring?

The images are attached to the post.

Thanks in advance.

Attachment

Attachment

POSTED BY: Claudio Chaib

I managed to get around the situation, this way it works:

z = ImageCompose[background, image, Scaled[a = RandomReal[{0, 1}, 2]]];
c = ImageDimensions[
  background]; DynamicModule[{b = {Transparent, Disk[{0, 0}, 1]}}, 
 Show[{z, Graphics@Dynamic[b], 
   Graphics[{Transparent, 
     EventHandler[
      Button[Rectangle[{a[[1]]*c[[1]] - 25, 
         a[[2]]*c[[2]] - 35}, {a[[1]]*c[[1]] + 25, 
         a[[2]]*c[[2]] + 35}], Dynamic[b], 
       Appearance -> 
        "Frameless"], {"MouseClicked" :> (b = 
          Text[Style["Text", Bold, Purple, 
            FontSize -> 40], {0.5*c[[1]], 0.5*c[[2]]}])}]}]}, 
  ImageSize -> Large]]

Thanks.

POSTED BY: Claudio Chaib
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract