You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by noni_4444 <na...@gmail.com> on 2007/09/07 21:57:16 UTC
Require repaint in applet to update canvas
I have an SVG document containing a rectangle which is to be animated
repeatedly depending upon a value received in a callback function. The
SVG document is loaded inside an applet. The problem i face is that
SVGCanvas is not updated automatically until i minimize the html page
containing the applet and maximize it. Doing this calls the repaint method
of applet. If i call the repaint method myself inside the callback method,
i see the canvas flickering. I have already read a dozen posts on this
forum
related to this and have tried all the suggested solutions. All point to
something like
"If you set the canvas's document state to JSVGCanvas.ALWAYS_DYNAMIC and run
all your document updates in the UpdateManager, it should update itself".
What to do to update the SVG canvas without calling repaint() ? I am already
following
the above guideline.
Following are relevant portions of my code.
public class BatikApplet extends Applet {
UpdateManager updateManager = null;
//Initialize the applet
public void init() {
jbInit();
svgCanvas = new JSVGCanvas();
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
String parser =
XMLResourceDescriptor.getXMLParserClassName();
docFactory = new SAXSVGDocumentFactory(parser);
rectDoc =
docFactory.createSVGDocument("http://localhost:8080/"+screenName);
registerListeners();
svgCanvas.setDocument(rectDoc);
this.add(svgCanvas, BorderLayout.NORTH);
svgCanvas.repaint();
}
// Paint method
public void paint(Graphics g) {
svgCanvas.paintComponent(g);
}
// Method which is called repeatedly with updated animation values to be
drawn
public synchronized void callback(final String strData) {
runnable = new Runnable() {
public void run() {
//Call to external object which changes the SVG document
oAnimEngine.animateScreen(strData);
}
};
try {
updateManager.getUpdateRunnableQueue().invokeLater(runnable);
} catch (Exception ex) {
}
}
// Register listener method
public void registerListeners() {
svgCanvas.addGVTTreeRendererListener(new
GVTTreeRendererAdapter() {
public void gvtRenderingCompleted(GVTTreeRendererEvent e)
{
updateManager = svgCanvas.getUpdateManager();
oAnimEngine.setCustomSVGDocument(rectDoc);
}
});
}
}
--
View this message in context: http://www.nabble.com/Require-repaint-in-applet-to-update-canvas-tf4403481.html#a12562305
Sent from the Batik - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: Require repaint in applet to update canvas
Posted by noni_4444 <na...@gmail.com>.
In my case the problem turned out to be that i was mixing AWT and swing
components. I was adding jsvgcanvas to a awt panel. Mixing awt and swing
components is not a recommended approach. For details please see the
following links.
1. Why You Shouldn’t Mix AWT and Swing
http://www.devx.com/tips/Tip/14718
2. Painting in AWT and Swing
http://java.sun.com/products/jfc/tsc/articles/painting/#db
Naveed
--
View this message in context: http://www.nabble.com/Require-repaint-in-applet-to-update-canvas-tf4403481.html#a12666865
Sent from the Batik - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Why is repaint required in applet to update canvas ? plz reply
Posted by noni_4444 <na...@gmail.com>.
noni_4444 wrote:
>
> I have an SVG document containing a rectangle which is to be animated
> repeatedly depending upon a value received in a callback function. The
> SVG document is loaded inside an applet. The problem i face is that
> SVGCanvas is not updated automatically until i minimize the html page
> containing the applet and maximize it. Doing this calls the repaint method
> of applet. If i call the repaint method myself inside the callback method,
> i see the canvas flickering. I have already read a dozen posts on this
> forum
> related to this and have tried all the suggested solutions. All point to
> something like
>
> "If you set the canvas's document state to JSVGCanvas.ALWAYS_DYNAMIC and
> run
> all your document updates in the UpdateManager, it should update itself".
>
> What to do to update the SVG canvas without calling repaint() ? I am
> already following
> the above guideline.
>
> Following are relevant portions of my code.
>
> public class BatikApplet extends Applet {
> UpdateManager updateManager = null;
>
> //Initialize the applet
> public void init() {
> jbInit();
> svgCanvas = new JSVGCanvas();
> svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
> String parser =
> XMLResourceDescriptor.getXMLParserClassName();
> docFactory = new SAXSVGDocumentFactory(parser);
> rectDoc =
> docFactory.createSVGDocument("http://localhost:8080/"+screenName);
> registerListeners();
> svgCanvas.setDocument(rectDoc);
> this.add(svgCanvas, BorderLayout.NORTH);
> svgCanvas.repaint();
> }
>
> // Paint method
> public void paint(Graphics g) {
> svgCanvas.paintComponent(g);
> }
>
> // Method which is called repeatedly with updated animation values to be
> drawn
> public synchronized void callback(final String strData) {
> runnable = new Runnable() {
> public void run() {
> //Call to external object which changes the SVG document
> oAnimEngine.animateScreen(strData);
> }
> };
>
> try {
>
> updateManager.getUpdateRunnableQueue().invokeLater(runnable);
> } catch (Exception ex) {
>
> }
> }
>
> // Register listener method
> public void registerListeners() {
> svgCanvas.addGVTTreeRendererListener(new
> GVTTreeRendererAdapter() {
> public void gvtRenderingCompleted(GVTTreeRendererEvent
> e) {
> updateManager = svgCanvas.getUpdateManager();
> oAnimEngine.setCustomSVGDocument(rectDoc);
> }
> });
> }
> }
>
--
View this message in context: http://www.nabble.com/Require-repaint-in-applet-to-update-canvas-tf4403481.html#a12593679
Sent from the Batik - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: Require repaint in applet to update canvas
Posted by noni_4444 <na...@gmail.com>.
> This is probably flailing on your part but I think this
> is wrong, since you have added the canvas as a child of the Applet
> Swing should ensure that it gets painted. I'm not sure what the
> impact of adding this call would be...
I had actually multiple rectangles drawn on the canvas. With the previous
code only some of the rectangles were redrawn and there was flickering too.
I changed the following lines of code
// Paint method
public void paint(Graphics g) {
svgCanvas.paintComponent(g);
}
to
public void update(Graphics g) {
paint(g);
}
public synchronized void paint(Graphics g) {
super.paint(g);
}
and now all the rectangles are updated. But flickering is still there.
> oAnimEngine.initStructures(rectDoc, win, screenName);
> This might be your problem. You should get the Document from the
> Canvas rather than remember the document you passed to the canvas. In
> some cases the canvas has to clone the document passed to it so that it
> is using our DOM Implementation. I would think that your parsing code
> would generate a DOM that passes this test but if not this could explain
> your problems (you are changing rectDoc, but the canvas is displaying a
> separate document cloned from rectDoc).
I would have to redesign my whole application if i want to get an SVG
document from canvas each time in
my callback function. My current design parses the svg document initially
and stores references to some
of it's elements. Upon receiving the callback it updates these elements to
perform animation.
Since the rectangles are being redrawn allright, this doesn't seem to be the
problem too. If i am changing
one document and canvas is showing another document, there should be no
animation. For now there IS animation, it just flickers.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
--
View this message in context: http://www.nabble.com/Require-repaint-in-applet-to-update-canvas-tf4403481.html#a12636495
Sent from the Batik - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: Require repaint in applet to update canvas
Posted by th...@kodak.com.
Hi Noni,
noni_4444 <na...@gmail.com> wrote on 09/07/2007 03:57:16 PM:
> I have an SVG document containing a rectangle which is to be animated
> repeatedly depending upon a value received in a callback function.
> [..] If i call the repaint method myself inside the callback method,
> i see the canvas flickering.
Does the shape move? If not the problem may be that your
code to move the rectangle isn't really moving the rectangle.
In which case the canvas will decide that nothing had changed so
there is nothing to redraw.
> I have already read a dozen posts on this
> forum related to this and have tried all the suggested solutions. All
point to
> something like
>
> "If you set the canvas's document state to JSVGCanvas.ALWAYS_DYNAMIC and
run
> all your document updates in the UpdateManager, it should update
itself".
>
> What to do to update the SVG canvas without calling repaint() ? I am
already
> following the above guideline.
Then something must be broken because there is nothing
else to do. I've looked over your code and nothing really
obvious jumps out at me, but I have commented on some possible
problem code below.
> Following are relevant portions of my code.
>
> public class BatikApplet extends Applet {
> UpdateManager updateManager = null;
>
> //Initialize the applet
> public void init() {
> jbInit();
> svgCanvas = new JSVGCanvas();
> svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
> String parser =
> XMLResourceDescriptor.getXMLParserClassName();
> docFactory = new SAXSVGDocumentFactory(parser);
> rectDoc =
> docFactory.createSVGDocument("http://localhost:8080/"+screenName);
> registerListeners();
> svgCanvas.setDocument(rectDoc);
> this.add(svgCanvas, BorderLayout.NORTH);
This is potentially bad you should add the Canvas pack the
window and then set the Document. Otherwise there is a chance
that the document will do a wasted rendering at "zero" size.
> svgCanvas.repaint();
> }
>
> // Paint method
> public void paint(Graphics g) {
> svgCanvas.paintComponent(g);
> }
This is probably flailing on your part but I think this
is wrong, since you have added the canvas as a child of the Applet
Swing should ensure that it gets painted. I'm not sure what the
impact of adding this call would be...
>
> // Method which is called repeatedly with updated animation values to
be
> drawn
> public synchronized void callback(final String strData) {
> runnable = new Runnable() {
> public void run() {
> //Call to external object which changes the SVG document
> oAnimEngine.animateScreen(strData);
> }
> };
>
> try {
>
> updateManager.getUpdateRunnableQueue().invokeLater(runnable);
> } catch (Exception ex) {
>
> }
> }
>
> // Register listener method
> public void registerListeners() {
> svgCanvas.addGVTTreeRendererListener(new
> GVTTreeRendererAdapter() {
> public void gvtRenderingCompleted(GVTTreeRendererEvent
e)
> {
> updateManager = svgCanvas.getUpdateManager();
> oAnimEngine.setCustomSVGDocument(rectDoc);
This might be your problem. You should get the Document from the
Canvas rather than remember the document you passed to the canvas. In
some cases the canvas has to clone the document passed to it so that it
is using our DOM Implementation. I would think that your parsing code
would generate a DOM that passes this test but if not this could explain
your problems (you are changing rectDoc, but the canvas is displaying a
separate document cloned from rectDoc).
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org