java - can't draw on JFrame -


i'm trying make simle java program draws circle @ mouse localization, gets mouse x , y coordinates doesn't draw anything, tried draw string, circle , line nothing worked, changed code bit still doesn't works

class test4 {  public static string a; public static jframe frame = new jframe();     public static point gett(){  pointerinfo h = mouseinfo.getpointerinfo();  point b = h.getlocation();  return b;  }  public void paintcomponent(int x, int y, graphics g) {     g.drawoval(x, y, 10, 10); }  public static void main(string[] args) throws interruptedexception {     int h = 250;     int f = 200;     frame.setvisible(true);     frame.setsize(h, f);     frame.setlocationrelativeto(null);     while(true){     point b = gett();     int x = (int) b.getx();     int y = (int) b.gety();     system.out.println(x);     system.out.println(y);     frame.repaint();}}} 

  • don't perform custom painting directly on jframe. on jcomponent overriding paintcomponent method if can.

  • don't use infinite loop purpose. there mousemotionlistener mouse motion listening


public class test4 {      public static string a;     public static customdrawingpanel content;     public static jframe frame = new jframe();     final static int oval_width = 10;     final static int oval_height = 10;     static int x = -20, y = -20;     public static mousemotionlistener listener = new contentlistener();      public static void main(string[] args) throws interruptedexception {         int h = 250;         int f = 200;         frame.setdefaultcloseoperation(jframe.exit_on_close);          content = new customdrawingpanel();         content.addmousemotionlistener(listener);         frame.add(content);          frame.getcontentpane().setpreferredsize(new dimension(h, f));         frame.pack();         frame.setlocationrelativeto(null);          frame.setvisible(true);     }      //class performs custom drawing     static class customdrawingpanel extends jpanel {          public void paintcomponent(graphics g) {             super.paintcomponent(g);  //always call             g.drawoval(x, y, 10, 10);         }     }      //listener mouse motion     static class contentlistener implements mousemotionlistener {          @override         public void mousedragged(mouseevent e) {             mousemoved(e); //if delete line, when drag circle hang         }          @override         public void mousemoved(mouseevent e) {             x = e.getx() - oval_width / 2;             y = e.gety() - oval_height / 2;             content.repaint();         }     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -