2015-09-18 81 views
-2

我目前JFrame中有一個JPanel,我需要視圖跟隨一個對象,我不能移動世界的原因是因爲對象正在軌道另一個對象,所以工作如何移動世界將會更加困難。爪哇查看一個移動的對象在鞦韆

+1

[如何滾動攝像機添加到2D Java小遊戲?(http://gamedev.stackexchange.com/a/44270) –

+0

做我雖然用什麼?我試過JScrollPane和JViewport,但我不太瞭解它們。 –

+0

您不需要JViewPort,此解決方案僅適用於JPanel和其上的圖形。 –

回答

1

好吧,下面是如何做到這一點的例子。請注意,SSCCE不需要是面向對象的,因此您可能不應複製並粘貼到當前項目中。我不會解釋這些步驟,因爲幾乎所有內容都在我發佈的鏈接中進行了解釋。不過,這裏是參考文獻for the 2D camerafor the object moving in circular path

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.AffineTransform; 
import javax.swing.Timer; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Example { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Example(); 
      } 
     }); 
    } 

    public Example() { 

     JFrame frame = new JFrame(); 

     frame.setContentPane(new DrawingPanel()); 

     frame.setSize(400, 300); 
     frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     frame.setVisible(true); 

    } 

    class DrawingPanel extends JPanel { 

     int angle = 0; 
     int rotation = 0; 
     int radius = 150; 

     int ovalX = 150; 
     int ovalY = 150; 
     int ovalWidth = 100; 
     int ovalHeight = 100; 
     int ovalCenterX = ovalX + ovalWidth/2; 
     int ovalCenterY = ovalY + ovalHeight/2; 

     int recX; 
     int recY; 
     int recWidth = 50; 
     int recHeight = 50; 
     int recCenterX; 
     int recCenterY; 

     int WORLD_SIZE_X = 6000; 
     int WORLD_SIZE_Y = 6000; 

     int camX; 
     int camY; 

     public DrawingPanel() { 

      ActionListener al = new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        angle++; 
        rotation++; 
        recCenterX = (int) ((int) ovalCenterX + Math.sin(Math.toRadians(angle)) * radius); 
        recCenterY = (int) ((int) ovalCenterY + Math.cos(Math.toRadians(angle)) * radius); 
        recX = recCenterX - recWidth/2; 
        recY = recCenterY - recHeight/2; 
        repaint(); 
       } 
      }; 

      Timer timer = new Timer(15, al); 
      timer.start(); 

     } 

     @Override 
     public void paintComponent(Graphics g) { 

      super.paintComponent(g); 
      Graphics2D gg = (Graphics2D) g; 
      gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

      int VIEWPORT_SIZE_X = getWidth(); 
      int VIEWPORT_SIZE_Y = getHeight(); 

      int offsetMaxX = WORLD_SIZE_X - VIEWPORT_SIZE_X; 
      int offsetMaxY = WORLD_SIZE_Y - VIEWPORT_SIZE_Y; 

      camX = recCenterX - VIEWPORT_SIZE_X/2; 
      camY = recCenterY - VIEWPORT_SIZE_Y/2; 

      if (camX > offsetMaxX) { 
       camX = offsetMaxX; 
      } 

      if (camY > offsetMaxY) { 
       camY = offsetMaxY; 
      } 

      gg.translate(-camX, -camY); 

      gg.setColor(Color.BLUE); 
      gg.fillOval(ovalX, ovalY, ovalWidth, ovalHeight); 

      AffineTransform old = gg.getTransform(); 

      gg.rotate(Math.toRadians(rotation), recCenterX, recCenterY); 
      gg.setColor(Color.RED); 
      gg.drawRect(recX, recY, recWidth, recHeight); 

      gg.setTransform(old); 

      gg.setColor(Color.GREEN); 
      gg.drawLine(ovalCenterX, ovalCenterY, recCenterX, recCenterY); 

     } 

    } 

} 

enter image description here

+0

我把你的代碼複製到eclipse中,但是視圖保持居中在屏幕中間而不是繞着藍色圓圈。我的參考資料是用來解決問題的嗎?如果是這樣,謝謝,生病了吧。 –

+0

@TomHaffenden嗯,紅色的正方形是旋轉的,而不是藍色的圓圈 - 攝像機跟着紅色方塊。 –

+0

感謝您的幫助!這真的幫助我推進我的項目。 –