java - paintComponent method not being called by repaint -


i'm trying call paintcomponent method using repaint never being called. first class:

public class start {     public static void main(string[] args){         frame f = new frame();         f.createframe();     } }   

and class want paintcomponent method called happens blank frame appears:

import javax.swing.jbutton; import javax.swing.jcomponent; import java.awt.graphics; import javax.swing.jframe; import java.awt.image.*; import javax.swing.icon; import javax.swing.imageicon; import java.awt.event.*; import java.awt.*; import javax.swing.*; import javax.swing.timer;  public class frame implements runnable,actionlistener { jframe window = new jframe("frame"); int = 0; canvas mycanvas = new canvas(); public void createframe(){     window.setdefaultcloseoperation(jframe.exit_on_close);     window.setbounds(30, 30, 700, 500);     window.setfocusable(true);     window.setfocustraversalkeysenabled(false);     window.setvisible(true);     (new thread(new frame())).start(); }  public void run(){     timer timer = new timer (17,this);     timer.start(); }  public void actionperformed(actionevent e){     mycanvas.updategame();     mycanvas.render();     window.add(mycanvas); } }  class canvas extends jpanel{ int x = 10; int y = 10; public void updategame(){     x++; }  public void render(){     repaint();     system.out.println("repaint"); }  @override protected void paintcomponent(graphics g){     super.paintcomponent(g);     graphics2d g2 = (graphics2d) g;     g.drawstring("hi",x,y);     system.out.println("paint"); } 

}

repaint called multiple times paint never called. why isn't paintcomponent method being called repaint?

instead of creating new frame, pass in existing frame, i've commented line below:

public void createframe(){     window.setdefaultcloseoperation(jframe.exit_on_close);     window.setbounds(30, 30, 700, 500);     window.setfocusable(true);     window.setfocustraversalkeysenabled(false);     window.setvisible(true);     (new thread(new frame())).start(); <--- instead of creating new frame, pass in existing frame } 

you should consider following improvements well:

1) don't name class frame, collides frame

2) move body of createframe following 3 lines:

jframe window = new jframe("frame"); int = 0; canvas mycanvas = new canvas(); 

into constructor , make these local variables canvas member fields.

3) remove last line creates thread out of constructor, , expose method (e.g 'startanimation') , call after you've created object.

edit:

try this:

import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer;      public class frame implements runnable, actionlistener     {         final jframe window;         final canvas mycanvas;          public frame(){             this.window = new jframe("frame");             this.mycanvas = new canvas();             this.window.add(this.mycanvas);             this.window.setdefaultcloseoperation(jframe.exit_on_close);             this.window.setbounds(30, 30, 700, 500);             this.window.setfocusable(true);             this.window.setfocustraversalkeysenabled(false);             this.window.setvisible(true);         }          public void startapp() {             final thread t = new thread(this);             t.start();         }          public void run(){             final timer timer = new timer (1000,this);             timer.start();         }          public void actionperformed(actionevent e){             mycanvas.updategame();             window.repaint();         }          public static void main(string[] args) {             final frame f = new frame();             f.startapp();         }     }      class canvas extends jpanel {         int x = 10;         int y = 10;         public void updategame() {             x++;         }          @override         protected void paintcomponent(graphics g){             super.paintcomponent(g);             graphics2d g2 = (graphics2d) g;             g.drawstring("hi", this.x, this.y);             system.out.println("paint");         }     } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -