2012-01-07 52 views
0

我正在製作一款遊戲(基於CityVille的遊戲),您從上面俯瞰城市。我想要這樣做,以便我可以拖動世界來查看地圖的不同部分。如何實現一個可以拖動世界的系統?

這裏是我的代碼:

package com.cgp.buildtown; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.JPanel; 

public class Game extends JPanel implements Runnable, MouseMotionListener { 
    private static final long serialVersionUID = 1L; 
    private BufferedImage banner; 
    private String chars = " `[email protected]#4$5%6^7&8*9(0)-_=+qQwWeErRtTyYuUiIoOpP[{]}\\|aAsSdDfFgGhHjJkKlL;:'\"zZxXcCvVbBnNmM,<.>/?"; 
    public String cityName; 
    private int[] offset; 
    public Thread thread; 
    private int totalOffset = 0; 
    private boolean[][] townAreas = new boolean[15][15]; 

    public Game() { 
     super(); 
     setBackground(new Color(69, 139, 0)); 
     loadImages(); 
     townAreas[8][8] = true; 
     townAreas[0][1] = true; 
    } 

    @Override 
    public void addNotify() { 
     super.addNotify(); 
     thread = new Thread(this); 
    } 

    void findOffset() { 
     offset = new int[cityName.length()]; 
     for (int i = 0; i < cityName.length(); i++) { 
      int ch = chars.indexOf(cityName.charAt(i)); 
      if (ch < 0) 
       continue; 
      if (isShort(ch)) { 
       for (int j = i + 1; j < offset.length; j++) { 
        offset[j] += 25; 
       } 
       totalOffset += 25; 
      } 
     } 
    } 

    private boolean isShort(int ch) { 
     if (ch == 0 || ch == 1 || ch == 4 || ch == 14 || ch == 20 || ch == 22 || ch == 41 || ch == 42 || ch == 47 || ch == 48 || ch == 49 || ch == 50 || ch == 51 || ch == 52 || ch == 65 || ch == 69 || ch == 71 || ch == 72 || ch == 73 || ch == 74 || ch == 89 || ch == 91 || ch == 93) 
      return true; 
     else 
      return false; 
    } 

    private void loadImages() { 
     try { 
      banner = ImageIO.read(new File("res/banner.png")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void mouseDragged(MouseEvent e) { 

    } 

    @Override 
    public void mouseMoved(MouseEvent e) { 

    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(new Color(118, 238, 0)); 

     for (int i = 0; i < 15; i++) { 
      for (int j = 0; j < 15; j++) { 
       if (townAreas[i][j]) { 
        g.fillRect(j * 400, i * 400, 400, 400); 
       } 
      } 
     } 

     int length = (cityName.length() * 45) - totalOffset; 
     int x = 400 - (length/2); 
     for (int k = 0; k < cityName.length(); k++) { 
      int ch = chars.indexOf(cityName.charAt(k)); 
      if (ch < 0) 
       continue; 
      g.drawImage(banner, (x + (k * 45)) - offset[k], 0, ((x + (k * 45)) + 45) - offset[k], 45, ch * 45, 0, (ch * 45) + 45, 45, null); 
     } 
     g.drawImage(banner, x - 15, 0, x, 45, 4276, 0, 4290, 45, null); 
     g.drawImage(banner, x + length, 0, x + length + 15, 45, 4291, 0, 4305, 45, null); 
    } 

    @Override 
    public void run() { 
     while (true) { 
      repaint(); 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

提前感謝!

+2

這不是一個具體的問題。哪一部分,特別是你有麻煩? – 2012-01-07 18:00:46

+0

將系統置於可以拖動世界的地方(即MouseMotionListeners)。我嘗試過不同的方式來做到這一點,但都沒有成功。 – Cg2916 2012-01-07 18:01:36

+0

也許嘗試代碼審查堆棧交換網站..或嘗試具體問題。 – dotjoe 2012-01-07 18:03:15

回答

0

我認爲你想要的效果的術語叫做「slippy map」。這與您在Google地圖中看到的用戶體驗相同。

在Web瀏覽器中有很多這樣的事情的例子,甚至有一些JQuery插件。在Java2D中做 - 它需要Google。

我們需要更多具體的信息來幫助人們在黑暗中進行刺探。

+0

好吧,我會開始尋找。這正是我想要的。 – Cg2916 2012-01-07 18:08:09

相關問題