java - Create VolatileImage always return null -
i creating volatile image in following class, hitting nullpointerexception every time. reason why createvolatileimage() returning null (from javadoc) graphicsenvironment.isheadless() true.
import java.awt.graphics; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.image.volatileimage; import javax.swing.jpanel; public class tube extends jpanel { private volatileimage image; private int width, height; public tube() { super(true); } public tube(int width, int height) { this.width = width; this.height = height; createimage(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if(image == null) createimage(); if(image.validate(getgraphicsconfiguration()) == volatileimage.image_incompatible) createimage(); graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_lcd_hrgb); g2.drawimage(image, 0, 0, this); } public volatileimage getbufferedimage() { return this.image; } private void createimage(){ if(image != null){ image.flush(); image = null; } image = createvolatileimage(width, height); image.setaccelerationpriority(1.0f); } }
how can return non-null volatileimage ??
this change headless mode:
system.setproperty("java.awt.headless", "false");
Comments
Post a Comment