Repaint() - Must fix for degree project

My program has classGUIControl extends JFrame for the main swing interface, with an internal classDrawJPanel extends JPanel to allow drawing of a waveform directly on screen. ClassFileio is used for I/O (!).

I can draw a waveform on the DrawJPanel and save to file. The drawing works by getting the current x,y value and adding it to an array. PaintComponent draws the array values on the DrawJPanel. When I load it from file the sound plays fine, but I can't get the waveform to repaint() on the DrawJPanel.

I need to call repaint() from outside DrawJPanel - from inside the actionListener on File-Open. I have tried putting drawPanel.repaint() inside the actionListener but then it needs to be declared final - so I wrote a methodredrawWaveform within GUIControl which makes this call, but will not compile (cannot resolve symbol).

publicclass GUIControlextends JFrame

...

privatevoid Initialize()throws Exception

...

menuFileOpen.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

waveformArray = myIO.readWaveform();

redrawWaveform();

...

publicvoid redrawWaveform(){drawPanel.repaint();}

...

privateclass DrawJPanelextends JPanel{public DrawJPanel{

...

publicvoid paintComponent(Graphics g)...

Any help gratefully appreciated.

Sandra.

[2165 byte] By [mamscw] at [2007-9-26 2:08:35]
# 1

I'm not sure if I completely understand the problem, but I think you have a couple options you could pursue. One is paintComponents(Graphics g), and give it DrawJPanel.getGraphics() as the argument, or something along those lines. Also, you could try invalidate()/revalidate(). Although these may work, I think the best, but more difficult, way is to write your own extension of RepaintManager. This should give you far better control over what, and when, repainting happens. Hope this helps,

m

wywiwyg at 2007-6-29 8:57:17 > top of Java-index,Archived Forums,Swing...
# 2

I didn't understand exactly what you mean but maybe this helps:

Inside the actionPerformed method you call

redrawWaveForm()

you should call that method like this:

GUIContrlo.this.redrawWaveForm()

another way to do that is make GUIControl implement actionListener:

public class GUIControl extends JFrame implements ActionListener

public void actionPerformed(ActionEvent e) {

...

redrawWaveForm(); }

private void Initialize() {

menuFileOpen.addActionListener(this);

}

jpob77 at 2007-6-29 8:57:17 > top of Java-index,Archived Forums,Swing...
# 3

I'm still having difficulties with the repaint, but at runtime now. I'll try and clarify what I'm trying to do first.

Class GUIControl's subclass DrawJPanel allows the user to draw a waveform on the screen which can then be played back, using the JSyn libraries. This all works fine, including the drawing, which uses paintComponent.

I'm calling class Fileio from GUIControl to save the array of integers generated when the waveform is drawn. The drawing area is then cleared. When I call Fileio to load a saved waveform ( the array of integers) I wish to use this data to play the waveform, which works fine, and to redraw the waveform on the screen - the panel defined by DrawJPanel. This redrawing of the loaded waveform on the screen is what I can't make work.

At runtime, when a waveform is loaded, method redrawWaveform - defined in GUIControl, of which DrawJPanel is a subclass - calls drawPanel.repaint(). This gives a NullPointerException. I assume that this is because it doesn't know about the Graphics g object when repaint() is called from outside DrawJPanel. I don't understand how to use getGraphics() as an argument to make this work.

Also note that when the user first draws the waveform on the screen, the code for this is in class DrawJPanel and uses the paintComponent method to draw this. This is the same paintComponent method which needs to be called to redraw the loaded waveform - unless there is a different way of doing this which will work for both drawn and loaded waveforms.

Any ideas on this?

Thanks.

Sandra.

mamscw at 2007-6-29 8:57:17 > top of Java-index,Archived Forums,Swing...
# 4

I need more information to be able to help you.

You can check the program output when the NullPointerException is thrown to know the line number and then to know what object was null.

How do you know that the graphics was null?

Do you initialize all object references before using them?

When you use arrays do you initialize the array and then each element?

jpob77 at 2007-6-29 8:57:17 > top of Java-index,Archived Forums,Swing...
# 5

Hi

> You can check the program output when the

> NullPointerException is thrown to know the line number

> and then to know what object was null.

The error message is:

Exception occurred during event dispatching:

java.lang.NullPointerException

at TimbreFH1.GUIControl.redrawWaveform(GUIControl.java: 694)

Followed by all the error messages caused by this.

This line of code at 694 is:

drawPanel.repaint();

inside method redrawWaveform, which is supposed to call paintComponent inside DrawJPanel drawPanel to redraw the waveform.

When I have put print statements in paintComponent it shows that this exception is occurring as soon as paintComponent is entered in this way - i.e. not print statements are showing, although they do when paintComponent is entered normally to draw the waveform ( i.e. when not loading from file, just drawing directly on screen)

> How do you know that the graphics was null?

This is why I assume that it is a problem with the graphics object - perhaps it is null.

> Do you initialize all object references before using

> them?

Yes.

> When you use arrays do you initialize the array and

> then each element?

No, because I thought that java did this for you. Or is it risky to assume that this is the case.

I hope that this is enough detail.

Thanks again.

Sandra.

mamscw at 2007-6-29 8:57:17 > top of Java-index,Archived Forums,Swing...
# 6
Thanks for your help. I've now gone for the program restructuring option which has fixed the problem. I'm now calling the redrawWaveform method from within repaint() and passing Graphics g as the parameter, which seems to have fixed it.Sandra.
mamscw at 2007-6-29 8:57:17 > top of Java-index,Archived Forums,Swing...