2013-04-29 96 views
1

我正在爲我的第一個學校項目的java遊戲工作,並且在基於數組中的信息繪製圖形時遇到了一些問題。Java:從數組中繪製圖形

我基本上試圖做的是創建一個2D數組(矩陣),它將存儲有關玩家可以移動的世界的所有信息。所以數組中的一些元素將包含牆,其他開放空間供玩家在移動,等等...

我有我的工作,從這個示例代碼:

/** 
* 
* @author Rasztemberg 
*/ 
package simpleGame; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.JPanel; 


/** 
* GameBoard is a part of JPanel. 
* It has a Graphical Object {@link Graphics} 
* that can be used to render shapes etc. Pass it's reference to any object you 
* want to display in the gameBoard panel. 
*/ 


public class GameBoard extends JPanel implements KeyListener{ 



Player player; 
Player enemy; 

public GameBoard(){ 

    // SETUP PLAYER ON THE BOARD; 
    int xPos = 0; 
    int yPos = 0; 
    int width = 20; 
    int height = 20; 
    Color playerC = Color.BLUE; 

    player = new Player(xPos, yPos, width, height, playerC); 

    // SETUP ENEMY ON THE BOARD; 
    enemy = new Player(100, 100, width, height, Color.RED); 

    addKeyListener(this); 
} 


/* 
* 
* JPanel function to display all gameBoard object graphics. 
*/ 

@Override 
public void paint(Graphics g){ 
    super.paint(g); // Call it's parent for proper rendering. 

    player.display(g); 
    enemy.display(g); 
} 


@Override 
public void keyTyped(KeyEvent e) {} 

@Override 
public void keyPressed(KeyEvent e) { 

    int keyCode = e.getKeyCode(); 
    if (keyCode == KeyEvent.VK_LEFT) { 
     player.moveLeft(); 
    } 

    if (keyCode == KeyEvent.VK_RIGHT) { 
     player.moveRight(); 
    } 

    if (keyCode == KeyEvent.VK_DOWN) { 
     player.moveDown(); 
    } 

    if (keyCode == KeyEvent.VK_UP) { 
     player.moveUp(); 
    } 
} 

@Override 
public void keyReleased(KeyEvent e) {} 


/** 
* Set's focus on the panel so key events are catch. 
*/ 

public boolean isFocusTraversable() { 
    return true; 
} 

}

而且,

/** 
* @author Rasztemberg 
*/ 
package simpleGame; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.event.MouseAdapter; 
import javax.swing.JPanel; 


/** 
* This is main player Class. 
* 
*/ 


public class World { 

// Snake parameters 

private int x; 
private int y; 
private int width; 
private int height; 
private Color color; 

/** 
* Class constructor. Called when instantiated. 
* Assigns x and y coordinates to position the player. 
* Sets width, height and color to the rendered object. 
* 
*/ 


public World(int x, int y, int w, int h, Color c){ 

    this.x = x; 
    this.y = y; 
    this.width = w; 
    this.height = h; 
    this.color = c; 
} 

/** 
* Accepts Graphics object to render 
* player 1 shape 
*/ 

public void display(Graphics g) { 

    // This is player rendered graphics. 
    Graphics2D walls = (Graphics2D) g; // Graphical library to render shapes. 
    walls.setColor(color); 
    walls.drawRect(x, y, width, height); 
    walls.fillRect(x, y, width, height); 
} 
} 

現在,我做了這個for循環填充測試陣列:

int[][] wallArray = new int[800][600]; 

     for (int x = 0; x < wallArray.length; x++) { 
      for (int y = 0; y < wallArray.length; y++) { 
       wallArray[x][y] = 1; 
      } 
     } 

     wallArray[100][100] = 0; 
     greatWall = new World(wallArray); 

你知道我該如何繪製這個數組?我很抱歉的代碼長度...

回答

2

根據您的遊戲鍵盤的油漆陣列上只是油漆的東西:

@Override 
public void paint(Graphics g){ 
    super.paint(g); // Call it's parent for proper rendering. 
    for (int i = 0; i<wallArray.length; i++) 
     for (int j = 0; j<wallArray[0].length; j++){ 
      //do something for every field in the array 
      //i.e. g.setColor(Color.getColor(wallArray[i][j], 50, 50)); 
      //g.drawLine(i,j,i,j); 
    } 
    player.display(g); 
    enemy.display(g); 
} 

但是,你應該通過一些教程先下去繪畫在Java中: http://www.oracle.com/technetwork/java/painting-140037.html

1

最好是有牆壁

List<Rectangle> walls = new ArrayList<>(); 

這是更優化,你可以簡單地做:

for (Rectangle rect: walls) { 
    g.fillRect(rect.x, rect.y, rect.width, rect.height); 
} 

牆壁檢測相似。

+0

但是在這種情況下,您必須每次遍歷整個列表。我認爲最有效的方法應該是爲遊戲板和集合等不可改變的東西組成的陣列。 – NeplatnyUdaj 2013-04-29 14:00:57