swing - Java: Add 2 animated objects (paintComponent) on the same JPanel -


i have class creates animated object (an wormlike animation) repainting through timer. , class have frame , panel. when create 2 (mov , mov2) instances of object , add panel, appear in separeted panels (or seems like). heres code.

public class movimento extends jcomponent{     int t;       int a;       int[][] matriz;      public movimento(int tamanho, int area){                 t = tamanho;         = area;          gerarmatriz();         gerarpanel();          actionlistener counter = new actionlistener() {             public void actionperformed(actionevent ev){                 movimentarmatriz();                 repaint();             }         };                   new timer(1, counter).start();     }      public void gerarpanel(){         this.setpreferredsize(new dimension(a, a));     }         public void gerarmatriz(){        /*         *generates array[][] initial coordinates         */     }      public void movimentarmatriz(){        /*         * add new coordinate last space of array           */     }      public void paintcomponent(graphics g){         super.paintcomponent(g);         for(int = 0; < matriz.length; i++){             g.drawrect(matriz[i][0],matriz[i][1],1,1);         }     } 

then create new movimento objects here

public class geraimg{     jframe frame = new jframe("gera imagem");     jpanel panel = new jpanel();      movimento mov = new movimento(1000,400);     movimento mov2 = new movimento(100,400);      public geraimg(){         fazerframe();        }      public void fazerframe(){          panel.setopaque(true);         panel.setbackground(new color(150,200,20,255));         panel.add(mov2);         panel.add(mov);          frame.add(panel);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setresizable(false);         frame.pack();         frame.setvisible(true);     }     public static void main(string[] args){         new geraimg();           } } 

then 2 separeted animated panels side side, not 2 worms inside same panel.

is concept totally wrong? thanks

panel.add(mov2); panel.add(mov); 

by default jpanel uses flowlayout, yes 2 components area added side side.

you can try using:

panel.setlayout(null); panel.add(mov2); panel.add(mov); 

this allow components randomly positioned within panel. responsible using setsize(...) , setlocation(...) on component component can painted in panel.

however, better approach create own class implements own painting method. like:

public class movimento() {     ...      public void paintme(graphics g)     {         for(int = 0; < matriz.length; i++)         {             g.drawrect(matriz[i][0],matriz[i][1],1,1);         }     } } 

then create component paint of movimento objects. like:

public class movimentoanimator extends jpanel {     private list<movimento> movimentos = new arraylist<movimento>();      public void addmovimento(movimento movimento)     {         movimentos.add( movimento );     }      @override     protected void paintcomponent(graphics g)     {         super.paintcomponent(g);          (movimento movimento: movimentos         {             movimento.paintme( g );         }     } } 

this class responsible animation.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -