Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Figure paint causes 100% CPU load
Figure paint causes 100% CPU load [message #220767] Fri, 04 August 2006 12:24 Go to next message
Eclipse UserFriend
Originally posted by: dscholz.htwm.de

Hello,

at first I want to let you know that I'm German. So excuse me if my
English is not perfect.

I'm new to GEF. After reading some articles and tutorials I went to the
shapes example to learn and understand GEF a little bit more. I extended
this example by painting a sine figure depending on its size and the
number of oscillations. I do this by overriding the paint method and
drawing the sine with bezier lines (Path.cubeTo(...)). The figure is
painted properly. You can see that on this picture (3 oscillations):

http://img209.imageshack.us/img209/5725/sinustg7.jpg

All seems ok, but when the figure is drawn, the CPU load goes up to 100%
and stays at this value even after the painting. If I delete the figure or
exit the application everything is ok.

Here is the code of the figure, I think it can be helpful:

package org.eclipse.gef.examples.shapes.model;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Path;

public class SinusFigure extends Figure {

private int oscillations;

public SinusFigure() {
oscillations = 1;
}

public SinusFigure(int osci) {
if (osci > 0)
oscillations = osci;
else
throw (new IllegalArgumentException());
}

public void paint(Graphics g) {
setBackgroundColor(this.getBackgroundColor());
setForegroundColor(this.getForegroundColor());
g.setLineWidth(1);
Path p = new Path(null);
Point top_left = this.getLocation();
Point bottom_right = this.getBounds().getBottomRight();
int diffy = (bottom_right.y - top_left.y)/2;
int ymid = top_left.y + diffy; //middle of y
int diffx = (bottom_right.x - top_left.x)/(2 * oscillations); //distance
between zeroises
int diffamp = diffx/3; //x-distance for controlpoints
int xstart = top_left.x; //x value of the starting point for the next
bezier line
int ystart = ymid; //y value of the starting point for the next bezier
line
int xzero = 0; //the zeroise
int cx1, cy1, cx2, cy2; //the values for the bezier
p.moveTo(xstart, ystart);
for(int i=0; i < oscillations; i++)
{
xstart = top_left.x + (i * 2) * diffx;
xzero = xstart + diffx;
cx1 = xstart + diffamp;
cy1 = top_left.y;
cx2 = xstart + 2 * diffamp;
cy2 = cy1;
p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
xstart = xzero;
xzero = xstart + diffx;
ystart = ymid;
cx1 = xstart + diffamp;
cy1 = bottom_right.y;
cx2 = xstart + 2 * diffamp;
cy2 = cy1;
p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
}
g.drawPath(p);
p.dispose();
Rectangle rect = this.getBounds().getCopy();
rect.width = rect.width - (rect.right() - xzero); //equalize the width
of bounds-rectangle to the width of the sinus
this.setBounds(rect);
}
}

I assume that the figure is repainted the whole time or something similar
which causes the 100% CPU load. But for what reason? What's wrong? I hope
it's not completely wrong ;-) Thanks for your help.

BTW: I'm using Eclipse + GEF 3.2 and the Java 1.4.2 SDK.
Re: Figure paint causes 100% CPU load [message #220776 is a reply to message #220767] Fri, 04 August 2006 13:04 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ingo.koch[nospam].sap.com

you call:
//...
setBackgroundColor(this.getBackgroundColor());
setForegroundColor(this.getForegroundColor());
//...
in the paint method.
this re"paint"s the figure...

by the way, these statements do exactly... nothing
did you mean: graphics.setForegroundColor(this.getForegroundColor()); ?


Regards, Ingo



"Daniel Scholz" <dscholz@htwm.de> wrote in message
news:ebf6ea13184c72e5e5cb28deabb8488d$1@www.eclipse.org...
> Hello,
>
> at first I want to let you know that I'm German. So excuse me if my
> English is not perfect.
>
> I'm new to GEF. After reading some articles and tutorials I went to the
> shapes example to learn and understand GEF a little bit more. I extended
> this example by painting a sine figure depending on its size and the
> number of oscillations. I do this by overriding the paint method and
> drawing the sine with bezier lines (Path.cubeTo(...)). The figure is
> painted properly. You can see that on this picture (3 oscillations):
>
> http://img209.imageshack.us/img209/5725/sinustg7.jpg
>
> All seems ok, but when the figure is drawn, the CPU load goes up to 100%
> and stays at this value even after the painting. If I delete the figure or
> exit the application everything is ok.
>
> Here is the code of the figure, I think it can be helpful:
>
> package org.eclipse.gef.examples.shapes.model;
>
> import org.eclipse.draw2d.Figure;
> import org.eclipse.draw2d.Graphics;
> import org.eclipse.draw2d.geometry.Point;
> import org.eclipse.draw2d.geometry.Rectangle;
> import org.eclipse.swt.graphics.Path;
>
> public class SinusFigure extends Figure {
>
> private int oscillations;
>
> public SinusFigure() {
> oscillations = 1;
> }
>
> public SinusFigure(int osci) {
> if (osci > 0)
> oscillations = osci;
> else
> throw (new IllegalArgumentException());
> }
>
> public void paint(Graphics g) {
> setBackgroundColor(this.getBackgroundColor());
> setForegroundColor(this.getForegroundColor());
> g.setLineWidth(1);
> Path p = new Path(null);
> Point top_left = this.getLocation();
> Point bottom_right = this.getBounds().getBottomRight();
> int diffy = (bottom_right.y - top_left.y)/2;
> int ymid = top_left.y + diffy; //middle of y
> int diffx = (bottom_right.x - top_left.x)/(2 * oscillations); //distance
> between zeroises
> int diffamp = diffx/3; //x-distance for controlpoints
> int xstart = top_left.x; //x value of the starting point for the next
> bezier line
> int ystart = ymid; //y value of the starting point for the next bezier
> line
> int xzero = 0; //the zeroise
> int cx1, cy1, cx2, cy2; //the values for the bezier
> p.moveTo(xstart, ystart);
> for(int i=0; i < oscillations; i++)
> {
> xstart = top_left.x + (i * 2) * diffx;
> xzero = xstart + diffx;
> cx1 = xstart + diffamp;
> cy1 = top_left.y;
> cx2 = xstart + 2 * diffamp;
> cy2 = cy1;
> p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
> xstart = xzero;
> xzero = xstart + diffx;
> ystart = ymid;
> cx1 = xstart + diffamp;
> cy1 = bottom_right.y;
> cx2 = xstart + 2 * diffamp;
> cy2 = cy1;
> p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
> }
> g.drawPath(p);
> p.dispose();
> Rectangle rect = this.getBounds().getCopy();
> rect.width = rect.width - (rect.right() - xzero); //equalize the width
> of bounds-rectangle to the width of the sinus
> this.setBounds(rect);
> }
> }
>
> I assume that the figure is repainted the whole time or something similar
> which causes the 100% CPU load. But for what reason? What's wrong? I hope
> it's not completely wrong ;-) Thanks for your help.
>
> BTW: I'm using Eclipse + GEF 3.2 and the Java 1.4.2 SDK.
>
Re: Figure paint causes 100% CPU load [message #220797 is a reply to message #220776] Fri, 04 August 2006 13:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dscholz.htwm.de

Hello Ingo,

your answer is exactly the right solution. I changed the 2 lines with the
set...Color(...) methods to this:

g.setBackgroundColor(this.getBackgroundColor());
g.setForegroundColor(this.getForegroundColor());

Everything is fine now. It was a small mistake but of course it's a big
difference between the figure and the graphics object. Thank you very much
for your help.

Regards, Daniel

Ingo Koch wrote:

> you call:
> //...
> setBackgroundColor(this.getBackgroundColor());
> setForegroundColor(this.getForegroundColor());
> //...
> in the paint method.
> this re"paint"s the figure...

> by the way, these statements do exactly... nothing
> did you mean: graphics.setForegroundColor(this.getForegroundColor()); ?


> Regards, Ingo



> "Daniel Scholz" <dscholz@htwm.de> wrote in message
> news:ebf6ea13184c72e5e5cb28deabb8488d$1@www.eclipse.org...
>> Hello,
>>
>> at first I want to let you know that I'm German. So excuse me if my
>> English is not perfect.
>>
>> I'm new to GEF. After reading some articles and tutorials I went to the
>> shapes example to learn and understand GEF a little bit more. I extended
>> this example by painting a sine figure depending on its size and the
>> number of oscillations. I do this by overriding the paint method and
>> drawing the sine with bezier lines (Path.cubeTo(...)). The figure is
>> painted properly. You can see that on this picture (3 oscillations):
>>
>> http://img209.imageshack.us/img209/5725/sinustg7.jpg
>>
>> All seems ok, but when the figure is drawn, the CPU load goes up to 100%
>> and stays at this value even after the painting. If I delete the figure or
>> exit the application everything is ok.
>>
>> Here is the code of the figure, I think it can be helpful:
>>
>> package org.eclipse.gef.examples.shapes.model;
>>
>> import org.eclipse.draw2d.Figure;
>> import org.eclipse.draw2d.Graphics;
>> import org.eclipse.draw2d.geometry.Point;
>> import org.eclipse.draw2d.geometry.Rectangle;
>> import org.eclipse.swt.graphics.Path;
>>
>> public class SinusFigure extends Figure {
>>
>> private int oscillations;
>>
>> public SinusFigure() {
>> oscillations = 1;
>> }
>>
>> public SinusFigure(int osci) {
>> if (osci > 0)
>> oscillations = osci;
>> else
>> throw (new IllegalArgumentException());
>> }
>>
>> public void paint(Graphics g) {
>> setBackgroundColor(this.getBackgroundColor());
>> setForegroundColor(this.getForegroundColor());
>> g.setLineWidth(1);
>> Path p = new Path(null);
>> Point top_left = this.getLocation();
>> Point bottom_right = this.getBounds().getBottomRight();
>> int diffy = (bottom_right.y - top_left.y)/2;
>> int ymid = top_left.y + diffy; //middle of y
>> int diffx = (bottom_right.x - top_left.x)/(2 * oscillations); //distance
>> between zeroises
>> int diffamp = diffx/3; //x-distance for controlpoints
>> int xstart = top_left.x; //x value of the starting point for the next
>> bezier line
>> int ystart = ymid; //y value of the starting point for the next bezier
>> line
>> int xzero = 0; //the zeroise
>> int cx1, cy1, cx2, cy2; //the values for the bezier
>> p.moveTo(xstart, ystart);
>> for(int i=0; i < oscillations; i++)
>> {
>> xstart = top_left.x + (i * 2) * diffx;
>> xzero = xstart + diffx;
>> cx1 = xstart + diffamp;
>> cy1 = top_left.y;
>> cx2 = xstart + 2 * diffamp;
>> cy2 = cy1;
>> p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
>> xstart = xzero;
>> xzero = xstart + diffx;
>> ystart = ymid;
>> cx1 = xstart + diffamp;
>> cy1 = bottom_right.y;
>> cx2 = xstart + 2 * diffamp;
>> cy2 = cy1;
>> p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
>> }
>> g.drawPath(p);
>> p.dispose();
>> Rectangle rect = this.getBounds().getCopy();
>> rect.width = rect.width - (rect.right() - xzero); //equalize the width
>> of bounds-rectangle to the width of the sinus
>> this.setBounds(rect);
>> }
>> }
>>
>> I assume that the figure is repainted the whole time or something similar
>> which causes the 100% CPU load. But for what reason? What's wrong? I hope
>> it's not completely wrong ;-) Thanks for your help.
>>
>> BTW: I'm using Eclipse + GEF 3.2 and the Java 1.4.2 SDK.
>>
Re: Figure paint causes 100% CPU load [message #220822 is a reply to message #220797] Fri, 04 August 2006 16:52 Go to previous message
Eclipse UserFriend
Originally posted by: none.unknown.com

You should be overriding paintFigure(), not paint(). Then you won't have to
set the colors yourself, among other things.

"Daniel Scholz" <dscholz@htwm.de> wrote in message
news:8a4bf93a1cdd0a2855f062c1d9f0d10d$1@www.eclipse.org...
> Hello Ingo,
> your answer is exactly the right solution. I changed the 2 lines with the
> set...Color(...) methods to this:
>
> g.setBackgroundColor(this.getBackgroundColor());
> g.setForegroundColor(this.getForegroundColor());
>
> Everything is fine now. It was a small mistake but of course it's a big
> difference between the figure and the graphics object. Thank you very much
> for your help.
>
> Regards, Daniel
>
> Ingo Koch wrote:
>
>> you call:
>> //...
>> setBackgroundColor(this.getBackgroundColor());
>> setForegroundColor(this.getForegroundColor());
>> //...
>> in the paint method.
>> this re"paint"s the figure...
>
>> by the way, these statements do exactly... nothing
>> did you mean: graphics.setForegroundColor(this.getForegroundColor()); ?
>
>
>> Regards, Ingo
>
>
>
>> "Daniel Scholz" <dscholz@htwm.de> wrote in message
>> news:ebf6ea13184c72e5e5cb28deabb8488d$1@www.eclipse.org...
>>> Hello,
>>>
>>> at first I want to let you know that I'm German. So excuse me if my
>>> English is not perfect.
>>>
>>> I'm new to GEF. After reading some articles and tutorials I went to the
>>> shapes example to learn and understand GEF a little bit more. I extended
>>> this example by painting a sine figure depending on its size and the
>>> number of oscillations. I do this by overriding the paint method and
>>> drawing the sine with bezier lines (Path.cubeTo(...)). The figure is
>>> painted properly. You can see that on this picture (3 oscillations):
>>>
>>> http://img209.imageshack.us/img209/5725/sinustg7.jpg
>>>
>>> All seems ok, but when the figure is drawn, the CPU load goes up to 100%
>>> and stays at this value even after the painting. If I delete the figure
>>> or
>>> exit the application everything is ok.
>>>
>>> Here is the code of the figure, I think it can be helpful:
>>>
>>> package org.eclipse.gef.examples.shapes.model;
>>>
>>> import org.eclipse.draw2d.Figure;
>>> import org.eclipse.draw2d.Graphics;
>>> import org.eclipse.draw2d.geometry.Point;
>>> import org.eclipse.draw2d.geometry.Rectangle;
>>> import org.eclipse.swt.graphics.Path;
>>>
>>> public class SinusFigure extends Figure {
>>>
>>> private int oscillations;
>>>
>>> public SinusFigure() {
>>> oscillations = 1;
>>> }
>>>
>>> public SinusFigure(int osci) {
>>> if (osci > 0)
>>> oscillations = osci;
>>> else
>>> throw (new IllegalArgumentException());
>>> }
>>>
>>> public void paint(Graphics g) {
>>> setBackgroundColor(this.getBackgroundColor());
>>> setForegroundColor(this.getForegroundColor());
>>> g.setLineWidth(1);
>>> Path p = new Path(null);
>>> Point top_left = this.getLocation();
>>> Point bottom_right = this.getBounds().getBottomRight();
>>> int diffy = (bottom_right.y - top_left.y)/2;
>>> int ymid = top_left.y + diffy; //middle of y
>>> int diffx = (bottom_right.x - top_left.x)/(2 * oscillations); //distance
>>> between zeroises
>>> int diffamp = diffx/3; //x-distance for controlpoints
>>> int xstart = top_left.x; //x value of the starting point for the next
>>> bezier line
>>> int ystart = ymid; //y value of the starting point for the next bezier
>>> line
>>> int xzero = 0; //the zeroise
>>> int cx1, cy1, cx2, cy2; //the values for the bezier
>>> p.moveTo(xstart, ystart);
>>> for(int i=0; i < oscillations; i++)
>>> {
>>> xstart = top_left.x + (i * 2) * diffx;
>>> xzero = xstart + diffx;
>>> cx1 = xstart + diffamp;
>>> cy1 = top_left.y;
>>> cx2 = xstart + 2 * diffamp;
>>> cy2 = cy1;
>>> p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
>>> xstart = xzero;
>>> xzero = xstart + diffx;
>>> ystart = ymid;
>>> cx1 = xstart + diffamp;
>>> cy1 = bottom_right.y;
>>> cx2 = xstart + 2 * diffamp;
>>> cy2 = cy1;
>>> p.cubicTo(cx1, cy1, cx2, cy2, xzero, ymid);
>>> }
>>> g.drawPath(p);
>>> p.dispose();
>>> Rectangle rect = this.getBounds().getCopy();
>>> rect.width = rect.width - (rect.right() - xzero); //equalize the width
>>> of bounds-rectangle to the width of the sinus
>>> this.setBounds(rect);
>>> }
>>> }
>>>
>>> I assume that the figure is repainted the whole time or something
>>> similar
>>> which causes the 100% CPU load. But for what reason? What's wrong? I
>>> hope
>>> it's not completely wrong ;-) Thanks for your help.
>>>
>>> BTW: I'm using Eclipse + GEF 3.2 and the Java 1.4.2 SDK.
>>>
>
Previous Topic:Trouble with GEF and update in RCP application
Next Topic:question about move editpart
Goto Forum:
  


Current Time: Wed Apr 24 16:16:35 GMT 2024

Powered by FUDForum. Page generated in 0.03298 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top