java - How to acces values from within MouseListener -


i trying update value of variable 'dest' mouse location whenever pressed. whenever checking if value has been updated (in run method) see has not been. wrong?

public class boid extends jpanel implements runnable{        private vector2d dest = new vector2d(300,300);      public boid ()                   addmouselistener (new mouseadapter() {                       public void mousepressed (mouseevent e) {                            boid.this.dest = new vector2d(e.getx(), e.gety());                 system.out.println("mouse "+boid.this.squarex);             }         });     }       public void run(){                               system.out.println(dest);            }      public void drawboid(graphics g) {         g.setcolor(color.red);         g.drawline((int)pos.getx(), (int)pos.gety(), (int)dest.getx(), (int)dest.gety());     }  } 

(edited) calling run method in following way. not sure right way, i'm moving processing how thought may work...

public class gui{     private static boid b = new boid();     private static timer tt;          public static void main(string[] args) {                 jframe mainframe = new jframe("boid gui");             mainframe.setlocationrelativeto(null);             mainframe.setresizable(false);             mainframe.setsize(640,480);             mainframe.setdefaultcloseoperation(jframe.exit_on_close);              jpanel contentpanel = new jpanel();             contentpanel.setpreferredsize(new dimension(640,480));              contentpanel.setbackground(color.black);              mainframe.add(contentpanel);             mainframe.setvisible(true);                actionlistener updatedrawing = new actionlistener() {                 public void actionperformed(actionevent e) {                                         b.run();                                            b.drawboid(contentpanel.getgraphics());                  }             };              tt = new timer(50, updatedrawing);             tt.start();     } } 

also tried following main, seems boid.run method 'runs' once:

public static void main(string[] args) {         eventqueue.invokelater(new runnable() {          public void run() {             jframe mainframe = new jframe("boid gui");             mainframe.setlocationrelativeto(null);             mainframe.setresizable(false);             mainframe.setsize(640,480);             mainframe.setdefaultcloseoperation(jframe.exit_on_close);              jpanel contentpanel = new jpanel();             contentpanel.setpreferredsize(new dimension(640,480));              contentpanel.setbackground(color.black);              mainframe.add(contentpanel);             setwindow(mainframe);             mainframe.setvisible(true);                b.run();         }         }); } 

you whole design incorrect. not how custom painting.

  1. there no need timer.
  2. you don't use getgraphics()

i trying update value of variable 'dest' mouse location whenever pressed

some painting basics:

  1. you need override paintcomponent(...) method of panel painting.
  2. in case need properties in class paint. want variables mousex , mousey. need method setmouselocation(x, y) allows change these variables.
  3. when mouse click happens invoke setmouselocation(...) method of panel new values , invoke repaint() on panel.

check out section swing tutorial on custom painting more information , working examples.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -