java - JPanel: automatically paint at creation? -


this must rather trivial , straight forward, cannot figure out.

this jpanel looks like, added jframe:

private class radarpanel extends jpanel {                public radarpanel() {             super();             this.repaint();         }          @override         public void paintcomponent(graphics g) {             super.paintcomponent(g);              //painting logic here              //repaint in 500 ms             this.repaint(500);         }     } 

now, when resize jframe jpanel starts getting redrawn time. however, when not resize jframe jpanel's paintcomponent method not seem called, though call repaint in constructor.

any advice? thanks.

update:

more complete code (everything except drawing logic):

import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.util.arraylist;  import javax.swing.jframe; import javax.swing.jpanel;  public class playerradar extends jframe {     private static final long serialversionuid = 230324190;      //settings     private static final int windowwidth = 300;     private static final int windowheight = 300;     private static final int maxdistance = 250;      //components     private playerradar radarwindow;     private jpanel radarpanel;      public playerradar(string title) {         super(title);          //set reference         radarwindow = this;          //create radar window         dimension screensize = java.awt.toolkit.getdefaulttoolkit().getscreensize();         this.setalwaysontop(true);         this.setbackground(new color(0xffffff));         this.setbounds(screensize.width - windowwidth, 0, windowwidth, windowheight);         this.addwindowlistener(new windowadapter() {             public void windowclosing(windowevent e) {                 radarwindow.setvisible(false);             }         });         this.setvisible(true);          //create jpanel drawing         radarpanel = new radarpanel();         radarpanel.setbounds(0, 0, windowwidth, windowheight);         radarpanel.setbackground(new color(0xffffff));          //add frame         this.getcontentpane().add(radarpanel);     }      private class radarpanel extends jpanel {         private static final long serialversionuid = 230324191;         private static final int repaintinterval = 500;          public radarpanel() {             super();         }          @override     public void paint(graphics g) {         super.paint(g);          //draw player oval (center of frame)         g.setcolor(color.blue); //blue         int ovalwidth = (int) math.round(this.getwidth() / 30);         int ovalheight = (int) math.round(this.getheight() / 30);         int playerlocalx = (int) math.round(this.getwidth() / 2);         int playerlocaly = (int) math.round(this.getheight() / 2);         int ovalx = playerlocalx - ovalwidth / 2;         int ovaly = playerlocaly - ovalheight / 2;         g.filloval(ovalx, ovaly, ovalwidth, ovalheight);         g.setcolor(color.black); //black         g.drawoval(ovalx, ovaly, ovalwidth, ovalheight);          //get info of player         playerinfo thisplayer = gameutil.getplayerinfo();         float playerposz = thisplayer.position[0];         float playerposx = thisplayer.position[2];         //float playerrotrad = thisplayer.rotation;          //set rectangle specs         int rectwidth = this.getwidth() / 40;         int rectheight = this.getwidth() / 40;          //only continue if have information our player         if (thisplayer != null) {             //get nearby players             arraylist<playerinfo> playersinfo = gameutil.getnearbyplayers();              //for each other player, draw rectangle             (playerinfo playerinfo : playersinfo) {                                  //get data                 float posz = playerinfo.position[0];                 float posx = playerinfo.position[2];                 //float rotrad = playerinfo.rotation;                  //calculate relative x , y                 int rectx = playerlocalx + math.round((posx - playerposx) / maxdistance * this.getwidth() / 2) - rectwidth / 2;                 int recty = playerlocaly + ovalheight / 2 + math.round((playerposz - posz) / maxdistance * this.getheight() / 2)  - rectheight / 2;                  //draw rectangle                 g.setcolor(color.red);                 g.fillrect(rectx, recty, rectwidth, rectheight);                 g.setcolor(color.black);                 g.drawrect(rectx, recty, rectwidth, rectheight);             }         }          //repaint         this.repaint(repaintinterval);     }     } } 

you correct first time. custom painting done in paintcomponent() method, not paint() method.

you should never invoke repaint() within paintcomponent() method, since result in infinite loop.

if want animate painting, should using swing timer schedule animation.

you should not using use setsize(). job of layout manager. instead can override getpreferredsize() method of panel (or use setpreferredsize()) , can pack() frame, instead of setting size.

the panel should added frame before frame made visible otherwise has size of (0, 0) means there nothing paint.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -