how to create a drawing callback ?
5 次查看(过去 30 天)
显示 更早的评论
Hi, Im used to working in C and opengl and i am trying to maintain the same workflow. I wrote the code attached at the bottom (basically what it does is open a figure and an axis and allow to draw markers in different colors according to the mouse key that was pressed.
however this is not the proper way to do this (in C that is). I used to define a drawing callback function - all plotting/drawing is done in it. the input callback simply change the global variables. The problem is when i tried to build this type of function i couldnt find a way to execute it (since its supposed to be executed after any event and not a specific one)
anyone has a clue how to set a custom drawing callback? I hope i was clear (i know the phrasing came out bad ) Thanks
function [ ] = main5( )
%main5 demonstrates how to process keybord and mouse inputs
%clear all; close all; clc; %not recommended use inside functions
%Initialize Variables
global CursorPos
global WhichButton
CursorPos=[];
WhichButton=[];
Figureh=0;
Axesh=0;
Objecth=0;
bgcolor='r';
width=500;
height=500;
%code
Figureh=figure('NumberTitle','off','Name','Itzik Clock',...
'Position',[200 200 width height],'Color',bgcolor); %bg is set to red so we know that we can only see the axes
Axesh=axes('Color','c','Position',[0 0 1 1],...
'XLim',[0 width],'YLim',[0 height],'XTick',[],'YTick',[]);
daspect([1 1 1]);
set(Figureh,'KeyPressFcn',@KeyboardCB);
set(Figureh,'WindowButtonDownFcn',@MouseDownCB);
drawnow;
%set(Figureh,'WindowButtonMotionFcn',@MouseMotionCB);
end
function [ ] = KeyboardCB(hObject,event)
%Example print of the key which was pressed
fprintf('I pressed %c key\n',event.Key);
switch event.Key
case 'r'
cla;
end
end
function [ ] = MouseDownCB(hObject,event)
%Example print X,Y coordinates when mouse is clicked
global CursorPos
global WhichButton
CursorPos=get(hObject,'CurrentPoint');
WhichButton=get(hObject,'SelectionType');
hold all;
ScatterHandle=scatter(CursorPos(1),CursorPos(2));
set(ScatterHandle,'MarkerEdgeColor','none');
switch WhichButton
case 'normal'
set(ScatterHandle,'MarkerFaceColor','y');
case 'alt'
set(ScatterHandle,'MarkerFaceColor','r');
case 'extend'
set(ScatterHandle,'MarkerFaceColor','b');
end
disp(['You clicked X:',num2str(CursorPos(1)),', Y:',num2str(CursorPos(2))]);
end
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!