2016-04-25 33 views
0

問題があります。 JFrame contentPaneに追加されたJPanelにrectを描画したいと思います。私はそのxがset posにあるが、-xを移動して+ xが始まるところで再開したい。つまり800 x 400のJPanelを持っている場合、rextにこれらのパラメータを取り込み、x軸(x-Velx)に沿って移動して800で再描画し、-x方向に続けます。私はこれが十分な情報ではないことを知っています。私の本のどれも私が何をしようとしているかに基づいているので、適切な用語が不足しています。JMponentsを扱うXMotion

+0

がそれをネジ気にしないをすることの良い例です私が思いついたアイデア。私が探しているものについては、適切な用語が不足しています。 –

+0

[this](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788)や[this](http://stackoverflow.com/questions/16908418/paintcomponent-not-働いている/ 16908462#16908462)? – MadProgrammer

答えて

1

//ここでは、私はもう一度基礎から始めるとスクラッチよ、この

パブリッククラスAnimatedBoat {

public static void main(String[] args) { 
    new AnimatedBoat(); 
} 

public AnimatedBoat() { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
      } 

      JFrame frame = new JFrame("Test"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLayout(new BorderLayout()); 
      frame.add(new AnimationPane()); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
     } 

    }); 
} 

public class AnimationPane extends JPanel { 

    private BufferedImage boat; 
    private int xPos = 0; 
    private int direction = 1; 

    public AnimationPane() { 
     try { 
      boat = ImageIO.read(new File("boat.png")); 
      Timer timer = new Timer(40, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        xPos += direction; 
        // change directions off window width 
        if (xPos + boat.getWidth() > getWidth()) { 
         xPos = getWidth() - boat.getWidth(); 
         direction *= -1; 
        } else if (xPos < 0) { 
         xPos = 0; 
         direction *= -1; 
        } 
        repaint(); 
       } 

      }); 
      timer.setRepeats(true); 
      timer.setCoalesce(true); 
      timer.start(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return boat == null ? super.getPreferredSize() : new Dimension(boat.getWidth() * 4, boat.getHeight()); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     int y = getHeight() - boat.getHeight(); 
     g.drawImage(boat, xPos, y, this); 

    } 

} 

}

+0

[イメージ(アニメーション)の移動方法](http://stackoverflow.com/questions/14432816/how-to-move-an-image-animation/14436331#14436331)または[私は試行していますスレッドを使用してアプレット内のボールを移動するが、移動しない](http://stackoverflow.com/questions/14456072/i-am-trying-to-move-a-ball-in-applet-using-thread-but-その移動していない/ 14456808#14456808)。私はあなたが他の人のコストで評判を向上させようとしているので、この種の行動が評価されていないことがわかるでしょう。もしこの例が本当に役に立つのであれば、質問はduplictaeとして閉じなければなりません – MadProgrammer

+0

私は「盲目的な」コードダンプをOPに何も教えていないので、この場合はなぜSwing Timerを使うのでしょうか?このように 'JPanel'を使うことの賛否両論は何ですか? – MadProgrammer

+0

@MadProgrammer私はちょうどOPが方向を変える例を必要としていたと思われる例を見つけました。私は時間をかけて来る評判ポイントをつくろうとはしていません。私はちょうどコメントすることができたと思うが、私は例に彼をリンクするコメントを右にするためには50 rpが必要なのでできない。私はこれが私のコードであり、オープンソース形式の例であるとは言いませんでした。もしあなたが私を助けようとしてくれてありがとうございましたら申し訳ありません。誰かに見せたいコードを私に与えてもらいたいですが、あなたとコミュニティに怒られた場合、それは私の意見です。 –