java - How to store and mark all mouse-clicked-point on an Image loaded in JPanel? -
up till have used code given below load image in new new jframe , coordinates of pixels of image hard-coded in code itself. however, wish dynamically i.e mark points clicked mouse , store coordinates in array or so.
the code running properly, image chosen using jfilechooser in class , path passed parameter class.
please me out.
public class graphicsset extends jframe{ font f ; bufferedimage baseimage ; file file ; string gimage; jframe imageframe; public graphicsset(string image) { super("frame") ; // imageframe = new jframe("click on select points"); file = new file(image) ; gimage = image; try { baseimage = imageio.read(file) ; } catch(ioexception e) { system.out.println(e) ; } int imagewidth = baseimage.getwidth(); int imageheight = baseimage.getheight(); system.out.println(imagewidth); setbounds(0,0,imagewidth,imageheight) ; setvisible(true) ; //setdefaultcloseoperation (exit_on_close) ; } public void paint(graphics g) { g.drawimage(baseimage, 0, 0, null) ; string[] coordstext = new string[]{ "264.33,329.94","244.24,382.57","243.00,328.88", "264.33,329.94","272.06,331.59","278.30,341.00", "284.28,350.02","282.18,367.78","275.24,375.79", "272.89,378.50","269.26,380.27","266.00,381.66", "259.36,384.50","258.52,383.52","252.00,383.09", "244.24,382.57","238.62,383.56","232.21,377.61", "228.01,373.71","225.52,365.66","226.13,360.00", "226.13,360.00","227.55,354.00","227.55,354.00", "228.20,350.96","227.74,347.67","228.74,345.00", "230.78,339.55","237.90,331.81","243.00,328.88", "248.10,327.42","249.02,328.30","254.00,328.88" }; // polygons stored in instances of path2d.float. after create instance // of path2d.float must set vertices -- easiest way through // moveto(x,y) , lineto(x,y) methods. path2d.float regionofinterest = new path2d.float(); // must store first x,y coordinates can close path, creating line // last point first one. boolean isfirst = true; double firstx=0,firsty=0; // each of x,y coordinates, parse , store them on path2d.float. for(string s:coordstext) { string[] xy = s.split(","); double x = double.parsedouble(xy[0]); double y = double.parsedouble(xy[1]); if (isfirst) { regionofinterest.moveto(x,y); firstx = x; firsty = y; isfirst = false; } else { regionofinterest.lineto(x,y); } } // close path. regionofinterest.lineto(firstx,firsty); // have path define region of interest. in order dim image regions // outside of path must create path contains // region of interest. // first create path whole image -- rectangle image's coordinates. path2d.float pathforwholeimage = new path2d.float(); pathforwholeimage.moveto(0,0); pathforwholeimage.lineto(baseimage.getwidth(),0); pathforwholeimage.lineto(baseimage.getwidth(),baseimage.getheight()); pathforwholeimage.lineto(0,baseimage.getheight()); pathforwholeimage.lineto(0,0); // in order use constructive area geometry (cag) operations must use area class. // first create area path whole image... area wholeimage = new area(pathforwholeimage); // .. subtract region of interest area. wholeimage.subtract(new area(regionofinterest)); // have path2d.float region of interest , area rest of image. // draw , paint them need graphic context, image itself. graphics2d g2d = (graphics2d)baseimage.getgraphics(); // want antialiasing! g2d.setrenderinghint(renderinghints.key_antialiasing,renderinghints.value_antialias_on); // fill rest of image transparent (100/255) white. g2d.setcolor(new color(255,255,255,100)); g2d.fill(wholeimage); // draw region of interest thick, opaque red line. g2d.setstroke(new basicstroke(5f)); g2d.setcolor(new color(255,0,0,200)); g2d.draw(regionofinterest); // create new frame show results. jframe frame = new jframe(); //imageframe.settitle("highlighting image regions"); // create imageicon/label show image. imageicon icon = new imageicon(baseimage); jlabel label = new jlabel(icon); // add content pane. imageframe.getcontentpane().add(new jscrollpane(label)); // set gui parameters. //imageframe.setdefaultcloseoperation(jframe.exit_on_close); imageframe.pack(); //frame.setvisible(true); }
if understood need, help:
arraylist<float> coordsx = new arraylist<float>(); arraylist<float> coordsy = new arraylist<float>(); addmousemotionlistener(this); this.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e){ coordsx.add(e.getx()); //storing coordinate x coordsy.add(e.gety()); //storing coordinate y //add code draw circle each time click. int r = 6; //radius of circle/point. int x = e.getx()-(r/2); //position x (mouse in center of point) int y = e.gety()-(r/2); //position y (mouse in center of point) graphics g = getgraphics(); //getting graphic object g.setcolor(color.red); //setting color red g.filloval(x, y, r, r); //drawing circle/point g.dispose(); } });
just example think solve need, how store coordinates you.
update: in order draw point use filloval(x, y, r, r)
method graphics class.
btw, if use mouseclicked()
event, see how clicks appear not have effect (to click doesn't draw or store anything), because program detecting mousedragged()
event mousemotionlistener interface. solve can change current event mouseclicked()
mousepressed()
event mouselistener interface did in code.
if want bigger point, increase radius, or decrease if want smaller.
Comments
Post a Comment