java - JavaFx draw Image inside in Pane -
hi want drawing image inside in pane , when bounds heigh want cut image. image consists of tile. 1 tile size 256. full image bigger tham pane. don't know how can cut image. hi want draw 1 large image, consists of tiles. 1 tile has dimensions of 256x256. image in pane. @ time, image dimensions larger dimension pane. not know how draw in pane. help
public class testurlimage8 { public static arraylist<busstop2> list = new arraylist<>(); public static arraylist<positiontilesandurlpaths> positiontilesandurlpathslist = new arraylist<>(); public static hashmap<string, image> imgcache = new hashmap<>(); public static double lat; public static double lon; public static double deltay; public static double deltax; public static double positionx; public static double positiony; public static int[] imagecount = getcountimage(); public static int [] countimage = countimage(); public static int []x = new int [countimage[0]]; public static int []y = new int [countimage[1]]; private file file = new file("c:/users/022/workspace22/ekranlcd/res/images/kropka.png"); private image bus = new image(file.touri().tostring()); static arraylist<utltoimageconverter> threadlist = new arraylist<>(); public testurlimage8(pane pane) { } /** * method use count of image need * @return */ private static int[] getcountimage(){ int ximagecount = (int) math.ceil(main4.width/256); int yimagecount = (int) math.ceil(main4.height/256); return new int[] {ximagecount, yimagecount}; } /** * method use count of tiles * @return */ public static int[] countimage(){ int ximagecount = imagecount[0]; int yimagecount = imagecount[1]; if(ximagecount-1 %2 != 0){ ximagecount = ximagecount + 2; } if(yimagecount-1 %2 != 0){ yimagecount = yimagecount + 2; } return new int[] {ximagecount, yimagecount}; } /** * method use tiles * @param lat * @param lon * @return */ private static arraylist<busstop2> gettiles(double lat, double lon ){ int [] numbertile = gettilenumber(lat, lon, config.mapzoom); int a1 = 1; int a2 = 1; int a3 = 1; int a4 = 1; x[0] = numbertile[0]; y[0] = numbertile[1]; (int = 1; i<x.length; i++){ if(i%2==0){ x[i] = numbertile[0]+(a1); a1++; } else{ x[i] = numbertile[0]-(a2); a2++; } } (int = 1; i<y.length; i++){ if(i%2==0){ y[i] = numbertile[1]+(a3); a3++; } else{ y[i] = numbertile[1]-(a4); a4++; } } for(int = 0 ; i<x.length ; i++){ (int j = 0 ;j<y.length ; j++ ){ list.add(new busstop2(x[i], y[j], x[0] - x[i], y[0]-y[j])); } } return list; } /** * * @param list * @return */ private static arraylist<positiontilesandurlpaths> getimgpositionandurlspath(arraylist<busstop2> list){ for(busstop2 bus : list){ positiontilesandurlpathslist.add(new positiontilesandurlpaths(256*bus.getx(), 256*bus.gety(), config.mappath + "/" + bus.geta() + "/" + bus.getb() + ".png")); } return positiontilesandurlpathslist; } public static int [] gettilenumber(final double lat, final double lon, final int zoom) { int xtile = (int)math.floor( (lon + 180) / 360 * (1<<zoom) ) ; int ytile = (int)math.floor( (1 - math.log(math.tan(math.toradians(lat)) + 1 / math.cos(math.toradians(lat))) / math.pi) / 2 * (1<<zoom) ) ; if (xtile < 0) xtile=0; if (xtile >= (1<<zoom)) xtile=((1<<zoom)-1); if (ytile < 0) ytile=0; if (ytile >= (1<<zoom)) ytile=((1<<zoom)-1); return new int[] {xtile, ytile}; } static double tile2lon(int x, int z) { return x / math.pow(2.0, z) * 360.0 - 180; } static double tile2lat(int y, int z) { double n = math.pi - (2.0 * math.pi * y) / math.pow(2.0, z); return math.todegrees(math.atan(math.sinh(n))); } public void start(pane pane ,double lat, double lon) throws exception { int [] tiles= gettilenumber(lat, lon, config.mapzoom); canvas canvas = new canvas(config.xsize, config.ysize); graphicscontext gc = canvas.getgraphicscontext2d(); int [] aa =gettilenumber(lat,lon, config.mapzoom); gettiles(lat,lon); getimgpositionandurlspath(list); executorservice executor = executors.newfixedthreadpool(10); arraylist<utltoimageconverter2> threadlist = new arraylist<>(); for(positiontilesandurlpaths url : positiontilesandurlpathslist){ threadlist.add(new utltoimageconverter2(url.getpath())); } try { executor.invokeall(threadlist); } catch (interruptedexception e1) { e1.printstacktrace(); } system.out.println(imgcache.size()); system.out.println( aa[0] + " " + aa[1] ); deltax = tile2lon(tiles[0] + 1 , config.mapzoom) - tile2lon(tiles[0], config.mapzoom); deltay = tile2lat(tiles[1], config.mapzoom) - tile2lat(tiles[1] + 1 , config.mapzoom); positionx = (lon - tile2lon(tiles[0], config.mapzoom)) * config.imgsize/deltax; positiony = (tile2lat(tiles[1], config.mapzoom) - lat) * config.imgsize/deltay; gc.drawimage(bus,847.0-100 ,621.0-100); gc.stroketext("aalala", 847.0-10 ,621.0-10); for(positiontilesandurlpaths pos : getimgpositionandurlspath(list)){ gc.drawimage(imgcache.get(pos.getpath()),config.xsize/2-pos.getx()-config.imgsize/2 ,(config.ysize/2)- pos.gety()-config.imgsize/2, config.imgsize, config.imgsize); system.out.println(pos.getx() + " " + pos.gety()); } gc.drawimage(bus,config.xsize/2-config.imgsize/2-config.markwidth/2+positionx, config.ysize/2+positiony-config.imgsize/2-config.markheight/2, config.markwidth, config.markheight); pane.getchildren().add(canvas); } @suppresswarnings("unused") public static void clear(){ for(positiontilesandurlpaths url : positiontilesandurlpathslist){ url = null; } positiontilesandurlpathslist.clear(); threadlist.clear(); for(utltoimageconverter utl : threadlist){ utl = null; } for(busstop2 bus :list){ bus = null; } list.clear(); } }
there multiple options display part of image in javafx:
when using canvas
node, use the correct drawimage
, i.e. 1 not scale image target rectangle. e.g.
rectangle2d rectinsource = ... rectangle2d targetrect = ... gc.drawimage(image, rectinsource.getminx(), rectinsource.getminy(), rectinsource.getwidth(), rectinsource.getheight(), targetrect.getminx(), targetrect.getminy(), targetrect.getwidth(), targetrect.getheight());
alternatively use imageview
displaying part of image
setting viewport
property accordingly:
imageview imageview = new imageview(image); imageview.setviewport(rectinsource); // ----------- required, if rescaling desired ----------- imageview.setfitwidth(targetrect.getwidth()); imageview.setfitheight(targetrect.getheight()); // -------------------------------------------------------------- imageview.relocate(targetrect.getminx(), targetrect.getminy()); pane.getchildren().add(imageview);
rectinsource
denotes part of image should displayed, targetrect
position should drawn.
Comments
Post a Comment