java - why is it only painting one of the components? -


package puzzle;  import java.awt.color; import javax.swing.*; import java.util.*;   public class puzzle {  integer x = 50; integer y = 50; int x2 = 100; int y2 = 100; int vnotx = 0; int vnoty = 0; float t = 0; float t2  = 0;  jframe frame = new jframe(); move m = new move(x, y,color.green); move n = new move(x,y,color.orange); java.util.timer timer = new java.util.timer(); java.util.timer timer2 = new java.util.timer();  public puzzle() {      frame.setundecorated(true);     frame.setsize(400, 400);     frame.add(m);     frame.add(n);     frame.addkeylistener(new java.awt.event.keyadapter() {          public void keypressed(java.awt.event.keyevent evt) {             formkeypressed(evt);         }     });      com.sun.awt.awtutilities.setwindowopacity(frame, 1f);     timer.scheduleatfixedrate(new timertask() {          public void run() {             rd();         }     }, 0, 20);         timer2.scheduleatfixedrate(new timertask() {          public void run() {           t2+=.01;               x2 =(int) ((math.cos(t2)+1)*100);               y2 =(int) ((math.sin(t2)+1)*100);               system.out.println(x2+", "+y2);         }     }, 0, 2); } int z = 0;  public void rd() {    z++;      m = new move(x, y, color.green);     n = new move(x2, y2, color.orange);     frame.add(n);     frame.add(m);      frame.validate();  }  private void formkeypressed(java.awt.event.keyevent evt) {      int id = evt.getid();     int kc = evt.getkeycode();     if (kc == 39) {         final java.util.timer right = new java.util.timer();         right.scheduleatfixedrate(new timertask() {              public void run() {                 t += .01;                 int x1 = x;                 if (x + 51 < 400) {                     x = x1 + (int) (10 * t) + (-1 * ((int) (20 * t * t)));                 }                 if (t > .5) {                     t = 0;                     right.cancel();                 }               }         }, 0, 10);       }     if (kc == 37) {         final java.util.timer left = new java.util.timer();         left.scheduleatfixedrate(new timertask() {              public void run() {                 t += .01;                 int x1 = x;                 if (x - 1 >= 0) {                     x = x1 + (int) (-10 * t) - (-1 * ((int) (20 * t * t)));                 }                 if (t > .5) {                     t = 0;                     left.cancel();                 }               }         }, 0, 10);     }     if (kc == 40) {         final java.util.timer right = new java.util.timer();         right.scheduleatfixedrate(new timertask() {              public void run() {                 t += .01;                 int y1 = y;                 if (y + 51 < 400) {                     y = y1 + (int) (10 * t) + (-1 * ((int) (20 * t * t)));                 }                  if (t > .5) {                     t = 0;                     right.cancel();                 }               }         }, 0, 10);     }     if (kc == 38) {         final java.util.timer left = new java.util.timer();         left.scheduleatfixedrate(new timertask() {              public void run() {                 t += .01;                 int y1 = y;                 if (y-1 >= 0)                 {                 y = y1 + (int) (-10 * t) - (-1 * ((int) (20 * t * t)));                 }                 if (t > .5) {                     t = 0;                     left.cancel();                 }               }         }, 0, 10);     }     if (kc == 32) {     }     if (kc == 67) {     }     if (kc == 77) {     } }  public static void main(string[] args) {     new puzzle().frame.setvisible(true); } } 

why not adding both instances of move class. adds last 1 called. class paint below should change in order paint both instances of move

 package puzzle; import java.awt.*; import java.awt.geom.*; import javax.swing.*;  public class move extends jpanel{  int x;  int y;  color kk;   public move(int x1, int y1, color k) {     x=x1;     y=y1;     kk = k;   } public void paintcomponent(graphics g) {graphics2d g2 = (graphics2d)g;         super.paintcomponent(g2);       g2.setcolor(kk);      g2.fill(new roundrectangle2d.float(x, y, 50, 50,10,10));      g2.drawstring(x+", "+y, 200, 200);  }   } 

the class move paints rectangle position , color passed puzzle class

are taking layout managers consideration? know jframe's contentpane using borderlayout default layout manager, , if add components without constants, added borderlayout.center, , 1 of these can visible @ time (the last added).

i think might better disassociating move jpanel, yes making paintable, making more of logical object rather gui component , instead of having extend jpanel, allow single drawing jpanel hold 1 or more move objects , draw move objects in jpanel's paintcomponent method.

also, seem using several java.util.timers in code, since swing application, better served using javax.swing.timers instead, in fact perhaps 1 swing timer functioned game loop.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -