Saturday, 15 March 2014

java - drawString in BufferedImage disappears -



java - drawString in BufferedImage disappears -

i hope can help me concerning problem in drawstring.

i drawing within mill class , homecoming bufferedimage string on it.

public class mill { public bufferedimage create() { temppanel temppanel = new temppanel(new dimension(100, 100)); homecoming temppanel.createimage(); } private class temppanel extends jpanel { public temppanel(dimension d) { this.setvisible(true); this.setsize(d); } public bufferedimage createimage() { bufferedimage bi = new bufferedimage(this.getsize().getwidth(), this.getsize().getheight(), bufferedimage.translucent); this.paintcomponent(bi.creategraphics()); homecoming bi; } @override public void paintcomponent(graphics g) { super.paintcomponents(g); // general setup graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); // transparent background g2.setcomposite(alphacomposite.clear); g2.fillrect(0, 0, graphicalobject.width, graphicalobject.height); g2.setcomposite(alphacomposite.src); // different settings graphical object g2.setfont(new font("timesroman", font.plain, 12); // actual drawing g2.drawstring("test", 0, 0); } } }

and have jpanel in image should drawn:

public class label extends jpanel { // ... @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; mill factory = new factory(); bufferedimage img = factory.create(); g2.drawimage(img, 0, 0, null); } }

the problem ist text appears if minimize , restore window, start moving mouse within window, text disappears again. when phone call drawrect() instead of drawstring(), works fine?!

can tell me why?

thanks!

there number of things "weird" me out...

firstly, you're creating new graphics context, you're not disposing of it. leaving resources open , may prevent contents been rendered on oss...

so instead of...

bufferedimage bi = new bufferedimage(this.getsize().getwidth(), this.getsize().getheight(), bufferedimage.translucent); this.paintcomponent(bi.creategraphics());

you should doing more like...

bufferedimage bi = new bufferedimage(getwidth(), getheight(), bufferedimage.translucent); graphics2d g2d = bi.creategraphics(); this.paintcomponent(g2d); g2d.dispose();

secondly, text not render same way other elements are, is, doesn't start @ x/y , render down/right, in cases starts @ x/y , renders, slightly, up/right. actual coordinate based on fonts fontmetrics getascent value...

so, instead of...

g2.setfont(new font("timesroman", font.plain, 12)); // actual drawing g2.drawstring("test", 0, 0);

you should doing more like...

g2.setfont(new font("timesroman", font.plain, 12)); fontmetrics fm = g2.getfontmetrics(); // actual drawing g2.drawstring("test", 0, fm.getascent());

see working text apis or more details...

thirdly, jcomponent , extension, extends it, imageobsever, important, not painted ready display immediately, needs farther processing, rather having deal annoyance yourself, can delegate responsibility, instead of...

g2.drawimage(img, 0, 0, null);

you should doing more like...

g2.drawimage(img, 0, 0, this);

this allows component hear underlying image , respond changes might happen image info , update itself.

forthly...

private class temppanel extends jpanel { public temppanel(dimension d) { this.setvisible(true); this.setsize(d); } public bufferedimage createimage() { bufferedimage bi = new bufferedimage(this.getsize().getwidth(), this.getsize().getheight(), bufferedimage.translucent); this.paintcomponent(bi.creategraphics()); homecoming bi; } @override public void paintcomponent(graphics g) { super.paintcomponents(g); // general setup graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); // transparent background g2.setcomposite(alphacomposite.clear); g2.fillrect(0, 0, graphicalobject.width, graphicalobject.height); g2.setcomposite(alphacomposite.src); // different settings graphical object g2.setfont(new font("timesroman", font.plain, 12); // actual drawing g2.drawstring("test", 0, 0); } }

is freaking me out...swing components meant attached native peer onto rendered, optimised not paint when aren't displayable, throwing off rendering...

infact, accomplish same thing using like...

public static class mill { private static renderer renderer; public bufferedimage create() { if (renderer == null) { renderer = new renderer(new dimension(100, 100)); } homecoming renderer.createimage(); } private class renderer { private dimension size; public renderer(dimension size) { this.size = size; } public bufferedimage createimage() { bufferedimage bi = new bufferedimage(size.width, size.height, bufferedimage.translucent); graphics2d g2 = bi.creategraphics(); g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); // transparent background g2.setcomposite(alphacomposite.clear); g2.fillrect(0, 0, graphicalobject.width, graphicalobject.height); g2.setcomposite(alphacomposite.src); // different settings graphical object g2.setfont(new font("timesroman", font.plain, 12)); fontmetrics fm = g2.getfontmetrics(); // actual drawing g2.drawstring("test", 0, fm.getascent()); g2.dispose(); homecoming bi; } } }

which divorce actual painting else , provide command on it...

java swing

No comments:

Post a Comment