How to freedraw with a mouse, like in paint?

2 views (last 30 days)
tvb
tvb on 20 Jan 2018
Answered: Rik on 20 Jan 2018
So for this schoolproject I need to remake paint without using any of the preprogrammed functions that matlab provides. Currently I'm trying to code a drawing tool with your mouse, so that a user can hold his leftmousebutton, draw, and finish the action by releasing his leftmousebutton. So tried with ButtonDownFcn and WindowButtonMotionFcn but I couldn't figure out how to work with them...At the moment my code is as follows:
function workspace_ButtonDownFcn(hObject, eventdata, handles)
if valPen == 1
%msgbox('Button pressed'); This was just so I could see if it worked as
%intended...
set(gcf,'WindowButtonMotionFcn',@FreeDraw)
end
function FreeDraw(hObject, handles)
CurrP = get(gca, 'CurrentPoint');
handles.im(CurrP(1,1),CurrP(1,2),:)=0;
image(handles.im);
axis image
guidata(hObject,handles);

Answers (1)

Rik
Rik on 20 Jan 2018
Use ButtonDownFcn (of the axes object) to set a flag, use WindowButtonUpFcn (of the figure) to reset the flag. Now you can set WindowButtonMotionFcn to draw pixels, but exit immediately if a button is not pressed.
Inside FreeDraw I would explicitly set figure to normalized and check if CurrP is in the [0 1] range. Then you can convert those units to pixel locations inside im (don't forget to round and check for 0).

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!