java - save Image as PNG -
i'd save array point chart image(png), java program can show schatter diagram don't know how save it. perhaps imageio.write, , how? can give me advice solve problem. thank you
public class graph extends application { public void start(stage primarystage) { pane root = new pane(); int[] mydata = { 12, 9, 0, 1, 38, 19, 21, 72, 33, 83, 14, 10, 65, 46, 10, 17, 27, 38, 65, 98, 8, 58, 38, 79, 37, 69, 26, 15}; numberaxis xaxis = new numberaxis(); numberaxis yaxis = new numberaxis(); scatterchart scatterchart=new scatterchart(xaxis,yaxis); xychart.series data=new xychart.series(); (int i=0; i< mydata.length; i++) { data.getdata().add(new xychart.data(i,mydata[i])); } scatterchart.getdata().addall(data); root.getchildren().add(scatterchart); scene scene = new scene(root, 600, 400); primarystage.setscene(scene); primarystage.show(); file file = new file("c:\\images\\image.png"); // , ????? } public static void main(string[] args) { launch(args); } }
you can draw plots or diagram onto image bufferedimage. save bufferedimage file after that. can try that:
class myscatterplot { private bufferedimage buf; //constructors, initializations not shown. public void savebufferasimage(string pathname){ string fmt = ""; if(!(pathname == null || pathname.equals(""))){ fmt = pathname.substring(pathname.lastindexof(".")+1); file outputfile = new file(pathname); try{ imageio.write(buf, fmt, outputfile); }catch(ioexception ioe){system.out.println("unable save file");} } } public void drawimage(){ buf = new bufferedimage (200, 200, bufferedimage.type_int_argb); graphics2d g2d = buf.creategraphics(); g2d.fillrect(10, 10, 150, 150); //draw image here (example only) g2d.dispose(); } }
to save diagram image file (such .png
):
myscatterplot plot = new myscatterplot(); plot.drawimage(); plot.savebufferasimage("mygraph.png");
Comments
Post a Comment