swing - JAVA drawing graphic -


i want draw rectangle on image when mouse button pressed , released. , part works fine. want able see rectangle while drag mouse, lots of rectangles being drawn please help.

class actiontemp implements actionlistener {     public void actionperformed(actionevent e) {         mypanel.addmouselistener(new mouseadapter() {             @override             public void mousepressed(mouseevent event) {                 letsdraw = tempimage.creategraphics();                 point panelpoint = event.getpoint();                 sx = panelpoint.x;                 sy = panelpoint.y;             }             @override             public void mousereleased(mouseevent event) {                 letsdraw.draw(new rectangle2d.float(math.min(sx, curx),                         math.min(sy, cury), math.abs(sx - curx),                         math.abs(sy - cury)));                 letsdraw.dispose();                 mypanel.repaint();             }         });          mypanel.addmousemotionlistener(new mouseadapter() {             @override             public void mousedragged(mouseevent e) {                 point panelpoint = event.getpoint();                 curx = panelpoint.x;                 cury = panelpoint.y;                 letsdraw.draw(new rectangle2d.float(math.min(sx, curx),                         math.min(sy, cury), math.abs(sx - curx),                         math.abs(sy - cury)));                 mypanel.repaint();             }         });     } } 

start having @ painting in awt , swing , performing custom painting.

the basic problem is, painting directly image, means, unless have separate copy, you're compounding each successive rectangle on top of last.

instead, want paint each of them separately, like...

enter image description here

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.rectangle; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.logging.level; import java.util.logging.logger; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class selectionexample {      public static void main(string[] args) {         new selectionexample();     }      public selectionexample() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new borderlayout());                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          private rectangle selection = new rectangle();         private point clickpoint;          private bufferedimage tempimage;          public testpane() {             try {                 tempimage = imageio.read(new file("/users/shane/dropbox/megatokyo/thumnails/2.jpg"));             } catch (ioexception ex) {                 ex.printstacktrace();             }             mouseadapter ma = new mouseadapter() {                  @override                 public void mousedragged(mouseevent e) {                     int minx = math.min(e.getx(), clickpoint.x);                     int miny = math.min(e.gety(), clickpoint.y);                     int maxx = math.max(e.getx(), clickpoint.x);                     int maxy = math.max(e.gety(), clickpoint.y);                      selection.x = minx;                     selection.y = miny;                     selection.width = maxx - minx;                     selection.height = maxy - miny;                     repaint();                 }                  @override                 public void mousepressed(mouseevent e) {                     clickpoint = new point(e.getpoint());                 }              };              addmouselistener(ma);             addmousemotionlistener(ma);         }          @override         public dimension getpreferredsize() {             return tempimage == null ? new dimension(200, 200) : new dimension(tempimage.getwidth(), tempimage.getheight());         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             graphics2d g2d = (graphics2d) g.create();             int x = (getwidth() - tempimage.getwidth()) / 2;             int y = (getheight() - tempimage.getheight()) / 2;             g2d.drawimage(tempimage, x, y, this);             if (selection.width > 0 && selection.height > 0) {                 g2d.setcolor(new color(0, 0, 255, 64));                 g2d.fill(selection);                 g2d.setcolor(color.blue);                 g2d.draw(selection);             }             g2d.dispose();         }     }  } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -