MATLAB Answers

How to change the position of a pictue in a figure with the mouse?

1 view (last 30 days)
Hello everone
I have to program various chess puzzles for a school project. I would like to ask if it is possible to click on a picture of the chess figure in a figure and move it?
For example: For the move rook from f1 to f8, that you can click on the rook at f1 and then on the square f8 and the rook changes the position on the figure.
%Loading the images of the chess figures
%Size of the chess figures 100x100
n = 100;
%Showing the chessboard. Size 872x872 with borders with a wide od 36 Pixels.
%The actual Chessboard has the size 800x800
Cb = imshow('Cb.png');
hold on
%Showing the figures on the chess board. kbw means k = king, b = color of the chessfigure, w = color of the field.
a8 = imshow('kbw.png');
%Setting the position of the figur.
set(a8,'XData',[37 136],'Ydata',[37 136]);
h1 = imshow('kww.png');
set(h1,'XData',[37 136]+n*7,'Ydata',[37 136]+n*7);
h8 = imshow('qww.png');
set(h8,'XData',[37 136]+n*7,'Ydata',[37 136]+n);
f1 = imshow('rww.png');
set(f1,'XData',[37 136]+n*5,'Ydata',[37 136]+n*7);

Accepted Answer

Nitin Kapgate
Nitin Kapgate on 14 Dec 2020
You can refer to the following code snippet that demonstrates the functionality where you can click on a picture of the chess figure in a figure and move it. The program uses "ButtonDownFcn" callbacks for images.
Download all the attached files as they are necessary to run the following code snippet (testChessBoard.m).
function testChessBoard()
close all;
board = checkerboard(100);
hfig = figure;
board = imread("I.png");
board_obj = image(board);
board_obj.ButtonDownFcn = @checkerboard_ButtonDownFcn;
% CUrrently I am assuming only 2 Actors - Elephant and Queen
hold on
queen = imread("Queen80_80.png");
queen_obj= image(queen);
queen_obj.ButtonDownFcn = @queen_ButtonDownFcn;
hold on
elephant = imread("Elephant80_80.png");
elephant_obj= image(300,300,elephant);
elephant_obj.ButtonDownFcn = @elephant_ButtonDownFcn;
x_nextpos = 0;
y_nextpos = 0;
selectedActor = "elephant";
% Updates which actor is currently selected
function queen_ButtonDownFcn(hObject, eventdata, handles)
selectedActor = "queen";
function elephant_ButtonDownFcn(hObject, eventdata, handles)
selectedActor = "elephant";
% gets the X adn Y coordinates from mouse click
function checkerboard_ButtonDownFcn(hObject, eventdata, handles)
% Get the coordinates of the clicked point
hax = ancestor(hObject, 'axes');
point = get(hax, 'CurrentPoint');
point = round(point(1,1:2));
x_nextpos = point(1);
y_nextpos = point(2);
% moves the actor to the selected position
function moveActor(actorName)
if(actorName == "queen")
queen_obj = image(x_nextpos,y_nextpos,queen);
elseif(actorName == "elephant")
elephant_obj = image(x_nextpos,y_nextpos,elephant);
To try the code, perform the folowing steps:
  1. Run the testChessBoard.m file
  2. In the figure opened, click on Elephant image (Elephant is selected for movement)
  3. Now click on checkerboard where you want the Elephant to be moved (Elephant is moved there)
  4. Next, click on Queen image (Queen is selected for movement)
  5. Now click on checkerboard where you want the Queen t to be moved (Queen is moved there)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!