codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  repaint() slow performence..  sam_02 at 02:45 on Thursday, September 13, 2007
 

i have 11 arrays of a class called rectangle..

something like this..

rectangle one[];
rectangle two[];
..
..
..
rectangle eleven[];

//each array carries 1500 objects..

one = new rectangle[1500];
..
..
eleven = new rectangle[1500];

// then in a for loop I set them to have a
one[x] = new rectangle(x); // pass value x to object
..
..
eleven[x] = new rectangle(x);
------------------------------------------------
I get no errors in my code, but i noticed that the app is getting alot slower because there are too many objects to paint.. (11 x 1500)

and the biggest problem is that it needs to be repainted about 10 times per second.. since there is a object moving across the screen at 10 frames per second.. which is controlled by a different thread..

here is part of the thread..
------------------------------------------------

public final void run() {

try {
// calculate wait time between frames
double startTime = System.currentTimeMillis();
double sleepTime = 1000f / (float)(framesPerSecond);

// time next frame is to appear
double nextFrame = startTime;

// display the frames

for (int i = 1; i <= 1500; i++) {

// marker moved?
if (Main.timeMarker != i) {
// move the time marker
Main.timeMarker = i;

// force a repaint of the dope sheet
Main.ScrollPane.repaint(x1, y1, x2, y2);

}

// wait for next frame
nextFrame += sleepTime;
while (System.currentTimeMillis() <= nextFrame) {
// sleep a bit
sleep(1);
}
}
}

} catch (Exception e) {
// oops.
}
}


Main.ScrollPane.repaint(x1, y1, x2, y2);- here i pass the values of x1,x2,y1,y2.. so does this mean only that part will me repainted... and nth else.. ??


here is part of the repaint in the Main class..
------------------------------------------------

public void paintComponent(Graphics g) {

// repaint
super.paintComponent(g);

// get size of drawable area
int rectangles = 1500;
int rectangle_width = 10;
int height = 400;
int width = rectangles * rectangle_width; // 1500 * 10

// clear the background?
g.setColor(Color.White);
g.fillRect(0, 0, width, height);

// position of the time marker
int markerAtPixel = (timeMarker - 1) * rectangle_width;

// draw lines (vertical) - BACKGROUND
g.setColor(Color.black);
for (int i = 0; i < 1500; i++) {
// position of line
int x = i * 10;
g.drawLine(x, 0, x, height);

}

// draw lines (horizontal) - BACKGROUND
for (int i = 0; i < 12; i++) {
// position of line
int x = i * 10;
g.drawLine(0, i, width, 170);

}

// time marker - this constantly moves (10 times per second)
g.setColor(Color.red);
g.fillRect(markerAtPixel, 0, rectangle_width, height);

//
for (int x = 0; x <= rectangles; x++){

if (one[x] != null) {
if (one[x].index != 0){
one[x].render(g);
}
}

//..
//..
//..

if (eleven[x] != null) {
if (eleven[x].index != 0){
eleven[x].render(g);
}
}
}

}


i read this page.. http://www2.sys-con.com/ITSG/virtualcd/Java/archives/0611/mcgovern/index.html

I did "Always Override Update"..
any help..


<Added>

i forgot to mention that the object being painted is a JLabel..
so,

// repaint
super.paintComponent(g);

is calling it's super class which extends the jlabel..


  Re: repaint() slow performence..  crwood at 19:11 on Thursday, September 13, 2007
 

Some comments:
A JLabel is non-opaque by default. In Swing (J prefix) a non-opaque component is drawn by the next non-opaque ancestor in the containment hierarchy. Therefore you do not need to try to fill in the background of the non-opaque component, there is nothing to fill in and it cannot do it itself anyway. Calling super.paintComponent(g) does this, ie, has the superclass fill in the default background. The two lines that fill in the background do the smae thing. So unless you have set the labels opaque property to true you can eliminate both of these. If you have set the label opaque and do not need to add text or an ImageIcon you could use a JPanel instead of a JLabel. And you would only need on way of filling the default background, as mentioned above.

Overriding the update method is used in AWT animation to avoid flicker. We do not override update in Swing, the designers recommend we not do this. Swing gives double-buffering by default.

When you have a lot of graphics to draw every time it is often useful to draw it on an offscreen image or backbuffer and simply draw this image in the paintComponent method. I notice you are drawing the 1500 Rectangles on top of your animated/moving graphic/sprite, viz, markerAtPixel. If the rectangles were below the sprite you could draw them once on the back buffer/image. But drawing them on top will be slower. So you might consider drawing these rectangles on a transparent image and drawing this over/after drawing the sprite.
In pseudocode:

int w = component_width, h = component_height;
int type = BufferedImage.TYPE_INT_ARGB_PRE;
BufferedImage rectsImage = new BufferedImage(w, h, type);
Graphics2D g2 = rectsImage.createGraphics();
// draw your 1500 rectangles here using g2...
g2.dispose();
// make this image once, keep it as a member variable and draw it in your
// paintComponent method
protected void paintComponent(Graphics g) {
g.drawImage(backBuffer, 0, 0, this);
// draw your sprite
g.setColor(Color.red);
g.fillRect(markerAtPixel, 0, rectangle_width, height);
g.drawImage(rectsImage, 0, 0, this);
}

This should speed things up quite a bit.
I'd reccommend a slower sleep delay, "1" is pretty small. Your linked article also suggests this.

here i pass the values of x1,x2,y1,y2.. so does this mean only that part will me repainted... and nth else.. ??
Yes, incremental repainting is another technique for improved performance.

Main.ScrollPane.repaint
I would not do any repainting of a scrollPane but repaint the scrollPanes child component, your JLabel or whatever (JPanel, JComponent extension) component you are using for your graphics drawing, ie, the graphics component for the app.

  Re: repaint() slow performence..  sam_02 at 07:50 on Friday, September 14, 2007
 

that certainly did the trick, thanks alot for those tips crwood, might have a little transparency problem, that can be fixed but the performance really improved alot so much faster.

just one question regarding the buffered image.. would it be possible, to take in the buffered image and edit only a part of it. let's say there is a buffered image. and I want to edit an area of 15 x 15 at the coordinates (0, 0, 15, 15). i think the following code should do it?? cause there are some buttons on my gui that allow to change color of some rectangles. so instead of creating a new buffered image. i'd just edit the old one.


Graphics2D g2 = some_buffered_image.getGraphics();
g2.setColor(Color.black);
g2.fillRect(0, 0, 15, 15);


<Added>

opps.. double post..

  Re: repaint() slow performence..  sam_02 at 07:51 on Friday, September 14, 2007
 

that certainly did the trick, thanks alot for those tips crwood, might have a little transparency problem, that can be fixed but the performance really improved alot so much faster.

just one question regarding the buffered image.. would it be possible, to take in the buffered image and edit only a part of it. let's say there is a buffered image. and I want to edit an area of 15 x 15 at the coordinates (0, 0, 15, 15). i think the following code should do it?? cause there are some buttons on my gui that allow to change color of some rectangles. so instead of creating a new buffered image. i'd just edit the old one.


Graphics2D g2 = some_buffered_image.getGraphics();
g2.setColor(Color.black);
g2.fillRect(0, 0, 15, 15);


  Re: repaint() slow performence..  crwood at 16:34 on Friday, September 14, 2007
 


BufferedImage image;

private void editImage(Rectangle r, Color color) {
Graphics2D g2 = image.createGraphics();
g2.setColor(color)
g2.fill(r);
g2.dispose(); // free system resources
}


  Re: repaint() slow performence..  sam_02 at 09:50 on Monday, September 17, 2007
 

will try it out, thanks again. you've helped alot

  Re: repaint() slow performence..  arash farhad at 11:55 on Wednesday, March 10, 2010
 

Ground shaking content i get form your blog. Very nice post about slow performence. Thanks for your beneficial information about testking 1Y0-A08, testking JN0-400 and testking 70-284 certifications. These are the best certifications in the world.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Text wrapping on C# webforms buttons
•  Re: Replacing patterns a^b with pow(a,b)
•  Re: Problem - Object doesn`t support this property or method
•  great sight
•  Re: JavaScript: Not working in Firefox 3.x
•  Re: Difference between two dates including From & To date
•  Re: How to highlight text WITHIN a JTable cell?
•  Re: Javascript: return not in function?
•  Re: How to concatenate a string and a variable using perl?


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2010