2016-05-04 33 views
1

これはおそらく非常に単純な問題だと私は理解していますが、私はJPanelとJFrameを1週間だけ使っていますので、私の問題は、私はこのコードを持っていることをされていますJPanelはループ終了までアップデートまたは再描画しません

package Game_Elements; 
import java.awt.Color; 
import java.awt.Desktop.Action; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.RenderingHints.Key; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.AbstractAction; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Game implements KeyListener 
{ 
private JFrame frame = null; 
private JPanel panel = null; 
int y = 0; 
int x = 0; 
public boolean W_TRUE; 
public boolean S_TRUE; 
public boolean A_TRUE; 
public boolean D_TRUE; 
public void keyPressed(KeyEvent e) 
{ 

    if(e.getKeyCode()== KeyEvent.VK_W){ 
      y-= 15; 
      panel.setLocation(x, y); 
      W_TRUE = true; 

    } 
    if(e.getKeyCode()== KeyEvent.VK_S){ 
      y+= 15; 
      panel.setLocation(x, y); 
      S_TRUE = true; 
    } 
    if(e.getKeyCode()== KeyEvent.VK_A){ 
      x-= 15; 
      panel.setLocation(x, y); 
      A_TRUE = true; 
    } 
    if(e.getKeyCode()== KeyEvent.VK_D){ 
      x+= 15; 
      panel.setLocation(x, y); 
      D_TRUE = true; 
    } 
    if(D_TRUE == true && S_TRUE == true){ 
     y+= 15; 
     x+= 15; 
     panel.setLocation(x, y); 
    } 
    if(A_TRUE == true && W_TRUE == true){ 
     y-= 15; 
     x-= 15; 
     panel.setLocation(x, y); 
    } 
    if(A_TRUE == true && S_TRUE == true){ 
     y+= 15; 
     x-= 15; 
     panel.setLocation(x, y); 
    } 
    if(D_TRUE == true && W_TRUE == true){ 
     y-= 15; 
     x+= 15; 
     panel.setLocation(x, y); 
    } 
} 
public void keyReleased(KeyEvent e) 
{ 
    if(e.getKeyCode()== KeyEvent.VK_W){ 
     W_TRUE = false; 
} 
if(e.getKeyCode()== KeyEvent.VK_S){ 
     S_TRUE = false; 
} 
if(e.getKeyCode()== KeyEvent.VK_A){ 
     A_TRUE = false; 
} 
if(e.getKeyCode()== KeyEvent.VK_D){ 
     D_TRUE = false; 
} 
if(e.getKeyCode()== KeyEvent.VK_SPACE){ 
    System.out.println("Running JUMP"); 
    for(int j = y - 200; j < y; y-= 15){ 
     try { 
      Thread.sleep(10); 
     } catch (InterruptedException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     panel.setLocation(x, y); 
     panel.revalidate(); 
    } 
    System.out.println("Complete"); 

} 
} 

public void keyTyped(KeyEvent e) { 

} 

public void frameStart() 
{ 

    frame = new JFrame(); 
    frame.setVisible(true); 
    frame.setTitle("Desperation"); 
    frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); // otherwise nice exceptions java.awt.AWTError: 
    frame.setPreferredSize(new Dimension(1920, 1050)); 
    frame.setSize(new Dimension(1920, 1050)); 
    frame.setResizable(false); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    System.out.println("Frame Created"); 

    panel = new JPanel(); 
    panel.setBackground(Color.BLUE); 
    panel.setMaximumSize(new Dimension(200, 200)); 
    panel.setLocation(1080, 500); 
    panel.revalidate(); 
    frame.add(panel); 
    System.out.println("Panel Created"); 

    frame.addKeyListener(this); 
    System.out.println("Listener Created"); 
} 

public static void main(String[] args){ 

    Game obj = new Game(); 

    obj.frameStart(); 
} 

}

私が持っているパネル、基本的な200×200ブルーキューブを、ループの実行が完了するまで場所を変更していないようですので、より多くのです何よりも飛び降りる。私は再描画機能ができるだけ早く実行されることを理解しています。これは問題である可能性が高いので、あなたの良い人の誰かがそれをリフレッシュする別の方法についてアドバイスをしていますか?いくつかの進歩を少なくとも示す。私はJPanel、JFrame、または実際にJAnythingについてはあまり知らないので、アドバイスをいただければ幸いです。ありがとうございました!

this.revalidate(); 

をリフレッシュするための

+0

スタート(https://docs.oracle.com/javase/tutorial/uiswing/ "リフレッシュ" するための良い方法です。同時実行性/)、問題解決のための[Swing Timersの使い方](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)を参照してください。キーバインディングを使用する](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)将来、別の問題になる可能性があるものを解決する – MadProgrammer

答えて

0

は、[スイングにおける同時実行]を使用してのJComponent

+0

... 'Thread.sleep'をEDT? – MadProgrammer

+0

また、 'revalidate'は、OPがレイアウトを使用していると仮定して、レイアウトに影響を与えますが、その事実を疑問視する' setLocation'があるので、 – MadProgrammer

+0

@MadProgrammer .... Hmmm ....もっとコードを表示する必要がありますあなたのポイントを見てください。しかし、別の解決方法を示唆するコードが増えるまで、私は投稿を削除しません。別の問題として、指定されたLong時間連続して実行されているthread.sleepがあります。 – DarkV1

関連する問題