Canvas vs. JPanel

Hi everyone,

I am new to Swing and Graphics. recently I am trying to design a mutiple window GUI. I use JDesktopPane and JInternalFrame. I try to draw contours onto a Canvas or JPanel whis is itself added to the IInternalFrame.

Since Canvas is AWT so when write pure Swing I should use JPanel, that is my understanding. To my dismay, when I use Canvas and draw the contours by overwritting paint() method my contours can be drawn rather fast, However, when I use JPanel and paintComponent method the drawing is much slower. Worse, when I resize or move the internalFrame the JPanel is really slow, almost frozen.

What is the problem? Any suggestion is greatly appreciated!!

Thanks in advance

Taogo

[736 byte] By [taogoa] at [2007-11-26 20:01:42]
# 1

> when I use JPanel and paintComponent method the drawing is much slower

Yes, painting in Swing is slower than painting in AWT. Its usually not a problem.

Since we don't know what you are doing in your custom painting we can't make any suggestions on how to improve the painting performance.

camickra at 2007-7-9 23:00:26 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks camickr,

The code for paint() when drawing on Canvas and paintComponent when on JPanel are rather simple:

public void paint(Graphics g){//draw onto Canvas

public void paintComponent(Graphics g){//draw onto a JPanel

super.paintComponent(g);//draw onto JPanel

SetMeasurements();

DrawGrid(g, 50);//draw grid using step of 50 pixels.

workSpace = new boolean[workLength]; // workLength = 512 x 1024 x 32

ContourPlot(g, workSpace); // draw contour using a 512x1024 matrix floats

}

void SetMeasurements() {

d = getSize();

d.width -= 2*PLOT_MARGIN;

d.height -= 2*PLOT_MARGIN;

deltaX = d.width / (xSteps - 1.0); //grid interval

deltaY = d.height / (ySteps - 1.0);

}

Other part of codes are identical for either drawing on Canvas or JPanel.

I suspect my paintComponent method has some problem. I am considering first create Image and then use bufferedImage to draw, do you think that will speed up the display of

contours?

Thanks for all the help and

Cheers!!

taogoa at 2007-7-9 23:00:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

> I am considering first create Image and then use bufferedImage to

> draw, do you think that will speed up the display of contours?

I don't know what a contour is, but using a buffered image is a good idea whenever possible.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-9 23:00:26 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks again.

With imageBuffering most of the problems associated with JPanel are gone. However, it

still draw slowly when the figure is rendered for the first time. I mean slower than drawing using Canvas. Otherwise I am pretty happy with it. I almost must use JPanel since I need layeres of other components on the the contour (JPanel) and Canvas likely will block other components.

BTW:

What I try to draw is the visualization of peaks using contour as the peaks shown on maps of mountains for hikers.

taogoa at 2007-7-9 23:00:26 > top of Java-index,Desktop,Core GUI APIs...