필터 지우기
필터 지우기

how to create a drawing callback ?

조회 수: 1 (최근 30일)
Itzik Ben Shabat
Itzik Ben Shabat 2013년 8월 23일
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
  댓글 수: 3
Itzik Ben Shabat
Itzik Ben Shabat 2013년 8월 26일
sorry. i thought i did it but for some reason it didnt work. now its readable. any way you can help me out with this?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by