Java Swing BufferedImage poor quality -
i'm trying create drawing on bufferedimage , copy in onto jpanel. when draw directly on jpanel quality of picture v.good when using intermediate bufferedimage quality / resolution visibly reduced. i've checked zoom option osx's accessibility panel. i'm developing on macbook pro retina.
is there sort of automated scaling happening? doing wrong bufferedimage?
here's code demonstrating problem
package com.sample.gui; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.geom.ellipse2d; import java.awt.image.bufferedimage; import javax.swing.jframe; import javax.swing.jpanel; public class qualityproblem { private static final double dot_size = 4; public static void main(string[] args) { jframe frame = new jframe("chartpanel demo"); frame.setbackground(color.white); // jpanel draw = new drawingok(); jpanel draw = new drawingugly(); draw.setbackground(color.black); frame.getcontentpane().add(draw, borderlayout.center); frame.setsize(new dimension(1200, 900)); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } private static class drawingok extends jpanel { private static final long serialversionuid = 1l; @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2draw = (graphics2d) g.create(); try { g2draw.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2draw.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality); g2draw.setrenderinghint(renderinghints.key_stroke_control, renderinghints.value_stroke_pure); ellipse2d.double e = new ellipse2d.double(50, 50, dot_size, dot_size); g2draw.setcolor(color.yellow); g2draw.fill(e); } { g2draw.dispose(); } } } private static class drawingugly extends jpanel { private static final long serialversionuid = 1l; @override protected void paintcomponent(graphics g) { super.paintcomponent(g); dimension size = getparent().getsize(); bufferedimage image = new bufferedimage(size.width, size.height, bufferedimage.type_int_rgb); graphics2d ig = image.creategraphics(); ig.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); ig.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality); ig.setrenderinghint(renderinghints.key_stroke_control, renderinghints.value_stroke_pure); graphics2d g2draw = (graphics2d) g.create(); try { ellipse2d.double e = new ellipse2d.double(50, 50, dot_size, dot_size); ig.setcolor(color.yellow); ig.fill(e); g2draw.drawimage(image, 0, 0, null); } { ig.dispose(); g2draw.dispose(); } } } }
edited: added images 4 pixel dot , 50d both zoomed in. ugly 1 comes bufferedimage copied onto screen's graphics
i fixed drawing code.
here's ugly gui.
i moved sizing of panel panel constructor. setting frame size includes borders. setting panel size gives drawing area want.
i moved black background painting paintcomponent method. might painting in 1 place.
i cleaned drawing code. don't need make copy of paintcomponent graphics instance graphics2d.
i made circle bigger see sharpness. moved origin center of circle, , turned dot_size radius.
here's code.
package com.ggl.testing; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.geom.ellipse2d; import java.awt.image.bufferedimage; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; public class qualityproblem implements runnable { private static final double dot_size = 50d; public static void main(string[] args) { swingutilities.invokelater(new qualityproblem()); } @override public void run() { jframe frame = new jframe("chartpanel demo"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setbackground(color.white); // jpanel draw = new drawingok(); jpanel draw = new drawingugly(); frame.getcontentpane().add(draw, borderlayout.center); frame.pack(); frame.setvisible(true); } private class drawingok extends jpanel { private static final long serialversionuid = 1l; public drawingok() { this.setpreferredsize(new dimension(600, 400)); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2draw = (graphics2d) g; g2draw.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2draw.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality); g2draw.setrenderinghint(renderinghints.key_stroke_control, renderinghints.value_stroke_pure); g2draw.setcolor(color.black); g2draw.fillrect(0, 0, getwidth(), getheight()); ellipse2d.double e = new ellipse2d.double(300d - dot_size, 200d - dot_size, dot_size + dot_size, dot_size + dot_size); g2draw.setcolor(color.yellow); g2draw.fill(e); } } private class drawingugly extends jpanel { private static final long serialversionuid = 1l; public drawingugly() { this.setpreferredsize(new dimension(600, 400)); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); bufferedimage image = new bufferedimage(getwidth(), getheight(), bufferedimage.type_int_rgb); graphics2d ig = image.creategraphics(); ig.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); ig.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality); ig.setrenderinghint(renderinghints.key_stroke_control, renderinghints.value_stroke_pure); ig.setcolor(color.black); ig.fillrect(0, 0, getwidth(), getheight()); ellipse2d.double e = new ellipse2d.double(300d - dot_size, 200d - dot_size, dot_size + dot_size, dot_size + dot_size); ig.setcolor(color.yellow); ig.fill(e); ig.dispose(); g.drawimage(image, 0, 0, this); } } }
Comments
Post a Comment