Java:从数组中绘制图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16277156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Drawing graphics from an array
提问by P?tur Magnussen
I'm working on my first java game for a school project, and I'm having some problems drawing the graphics based on information in an array.
我正在为学校项目开发我的第一个 Java 游戏,但在根据数组中的信息绘制图形时遇到了一些问题。
What I'm basically trying to do is to create a 2D array (matrix) which will store all the information about the world in which the player can move. So some elements in the array will contain a wall, others open space for the player to move in, and so on...
我基本上要做的是创建一个二维数组(矩阵),它将存储有关玩家可以在其中移动的世界的所有信息。所以数组中的一些元素将包含一堵墙,其他元素为玩家提供移动空间,等等......
I have this sample code which I'm working from:
我有我正在使用的示例代码:
/**
*
* @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;
}
}
}
And,
和,
/**
* @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);
}
}
Now, I made this for loop to populate a test Array:
现在,我做了这个 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);
Do you know how I could draw this array? I apologize for the length of the code...
你知道我怎么画这个数组吗?我为代码的长度道歉......
回答by NeplatnyUdaj
Just paint something based on the array in your GameBoard's paint:
只需根据您的 GameBoard 油漆中的数组绘制一些内容:
@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);
}
But you should first go through some tutorial on Painting in Java: http://www.oracle.com/technetwork/java/painting-140037.html
但是你应该先阅读一些关于用 Java 绘画的教程:http: //www.oracle.com/technetwork/java/painting-140037.html
回答by Joop Eggen
It is better to have the walls as
最好有墙壁作为
List<Rectangle> walls = new ArrayList<>();
That is more optimal, as you can simply do:
这是更理想的,因为您可以简单地执行以下操作:
for (Rectangle rect: walls) {
g.fillRect(rect.x, rect.y, rect.width, rect.height);
}
Wall detection goes alike.
墙壁检测也是如此。