swing - Java repaint() not working on call -


i wonder why repiant() method not working intended more ... ex :

public class main extends jpanel implements actionlistener, mouselistener,mousemotionlistener{  private arraylist<node> nodes; private arraylist<edge> edges; private boolean addnode; private int no_of_nodes; private int width = 30, height = 30;   public static void main(string[] args){     main m = new main();     m.start(); }  public void start() {     nodes = new arraylist<node>();     edges = new arraylist<edge>();     jframe f = new jframe("sfg");     jpanel main_panel = new jpanel(new borderlayout());     jpanel buttons = new jpanel();//buttons containser     jpanel draw = new jpanel();     arraylist<jbutton> bs = new arraylist<jbutton>();     jbutton b1 = new jbutton("add node");     b1.addactionlistener(new add_node());     jbutton b2 = new jbutton("add edge");     b2.addactionlistener(new add_edge());     jbutton b3 = new jbutton("add arc");     b3.addactionlistener(new add_arc());     jbutton b4 = new jbutton("clear all");     b4.addactionlistener(new clear());     jbutton b5 = new jbutton("solve");     b5.addactionlistener(new solve());     bs.add(b1);     bs.add(b2);     bs.add(b3);     bs.add(b4);     bs.add(b5);     (int = 0; < bs.size(); i++) {         buttons.add(bs.get(i));     }     buttons.setbackground(color.gray);     main_panel.add(buttons,borderlayout.south);     draw.setbackground(color.darkgray);     draw.addmousemotionlistener(this);     draw.addmouselistener(this);     main_panel.add(draw);     main_panel.setbackground(color.gray);     f.add(main_panel);     f.setsize(1024, 600);     f.setdefaultcloseoperation(jframe.exit_on_close);     f.setlocationrelativeto(null);     f.setvisible(true); } 

and these methods

public void actionperformed(actionevent arg0) {     this.repaint(); }  public class add_node implements actionlistener{     public void actionperformed(actionevent e) {         system.out.println("add node");         addnode = true;     }  } 

now here when add node , call repaint nothing appears in paint area :

public void mouseclicked(mouseevent arg0) {     // todo auto-generated method stub     if(addnode){         addnode(arg0);         addnode = !addnode;     }     system.out.println(nodes.size());     this.repaint(); }  private void addnode(mouseevent arg0) {     // todo auto-generated method stub     int x = arg0.getx();     int y = arg0.gety();     node n = new node(no_of_nodes++);     n.setx_pos(x);     system.out.println(x + " " + y);     n.setx_pos(y);     nodes.add(n); } 

and that's paint() method not working anymore

        public void paint(graphics g){             super.paintcomponents(g);             fontmetrics f = g.getfontmetrics();             int nodeheight = math.max(height, f.getheight());             system.out.println("in repaint");         (node n : nodes) {                 system.out.println(n.getx_pos() + " " + n.gety_pos());                 int nodewidth = math.max(width, f.stringwidth(integer.tostring(n.getnode_id()))+width/2);                 g.setcolor(color.white);                 g.filloval(n.getx_pos()-nodewidth/2, n.gety_pos()-nodeheight/2, nodewidth, nodeheight);                 g.setcolor(color.black);                 g.drawoval(n.gety_pos()-nodewidth/2, n.gety_pos()-nodeheight/2, nodewidth, nodeheight);                 g.drawstring(integer.tostring(n.getnode_id()) , n.getx_pos()-f.stringwidth(integer.tostring(n.getnode_id()))/2, n.gety_pos()+f.getheight()/2);             }          } 

tia , sorry long question :)

  1. in start method, never add main jframe. based on out of context snippets of code you've provided, i'm left "assume" overriding paint of main class, means, paint never called it's not attached displayable component
  2. start shouldn't creating jframe. should create instance of main , add instance of jframe, 2 should separate. main should created , added main itself
  3. you shouldn't override paint (as general rule), should defiantly not circumvent painting process calling 1 of other paint methods, paintcomponents. instead (again "assuming" you're overriding paint in main), should override paintcomponent method , call super.paintcomponent before custom painting

take @ performing custom painting , painting in awt , swing more details.

also, might have read through code conventions java tm programming language, make easier people read code , read others


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -