Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to make a cookie cutter that will extract a small square from a much larger bitmap with a click of the mouse. The LocatorPane appears to be a useful way to move the cookie cutter around, but I'm not having much luck in drawing the small dynamic rectangle over the bitmap. I would like to change the appearance of the locator icon to a small rectangle, or just make the locator icon disappear. The LocatorPane option Appearance -> Graphics[Rectangle[]] didn't work. Beyond that there is the question of how to capture a point value with a mouse click. This is my first use of Dynamic. I suspect I am going about this all the wrong way and perhaps I should be doing something with mouse functions and cursor images. If so, I would appreciate it if someone would point me towards a pertinent page or two. I have not been able to find one.

This script draws the locator on a bitmap labeled "background":



Clear[background]
    
    
background = Image[Graphics[{LightGray, Rectangle[]}]];
DynamicModule[
    {pt = {180, 180}},
     LocatorPane[Dynamic[pt], background]
    ]

But when I add code for the dynamic rectangle, I find that it draws outside the background bitmap.


DynamicModule[
{pt = {180, 180}},
 
  {LocatorPane[Dynamic[pt], background],

   Graphics[

     {EdgeForm[{Thick, Red}],

      FaceForm[],
Rectangle[Dynamic[pt]]}

   ]}
 
 ]
share|improve this question
    
Related: Szabolcs applied the crop rectangle to zoom into graphics objects here. –  István Zachar Oct 3 '12 at 22:49
    
Also see image cropper here. –  István Zachar Oct 3 '12 at 23:05
add comment

1 Answer 1

up vote 5 down vote accepted

Combine the background and the Graphics with Show, so that you can supply both of them as the second argument to LocatorPane:

background = Image[Graphics[{LightGray, Rectangle[]}]];
DynamicModule[{pt1 = {180, 180}, pt2 = {20, 20}},
 LocatorPane[Dynamic[{pt1, pt2}],
  Show[
   background,
   Graphics[{EdgeForm[{Thick, Red}],
     FaceForm[], Rectangle[Dynamic[pt1], Dynamic[pt2]]}]]
  , Appearance -> Graphics[Rectangle[], ImageSize -> {10, 10}]]]

enter image description here

Update After further clarification of the question:

background = Image[Graphics[{LightGray, Rectangle[]}]];
DynamicModule[{pt1 = {180, 170}, pt2 = {190, 180}}, 
 LocatorPane[Dynamic[{pt1(*,pt2*)}], 
  Show[background, 
   Graphics[{EdgeForm[{Thick, Red}], FaceForm[], 
     Dynamic[Rectangle[pt1 + {5, 5}, pt1 - {5, 5}]]}]], Appearance -> None]]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.