image - How to keep and remove multiple graphic objects in java swing applications? -


i have image , use pre-defined positions on create graphic objects colors . mouse click try create ovals on colors . couldn't achieve objective. because , when click on pre-defined position create oval on when click on pre-defined position first oval has gone.

an oval can removed click on twice.

have @ ,

public class printdialog extends javax.swing.jdialog{ private int count = 0; private int count_1 = 0;  /**  * creates new form printfeetdialog  */ public printdialog(java.awt.frame parent, boolean modal) {     super(parent, modal);     initcomponents();     imagepanel panel = new imagepanel("areas.jpg");     this.getcontentpane().add(panel);     this.setresizable(false);     this.setlocationrelativeto(panel);     this.pack(); }  private void formmouseclicked(java.awt.event.mouseevent evt)                                   {                                           // todo add handling code here:     system.out.println("print y - " + evt.gety());     system.out.println("print x - " + evt.getx());      if ((evt.getx() >= 68 && evt.getx() <= 84) && (evt.gety() >= 44 && evt.gety() <= 72))     {         graphics g = getgraphics();         count++;         if (count == 1)         {             g.setcolor(color.red);             g.filloval(66, 52, 20, 20);             //  repaint();         } else if (count > 1)         {             g.setcolor(new color(-3692899));             g.filloval(66, 52, 20, 20);             repaint();             count = 0;         }         g.dispose();     }      if ((evt.getx() >= 137 && evt.getx() <= 157) && (evt.gety() >= 50 && evt.gety() <= 75))     {         graphics g1 = getgraphics();         count_1++;         if (count_1 == 1)         {             g1.setcolor(color.red);             g1.filloval(137, 54, 20, 20);         } else if (count_1 > 1)         {             g1.setcolor(new color(-3692899));             g1.filloval(66, 52, 20, 20);             repaint();             count_1 = 0;         }         g1.dispose();     } } }    

image panel class

public class imagepanel extends jpanel{  private image img;  public imagepanel(string img, string str) {     //this(new imageicon(img).getimage());     }  public imagepanel(string path) {     image img = new imageicon(path).getimage();     this.img = img;     dimension size = new dimension(img.getwidth(null), img.getheight(null));     setpreferredsize(size);     setminimumsize(size);     setmaximumsize(size);     setsize(size);     setlayout(null);       try     {         bufferedimage image = imageio.read(new file(path));         int rgb = image.getrgb(66, 52);         system.out.println("colour is: "+rgb);     }     catch(ioexception e)     {         e.printstacktrace();     } }  public void paintcomponent(graphics g) {     super.paintcomponent(g);     g.drawimage(img, 0, 0, null); } } 

have ideas please ?

thank .

don't use getgraphics, not how custom painting works, see painting in awt , swing , performing custom painting more details

the basic idea is, need kind of list, add each shape. when mouseclicked occurs, iterate through list , check see if mouse clicked occur 1 of shapes, if was, remove shape list, if not, create new shape @ point clicked , add list.

you use list inside paintcomponent method physically paint shapes.

this example extends imagepanel adding in custom painting

import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.shape; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.arraylist; import java.util.iterator; import java.util.list; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class test {      public static void main(string[] args) {         new test();     }      public test() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.add(new drawpane("/volumes/disk02/dropbox/megatokyo/thumnails/0.jpg"));                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class imagepanel extends jpanel {          private image img;          public imagepanel(string img, string str) {             //this(new imageicon(img).getimage());             }          public imagepanel(string path) {             image img = new imageicon(path).getimage();             this.img = img;             try {                 bufferedimage image = imageio.read(new file(path));                 int rgb = image.getrgb(66, 52);                 system.out.println("colour is: " + rgb);             } catch (ioexception e) {                 e.printstacktrace();             }         }          @override         public dimension getpreferredsize() {             return img == null ? new dimension(200, 200) : new dimension(img.getwidth(this), img.getheight(this));                   }          @override         public void paintcomponent(graphics g) {             super.paintcomponent(g);             g.drawimage(img, 0, 0, null);         }     }      public class drawpane extends imagepanel {          private list<shape> shapes;          public drawpane(string img, string str) {             super(img, str);             init();         }          public drawpane(string path) {             super(path);             init();         }          protected void init() {             shapes = new arraylist<>(25);             addmouselistener(new mouseadapter() {                 @override                 public void mouseclicked(mouseevent e) {                     boolean clicked = false;                     iterator<shape> = shapes.iterator();                     while (it.hasnext()) {                         shape shape = it.next();                         if (shape.contains(e.getpoint())) {                             it.remove();                             clicked = true;                             break;                         }                     }                     if (!clicked) {                         shapes.add(new ellipse2d.double(e.getx() - 10, e.gety() - 10, 20, 20));                     }                     repaint();                 }              });         }          @override         public void paintcomponent(graphics g) {             super.paintcomponent(g);              graphics2d g2d = (graphics2d) g.create();             g2d.setcolor(color.red);             (shape shape : shapes) {                 g2d.draw(shape);             }             g2d.dispose();         }      }  } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -