From: Brian F on
Hi,

I have a Matlab gui. It reads in data, and then it calls a function to create a Java Frame.

Now, I have the frame appearing fine, the problem is when I try to draw anything onto it, I just get the error

%%%%%%%%%%%%%

??? Attempt to reference field of non-structure array.

Error in ==> show_manuscript at 22
g.drawRect(10,10,5,5);

%%%%%%%%%%%%%

import javax.swing.*;
import java.awt.*;
import java.net.URL;


frame = JFrame('Manuscript');
imageIcon = ImageIcon('sheet_music.gif');
i = imageIcon.getImageObserver();

textArea = JTextArea();

image = imageIcon.getImage();
grayImage = GrayFilter.createDisabledImage(image);

g = textArea.getGraphics();

%g.drawImage(image, 0, 0, imageIcon.getImageObserver());
%g.drawLine(1,1,10,10);
g.drawRect(10,10,5,5);

textArea.paintComponents(g);


scrollPane = JScrollPane(textArea);
content = frame.getContentPane();
content.add(scrollPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(1);
frame.setSize(800, 600);
frame.setVisible(true);


%%%%%%%%%%%%%%

The problem seems to be that I cannot use any of the graphics methods , as they all give me the same error. Has anyone ever had or seen this problem before?

Thanks.
From: Donn Shull on
"Brian F" <clamps4(a)gmail.com> wrote in message <hlr6a4$knt$1(a)fred.mathworks.com>...
> Hi,
>
> I have a Matlab gui. It reads in data, and then it calls a function to create a Java Frame.
>
> Now, I have the frame appearing fine, the problem is when I try to draw anything onto it, I just get the error
>
> %%%%%%%%%%%%%
>
> ??? Attempt to reference field of non-structure array.
>
> Error in ==> show_manuscript at 22
> g.drawRect(10,10,5,5);
>
> %%%%%%%%%%%%%
>
> import javax.swing.*;
> import java.awt.*;
> import java.net.URL;
>
>
> frame = JFrame('Manuscript');
> imageIcon = ImageIcon('sheet_music.gif');
> i = imageIcon.getImageObserver();
>
> textArea = JTextArea();
>
> image = imageIcon.getImage();
> grayImage = GrayFilter.createDisabledImage(image);
>
> g = textArea.getGraphics();
>
> %g.drawImage(image, 0, 0, imageIcon.getImageObserver());
> %g.drawLine(1,1,10,10);
> g.drawRect(10,10,5,5);
>
> textArea.paintComponents(g);
>
>
> scrollPane = JScrollPane(textArea);
> content = frame.getContentPane();
> content.add(scrollPane, BorderLayout.CENTER);
> frame.setDefaultCloseOperation(1);
> frame.setSize(800, 600);
> frame.setVisible(true);
>
>
> %%%%%%%%%%%%%%
>
> The problem seems to be that I cannot use any of the graphics methods , as they all give me the same error. Has anyone ever had or seen this problem before?
>
> Thanks.

I am not an expert in java but your

g = textArea.getGraphics();

line is returning empty which is why

g.drawRect(10,10,5,5);

is giving the error message

The rest of your code is working

Donn
From: Yair Altman on
as Donn noted, this is because g = textArea.getGraphics() returns an empty value ([]) and therefore fails on g.drawRect().

This may be due to thread-timing issues (which can normally be remedied using pause(0.02) commands to allow the Java EDT time to finish rendering, or else use the javaMethodEDT/awtinvoke functions to ensure EDT completion prior to your command's execution.

Unfortunately, I think this may not help much if you don't implement a Java paint() function. This function cannot be coded in Matlab - only within a Java class. Without it, the automatic Java repaint actions may simply erase your drawings. The simple solution is to implement a simple Java class that implements paint() and then call this class from Matlab.

Yair Altman
http://UndocumentedMatlab.com
From: Brian on
"Yair Altman" <altmanyDEL(a)gmailDEL.comDEL> wrote in message <hlrr67$b17$1(a)fred.mathworks.com>...
> as Donn noted, this is because g = textArea.getGraphics() returns an empty value ([]) and therefore fails on g.drawRect().
>
> This may be due to thread-timing issues (which can normally be remedied using pause(0.02) commands to allow the Java EDT time to finish rendering, or else use the javaMethodEDT/awtinvoke functions to ensure EDT completion prior to your command's execution.
>
> Unfortunately, I think this may not help much if you don't implement a Java paint() function. This function cannot be coded in Matlab - only within a Java class. Without it, the automatic Java repaint actions may simply erase your drawings. The simple solution is to implement a simple Java class that implements paint() and then call this class from Matlab.
>
> Yair Altman
> http://UndocumentedMatlab.com

Thanks for the help guys, I'll try that now