Question

I'm drawing rectangles on the screen using the basic NSBezierPath method. I have an object set up to draw a customized rectangle on the screen with each instance of the object initialized. In that object I have a method that moves the rectangle around with the mouse when its dragged across the screen. To accomplish this I simply used the mouseDragged: method. The problem is as I move it, it leaves behind "image artifacts" which appear as thin 1px lines on the screen along the edges of where the rectangle was previously. This is similar to how when Microsoft Windows was running slowly, it used to leave ghosts of a window as you dragged it around. Remember how you would always try to draw stuff or fill the entire screen before it unfroze? Its like its just not redrawing quickly enough or something. The artifacts never go away unless I clear them out by drawing something over them (like dragging the rectangle back over them).

-(void)mouseDown:(NSEvent *)theEvent{
mouseLoc = [theEvent locationInWindow];
xDifference = mouseLoc.x - origin.x;
yDifference = mouseLoc.y - origin.y;

}

-(void)mouseDragged:(NSEvent *)theEvent{
mouseLoc = [theEvent locationInWindow];
NSPoint adjustedOrigin = NSMakePoint((mouseLoc.x-xDifference), (mouseLoc.y-yDifference));
[self setOrigin:adjustedOrigin];


}

Also if you're wondering about the blank square to the left of the blue rectangle, I can't explain it either. I have the black 'rhombus' going around in circles 30px in diameter, but every time it moves one space (one a second on an NSTimer) it jumps very briefly to that spot, but why I have no clue. the code for the movement of the rectangle is below:

i+=.5;
int rectangle2X = (100+30*cos(i));
int rectangle2Y = (100+30*sin(i));
//NSLog(@"(%d, %d)", rectangle2X, rectangle2Y);
rectangle2.origin = NSMakePoint(rectangle2X, rectangle2Y);

Any help would be great. I know the code above isn't everything that you guys might need so feel free to ask for more. The top code is from the object, the bottom code comes from the AppDelegate. Both rectangles in the picture are instances of my object.

Picture of Artifacts

Was it helpful?

Solution

Don't worry just call it modern art!

But seriously. It looks like you are not clearing the view in the drawRect: or drawInContext: method.

Paint a rect of background color over the bounds as the first thing you do to clear the prior drawing.

[myBGColor set];
NSRectFill([self bounds]);

NSViews don't automatically clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top