Thilina's Blog

I might be wrong, but…

MATLAB GUI programming (code level) 2.0 – Undocumented MATLAB – JAVA codes in MATLAB M-files

This is something I found really interesting in MATLAB. We can write m-scripts with JAVA codes, this is very useful when designing UI components for MATLAB applications. In this article I will share my experience on creating GUI in MATLAB using JAVA and also an implementation of video cropping software (backend part will be discussed on my next article).

For more information on Undocumented MATLAB, please visit Undocumented MATLAB official web page.

When using JAVA codes in *.m files we do not use “new” keywords to initiate any objects.  I am not sure the usage of import keyword in MATLAB/JAVA. Therefore JFrame must be used as  java.swing.JFrame. It is quite different from JAVA.

JFrame frame = new JFrame; // Java Syntax
frame = javax.swing.JFrame; % matlab syntax

Creating a JFrame, Adding title, setting size and layout is as follows,

frame = javax.swing.JFrame;
frame.setSize(1200,800);
frame.setTitle( 'Movie Maker');
frame.setLayout(java.awt.GridBagLayout);

We can use Java Border Factory as well. Setting up a JPanel can be done as follows,

playerPanel = javax.swing.JPanel;
playerPanel.setBorder( ...
  javax.swing.BorderFactory.createTitledBorder( ...
  javax.swing.BorderFactory.createEtchedBorder(), 'Video' ...
  ) ...
);
playerPanel.setLayout(java.awt.GridBagLayout);
playerPanel.setMinimumSize(java.awt.Dimension(800,600));
playerPanel.setMaximumSize(java.awt.Dimension(800,600));
playerPanel.setPreferredSize(java.awt.Dimension(800,600));

Setting actions can be done using the MATLAB keyword “ActionPerformedCallback” , we can set another *m file or function to call then action is triggered as follows,

set(pauseButton, 'ActionPerformedCallback',@playControlListener);

Even in positioning UI components, we can use GridbagLayout.

playerButtonPanel.add( ...
  captureButton, ...
  java.awt.GridBagConstraints( ...
    0, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST,
    java.awt.GridBagConstraints.NONE,...
    java.awt.Insets(6, 12, 6, 6), 1, 1));

In order to crop and to snap video frames I created Movie maker application using MATLAB and I used this kind of an implementation using JAVA for the GUI component. Complete code for the GUI is as below.

% Thilina S. Ambagahawaththa
% 2012/05/30
clc;
clear all;
close all;
%% Frame
frame = javax.swing.JFrame;
frame.setSize(1200,800);
frame.setTitle( 'Movie Maker');
frame.setLayout(java.awt.GridBagLayout);
%% Video Player Panel
playerPanel = javax.swing.JPanel;
  playerPanel.setBorder( ...
    javax.swing.BorderFactory.createTitledBorder( ...
    javax.swing.BorderFactory.createEtchedBorder(), 'Video' ...
  ) ...
);

playerPanel.setLayout(java.awt.GridBagLayout);
playerPanel.setMinimumSize(java.awt.Dimension(800,600));
playerPanel.setMaximumSize(java.awt.Dimension(800,600));
playerPanel.setPreferredSize(java.awt.Dimension(800,600));

viewPan = javax.swing.JScrollPane;
viewPan.setMinimumSize(java.awt.Dimension(800,600));
viewPan.setMaximumSize(java.awt.Dimension(800,600));
viewPan.setPreferredSize(java.awt.Dimension(800,600));

%% Player Button Panel
playerButtonPanel = javax.swing.JPanel;
  playerButtonPanel.setBorder( ...
    javax.swing.BorderFactory.createTitledBorder( ...
    javax.swing.BorderFactory.createEtchedBorder(), 'Control' ...
  ) ...
);
playerPanel.setLayout(java.awt.GridBagLayout);

%% Player Window
playerWindowLabel = javax.swing.JLabel;

%% Buttons
stateButton = javax.swing.JButton;
stateButton.setMinimumSize(java.awt.Dimension(60,25));
stateButton.setMaximumSize(java.awt.Dimension(60,25));
stateButton.setPreferredSize(java.awt.Dimension(60,25));

captureButton = javax.swing.JButton;
captureButton.setMinimumSize(java.awt.Dimension(120,25));
captureButton.setMaximumSize(java.awt.Dimension(120,25));
captureButton.setPreferredSize(java.awt.Dimension(120,25));
captureButton.setText( 'SaveFrame');
captureButton.setToolTipText( 'SF');
set(captureButton, 'ActionPerformedCallback',@playControlListener);

playButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_play.jpg' ));
playButton.setMinimumSize(java.awt.Dimension(25,25));
playButton.setMaximumSize(java.awt.Dimension(25,25));
playButton.setPreferredSize(java.awt.Dimension(25,25));
playButton.setToolTipText( 'Play');
set(playButton, 'ActionPerformedCallback',@playControlListener);

pauseButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_pause.jpg' ));
pauseButton.setMinimumSize(java.awt.Dimension(25,25));
pauseButton.setMaximumSize(java.awt.Dimension(25,25));
pauseButton.setPreferredSize(java.awt.Dimension(25,25));
pauseButton.setToolTipText( 'Pause');
set(pauseButton, 'ActionPerformedCallback',@playControlListener);

stopButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_stop.jpg' ));
stopButton.setMinimumSize(java.awt.Dimension(25,25));
stopButton.setMaximumSize(java.awt.Dimension(25,25));
stopButton.setPreferredSize(java.awt.Dimension(25,25));
stopButton.setToolTipText( 'Stop');
set(stopButton, 'ActionPerformedCallback',@playControlListener);

revButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_rev.jpg' ));
revButton.setMinimumSize(java.awt.Dimension(25,25));
revButton.setMaximumSize(java.awt.Dimension(25,25));
revButton.setPreferredSize(java.awt.Dimension(25,25));
revButton.setToolTipText( 'Rev');
set(revButton, 'ActionPerformedCallback',@playControlListener);

fwdButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_fwd.jpg' ));
fwdButton.setMinimumSize(java.awt.Dimension(25,25));
fwdButton.setMaximumSize(java.awt.Dimension(25,25));
fwdButton.setPreferredSize(java.awt.Dimension(25,25));
fwdButton.setToolTipText( 'Fwd');
set(fwdButton, 'ActionPerformedCallback',@playControlListener);

sbkButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_skipBck.jpg' ));
sbkButton.setMinimumSize(java.awt.Dimension(25,25));
sbkButton.setMaximumSize(java.awt.Dimension(25,25));
sbkButton.setPreferredSize(java.awt.Dimension(25,25));
sbkButton.setToolTipText( 'SBK');
set(sbkButton, 'ActionPerformedCallback',@playControlListener);

sfdButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_skipFwd.jpg' ));
sfdButton.setMinimumSize(java.awt.Dimension(25,25));
sfdButton.setMaximumSize(java.awt.Dimension(25,25));
sfdButton.setPreferredSize(java.awt.Dimension(25,25));
sfdButton.setToolTipText( 'SFD');
set(sfdButton, 'ActionPerformedCallback',@playControlListener);

ejtButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_ejt.jpg' ));
ejtButton.setMinimumSize(java.awt.Dimension(25,25));
ejtButton.setMaximumSize(java.awt.Dimension(25,25));
ejtButton.setPreferredSize(java.awt.Dimension(25,25));
ejtButton.setToolTipText( 'EJT');
set(ejtButton, 'ActionPerformedCallback',@playControlListener);

scrButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_scr.jpg' ));
scrButton.setMinimumSize(java.awt.Dimension(25,25));
scrButton.setMaximumSize(java.awt.Dimension(25,25));
scrButton.setPreferredSize(java.awt.Dimension(25,25));
scrButton.setToolTipText( 'Start Crop');
set(scrButton, 'ActionPerformedCallback',@playControlListener);

ecrButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_ecr.jpg' ));
ecrButton.setMinimumSize(java.awt.Dimension(25,25));
ecrButton.setMaximumSize(java.awt.Dimension(25,25));
ecrButton.setPreferredSize(java.awt.Dimension(25,25));
ecrButton.setToolTipText( 'End Crop');
set(ecrButton, 'ActionPerformedCallback',@playControlListener);

sveButton = javax.swing.JButton(javax.swing.ImageIcon('D:\Projects\Self\MovieMaker\project\icons\1_sve.jpg' ));
sveButton.setMinimumSize(java.awt.Dimension(25,25));
sveButton.setMaximumSize(java.awt.Dimension(25,25));
sveButton.setPreferredSize(java.awt.Dimension(25,25));
sveButton.setToolTipText( 'Save');
set(sveButton, 'ActionPerformedCallback',@playControlListener);

%% Sliders
playPointSlider = javax.swing.JSlider;
playPointSlider.setMinimum(1);
playPointSlider.setMaximum(10);
playPointSlider.setValue(1);
set(playPointSlider, 'MouseReleasedCallback',@playControlListener);

%% player Panel setup
viewPan.getViewport().add(playerWindowLabel);
playerPanel.add( ...
  viewPan, ...
  java.awt.GridBagConstraints( ...
  0, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.BOTH,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));

%% Button Panel Setup
% SB Rv Ps Pl St FF SF EJ
playerButtonPanel.add( ...
  stateButton, ...
  java.awt.GridBagConstraints( ...
  0, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  captureButton, ...
  java.awt.GridBagConstraints( ...
  0, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  sbkButton, ...
  java.awt.GridBagConstraints( ...
  1, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  revButton, ...
  java.awt.GridBagConstraints( ...
  2, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  pauseButton, ...
  java.awt.GridBagConstraints( ...
  3, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  playButton, ...
  java.awt.GridBagConstraints( ...
  4, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  stopButton, ...
  java.awt.GridBagConstraints( ...
  5, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  fwdButton, ...
  java.awt.GridBagConstraints( ...
  6, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  sfdButton, ...
  java.awt.GridBagConstraints( ...
  7, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  ejtButton, ...
  java.awt.GridBagConstraints( ...
  8, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  scrButton, ...
  java.awt.GridBagConstraints( ...
  8, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  ecrButton, ...
  java.awt.GridBagConstraints( ...
  8, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
playerButtonPanel.add( ...
  sveButton, ...
  java.awt.GridBagConstraints( ...
  8, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.NONE,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));

frame.add( ...
  playerPanel, ...
  java.awt.GridBagConstraints( ...
  0, 0, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.BOTH,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
frame.add( ...
  playPointSlider, ...
  java.awt.GridBagConstraints( ...
  0, 1, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.BOTH,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
frame.add( ...
  playerButtonPanel, ...
  java.awt.GridBagConstraints( ...
  0, 2, 1, 1, 1.0, 1.0, java.awt.GridBagConstraints.NORTHWEST, java.awt.GridBagConstraints.BOTH,...
  java.awt.Insets(6, 12, 6, 6), 1, 1));
  frame.setVisible(true);

Final GUI is as follows,

image

I will discuss the implementation of the backend of this application in my next article. Hope you have some basic idea on using JAVA UI components with *.m scripts. Thank you very much for reading.

2012 May 30 - Posted by | Java, MATLAB | , , , , , ,

8 Comments »

  1. Using import in Matlab is easy:

    >> import javax.swing.*
    >> frame = JFrame; % no need to add the javax.swing prefix any longer

    >> import java.awt.*
    >> dim = Dimension(120,25);

    Comment by Yair Altman | 2012 May 31 | Reply

  2. Thanks a lot, can you add an example of adding a combo box and its action listeners

    Comment by Chandima | 2012 November 29 | Reply

  3. Thanks for the prompt reply🙂

    Comment by Chandima | 2012 November 30 | Reply

  4. good article.. thanks for sharing..🙂

    Comment by About Java | 2012 December 30 | Reply

  5. Hi! Thilina, You doing a great Job. could you please explain how to connect MySQL Database to MATLAB. I want to store 45 images into MySQL Database. I am using both MATLAB-JAVA (Undocumented MATLAB – JAVA codes).

    Comment by Jean-Marie | 2013 January 9 | Reply

  6. Hi Thilina!
    Let’s have the simple code from Matlab website (http://www.mathworks.com/help/matlab/ref/im2java.html):

    I = imread(‘ngc6543a.jpg’);
    javaImage = im2java(I);
    frame = javax.swing.JFrame;
    icon = javax.swing.ImageIcon(javaImage);
    label = javax.swing.JLabel(icon);
    frame.getContentPane.add(label);
    frame.pack
    frame.show

    Now comes my question: how can we load new image to the same (java) frame and refresh the display?
    Could you please help me?

    Comment by Jozsef Orban | 2015 July 22 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: