java - repaint() Method Not Repainting JPanel -


ok, first off know there lot of other threads issue, try using invalidate() , validate() had no effect me.

my problem -- title makes obvious -- jpanel's repaint() method doesn't repaint. added debug messages in paintcomponent() method, here's console output:

debug -10 421 010 441 repainted moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse moved mouse 

notice see debug, (co-ordinates), , repainted once, @ beginning of program, yet call repaint(); each time mouse moved.

i added 5 classes one, believe preferred debugging purposes:

package com.trtld.spacewar;  import java.awt.color; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer;  public class groupclass {      private static content content = new content();      public static void main(string[] args) {         jframe window = new jframe("spacewar!");         content.setvisible(true);         window.setcontentpane(content);         mouseinput listener = new mouseinput();         content.addmousemotionlistener(listener);         content.addmouselistener(listener);         window.setsize(600, 480);         window.setlocation(100, 100);         window.setdefaultcloseoperation(jframe.exit_on_close);         window.setvisible(true);         //window.setresizable(false);         system.out.println("debug");     }   }   class target {      private int x1, y1;     private int x2, y2;      public target(int x1, int y1, int x2, int y2){         this.x1 = x1;          this.y1 = y1;         this.x2 = x2;         this.y2 = y2;      }      public int getx1(){         return x1;     }     public int getx2(){         return x2;     }     public int gety1(){         return y1;     }     public int gety2(){         return y2;     } } class content extends jpanel{          private static content instance;         private target[] targets = new target[9];            private bullet[] bullets = new bullet[999];//assuming no more 1000 bullets on screen @                                                   //once (they'd have have fancy-nice auto-clicker ha ha)         int spaceshipx = (getwidth()/2);               public content (){             int ylevel = (((int)math.random())*(getheight() / 4));             int xpos = ((int)math.random())*getwidth();             for(int = 0;!(i == 9); i++){                                         //create randomly located targets                 targets[i] = new target(xpos, ylevel, xpos-15, ylevel-10);             }         }          public void paintcomponent(graphics g){              super.paintcomponent(g);                g.setcolor(color.red);              system.out.println(spaceshipx-10 + " " + (getheight()-20) + " " + spaceshipx+10 + " " + getheight());              system.out.println("repainted");              g.fillrect(spaceshipx+50, getheight()-20 ,spaceshipx+100 , getheight());               /**  //commented out simplicity + needs fixed:                 for(target t : this.targets){        //paint targets                     g.drawrect(t.getx1(), t.gety1(), t.getx2(), t.gety2());                 }                 for(bullet b : this.bullets){         //paint bullets , remove invalid ones plus check hits                     if(b != null){                       g.drawrect(b.getx1(), b.gety1(), b.getx2(), b.gety2());                      if(!(b.checktargetpresence() == null)){                         for(target target : targets){                             if(b.checktargetpresence() == target){                                 target = null;                             }                         }                     }                      if(b.isvalid() == false){                         b = null;                     }                 }                 }*/         }           public static content getinstance(){             if(instance == null) {                 instance = new content();     //todo: singletons out-dated , not preferred; fix                 }                 return instance;         }       //getters , setters:          public bullet[] getbullets(){             return bullets;         }          public target[] gettargets(){             return targets;         }          public void settarget(target t, int loc){             this.targets[loc] = t;         }          public void setbullet(bullet b){             int location = -1;             if(bullets[0] == null){             this.bullets[0] = b;             return;             }             for(bullet bullet: bullets){                 location++;             if(bullets[location] == null){ //todo: messy, clean                 bullets[location] = b;                 return;             }                }          }         public int getspaceshipx(){         return spaceshipx;         }         public void setspaceshipx(int x){             spaceshipx = x-10;             repaint();          }      } class bullet implements actionlistener{      int x1, x2;     int y1 = 10, y2 = 5;      timer timer = new timer(250, this);      public bullet(int x1, int x2){         this.x1 = x1;         this.x2 = x2;         timer.start();     }      public void actionperformed(actionevent evt) {         y1 = y1 + 2;         y2 += 2;         content.getinstance().repaint();     }      public target checktargetpresence(){         for(target t : content.getinstance().gettargets()){             if(x1 <= t.getx1() && x1 >= t.getx2()){                 if(y1 <= t.gety1() && y1 >= t.gety2()){                          //hit!                     return t;                 }             }         }         return null; //todo      }      public boolean isvalid(){//checks if bullet still on screen          if(y1 >= content.getinstance().getheight() || y2 >= content.getinstance().getheight()){             return false;         }else{             return true;         }      }     public int getx1(){         return x1;     }     public int getx2(){         return x2;     }     public int gety1(){         return y1;     }     public int gety2(){         return y2;     } } class mouseinput implements mouselistener, mousemotionlistener {      public void mousedragged(mouseevent arg0) {          }      public void mousemoved(mouseevent evt) {         content.getinstance().setspaceshipx(evt.getx());         content.getinstance().repaint(); // try repaint here , in content class, nothing happens.         system.out.println("moved mouse");     }      public void mouseclicked(mouseevent arg0) {      }      public void mouseentered(mouseevent arg0) {          }      public void mouseexited(mouseevent arg0) {           }      public void mousepressed(mouseevent evt) {       content.getinstance().setbullet(new bullet(evt.getx()-2, evt.getx()+2));     system.out.println("clicked");     }      public void mousereleased(mouseevent arg0) {         }  } 

i appreciate greatly, , if need format differently let me know. please note first gui program.

i think problem create content class , add frame.

however, in mouselistener code invoke getinstance() method of content class , "instance" variable null new content instance created (but never added frame never painted).

so basic logic wrong don't want creating new instance.

instead in constructor of content class do:

instance = this; 

then getinstance() method return variable because have value.

also, mouselisteners should inner classes in of content class. should create , add listener class. when don't need static getinstance() method because can access instance variable directly.

but try using invalidate() , validate() had no effect me.

you should not use methods. used in awt. in swing might use:

revalidate(); repaint(); 

when add components gui after gui visible or when change property in custom class. revalidate() invoke layout manager. repaint() paint component. in class not changing size of need repaint() in setter methods.

edit:

mind sharing code added?

i added 1 line of code:

    public content (){         int ylevel = (((int)math.random())*(getheight() / 4));         int xpos = ((int)math.random())*getwidth();         for(int = 0;!(i == 9); i++)   {               //create randomly located targets             targets[i] = new target(xpos, ylevel, xpos-15, ylevel-10);         }          instance = this; // added     } 

make 1 change @ time. once verified assumption valid adding single line of code. can tidy rest of class. if make multiple changes @ once don't know 1 causing problem.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -