diff options
Diffstat (limited to 'libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java')
-rw-r--r-- | libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java | 94 |
1 files changed, 79 insertions, 15 deletions
diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java index d80306c8a82..d7217aa7a3f 100644 --- a/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GdkGraphics.java @@ -48,7 +48,6 @@ import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Shape; -import java.awt.SystemColor; import java.awt.image.ImageObserver; import java.text.AttributedCharacterIterator; @@ -81,18 +80,23 @@ public class GdkGraphics extends Graphics native void initStateUnlocked (GtkComponentPeer component); native void initState (int width, int height); native void initFromImage (GtkImage image); - native void copyState (GdkGraphics g); + native void nativeCopyState (GdkGraphics g); + + /** + * A cached instance that is used by {@link #create} in order to avoid + * massive allocation of graphics contexts. + */ + GdkGraphics cached = null; + + /** + * A link to the parent context. This is used in {@link #dispose} to put + * this graphics context into the cache. + */ + GdkGraphics parent = null; GdkGraphics (GdkGraphics g) { - color = g.color; - xorColor = g.xorColor; - font = g.font; - if (font == null) - font = new Font ("Dialog", Font.PLAIN, 12); - clip = new Rectangle (g.clip); - component = g.component; - + parent = g; copyState (g); } @@ -162,12 +166,54 @@ public class GdkGraphics extends Graphics public native void copyArea(int x, int y, int width, int height, int dx, int dy); - public Graphics create () + /** + * Creates a copy of this GdkGraphics instance. This implementation can + * reuse a cached instance to avoid massive instantiation of Graphics objects + * during painting. + * + * @return a copy of this graphics context + */ + public Graphics create() + { + GdkGraphics copy = cached; + if (copy == null) + copy = new GdkGraphics(this); + else + { + copy.copyState(this); + cached = null; + } + return copy; + } + + public native void nativeDispose(); + + /** + * Disposes this graphics object. This puts this graphics context into the + * cache of its parent graphics if there is one. + */ + public void dispose() { - return new GdkGraphics (this); + if (parent != null) + { + parent.cached = this; + parent = null; + } + else + nativeDispose(); } - public native void dispose(); + /** + * This is called when this object gets finalized by the garbage collector. + * In addition to {@link Graphics#finalize()} this calls nativeDispose() to + * make sure the native resources are freed before the graphics context is + * thrown away. + */ + public void finalize() + { + super.finalize(); + nativeDispose(); + } public boolean drawImage (Image img, int x, int y, Color bgcolor, ImageObserver observer) @@ -248,9 +294,8 @@ public class GdkGraphics extends Graphics public void drawString (String str, int x, int y) { drawString(getFontPeer(), str, x, y); - } + } - public void drawString (AttributedCharacterIterator ci, int x, int y) { throw new Error ("not implemented"); @@ -419,4 +464,23 @@ public class GdkGraphics extends Graphics translateNative (x, y); } + + /** + * Copies over the state of another GdkGraphics to this instance. This is + * used by the {@link #GdkGraphics(GdkGraphics)} constructor and the + * {@link #create()} method. + * + * @param g the GdkGraphics object to copy the state from + */ + private void copyState(GdkGraphics g) + { + color = g.color; + xorColor = g.xorColor; + font = g.font; + if (font == null) + font = new Font ("Dialog", Font.PLAIN, 12); + clip = new Rectangle (g.clip); + component = g.component; + nativeCopyState(g); + } } |