2016-04-25 20 views
0

我遇到了麻煩。我想在我的JFrame contentPane中添加一個矩形到我的JPanel。我希望這個x在一個pos位置,但移動-x並重新啓動,其中+ x開始。即如果我有一個800 x 400的JPanel,我想讓rext接受這些參數,但是沿着x軸(x-Velx)移動,在800處重新繪製自己,並沿着-x方向繼續。我知道這不是足夠的信息,我所觸摸的書中沒有一本是基於我正在嘗試做的,因此我缺乏適當的術語。XMotion處理JComponents

+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)或[我試圖使用線程在applet中移動球但它不移動](http://stackoverflow.com/questions/14456072/i-am-trying-to-move-a-ball-in-applet-using-thread-but-其-不移動的/ 14456808#14456808)。我想你會發現,這種行爲不被讚賞,因爲你試圖以犧牲他人爲代價來提高你的聲譽。如果這個例子真的有用,那麼這個問題應該作爲一個複製關閉 – MadProgrammer

+0

我也勸阻「盲目」代碼轉儲,因爲它教什麼都沒有,爲什麼你會在這種情況下使用Swing'Timer'?以這種方式使用'JPanel'有什麼優點或缺點? – MadProgrammer

+0

@MadProgrammer我剛剛找到一個例子,這似乎是OP需要一個改變方向的例子。我不會試圖建立那些隨着時間而來的聲望點。我想我可以剛剛發表評論,但我不能,因爲我需要50 rp才能正確評論他的例子。我從來沒有說過這是我的代碼,是來自開源形式的例子。對不起,如果你不讚賞我試圖幫助。我希望有人給我一段代碼,但如果我冒犯了你和社區,這只是我的意見而已。 – 2016-04-25 02:35:52