2011-10-11 46 views
2
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); 
} 
} 

爲什麼不添加移動類的兩個實例。它只添加最後一個叫。該類塗料低於我應該爲了改變它畫爲什麼只畫一個組件?

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); 

} 


} 

類的舉動描繪從益智類通過位置和顏色的矩形移動的兩個實例

回答

5

你服用佈局管理器考慮到?你知道JFrame的contentPane使用BorderLayout作爲它的默認佈局管理器,如果你添加組件到這個沒有常量的地方,它們將被添加BorderLayout.CENTER,並且只有其中一個可以在一次(最後一次添加)時可見。

我認爲你可能會更好地解除JPanel的移動,讓它變成可繪製的,但是使它更多的是一個邏輯對象而不是gui組件,而不是讓它擴展JPanel,允許單個繪圖JPanel持有一個或更多Move對象,然後在此JPanel的paintComponent方法中繪製Move對象。另外,你似乎在你的代碼中使用了幾個java.util.Timers,但因爲它是一個Swing應用程序,所以最好使用javax.swing.Timers代替它,事實上也許只有一個Swing作爲遊戲循環的定時器。

+0

所以如何將我去有關添加圖形componenents,以便能夠在腿上? –

+1

再次,我不會移動擴展JPanel。您可以讓它實現Shape接口,併爲單個JPanel提供一個MoveList對象的ArrayList,然後讓JPanel在其paintComponent方法中遍歷此列表,在其所持有的每個Move對象上調用繪畫或繪圖,並傳入其Graphics對象。 –

+0

@Hovercraft完整的鰻魚+++鞦韆金徽章+++ – mKorbel

3
if (kC == 39) { 

請勿使用幻數。

相反,你應該使用:

KeyEvent.VK_??? 
相關問題