【发布时间】:2015-05-13 14:12:25
【问题描述】:
我在this tutorial 中使用了六边形代码并创建了一个 createHex 类(我应该发布代码吗?)。链接的网页使用以下代码使用 createHex 中的数学实际绘制六边形:
@Override
public void paint(Graphics g){
for(int j = 0; int j < BOARD_HEIGHT; j++){
for(int i = 0; i < BOARD_HEIGHT; I++){
mCellMetrics.setCellIndex(i, j);
if(mCells[j][i] != 0){
mCellMetrics.computeCorners(mCornersX, mCornersY);
g.setColor((mCells[j][i] == L_ON) ? COLOR.ORANGE):COLOR.GRAY;
g.fillPolygon(mCornersX, mCornersY, NUM_HEX_CORNERS);
g.setColor(COLOR.BLACK)
g.drawPolygon(mCornersX, mCornersY, NUM_HEX_CORNERS);
}
}
}
}
我遇到的问题是Android没有包含所有必需方法的Graphics类。我围绕 android 文档进行了大约一个半小时的钓鱼,我发现最接近的是 Path 类,但它没有我需要的方法。我想使用顶部链接文章中的六边形代码,但我找不到图形类的等价物。如果没有等效项,有人可以告诉我如何使用链接代码获得我想要的结果吗?
我的问题:如何将链接文章中的代码移植到 android?
编辑:
我认为,如果我包含计算六边形的边和诸如此类的六边形代码,它可能对其他人(和潜在的回答者)有所帮助,所以这里是 createHex.java:
package com.rush;
/**
* Uniform hexagonal grid cell's metrics utility class.
*/
public class HexGridCell {
private static final int[] NEIGHBORS_DI = { 0, 1, 1, 0, -1, -1 };
private static final int[][] NEIGHBORS_DJ = {
{ -1, -1, 0, 1, 0, -1 }, { -1, 0, 1, 1, 1, 0 } };
private final int[] CORNERS_DX; // array of horizontal offsets of the cell's corners
private final int[] CORNERS_DY; // array of
vertical offsets of the cell's corners
private final int SIDE;
private int mX = 0; // cell's left coordinate
private int mY = 0; // cell's top coordinate
private int mI = 0; // cell's horizontal grid coordinate
private int mJ = 0; // cell's vertical grid coordinate
/**
* Cell radius (distance from center to one of the corners)
*/
public final int RADIUS;
/**
* Cell height
*/
public final int HEIGHT;
/**
* Cell width
*/
public final int WIDTH;
public static final int NUM_NEIGHBORS = 6;
/**
* @param radius Cell radius (distance from the center to one of the corners)
*/
public HexGridCell(int radius) {
RADIUS = radius;
WIDTH = radius * 2;
HEIGHT = (int) (((float) radius) * Math.sqrt(3));
SIDE = radius * 3 / 2;
int cdx[] = { RADIUS / 2, SIDE, WIDTH, SIDE, RADIUS / 2, 0 };
CORNERS_DX = cdx;
int cdy[] = { 0, 0, HEIGHT / 2, HEIGHT, HEIGHT, HEIGHT / 2 };
CORNERS_DY = cdy;
}
/**
* @return X coordinate of the cell's top left corner.
*/
public int getLeft() {
return mX;
}
/**
* @return Y coordinate of the cell's top left corner.
*/
public int getTop() {
return mY;
}
/**
* @return X coordinate of the cell's center
*/
public int getCenterX() {
return mX + RADIUS;
}
/**
* @return Y coordinate of the cell's center
*/
public int getCenterY() {
return mY + HEIGHT / 2;
}
/**
* @return Horizontal grid coordinate for the cell.
*/
public int getIndexI() {
return mI;
}
/**
* @return Vertical grid coordinate for the cell.
*/
public int getIndexJ() {
return mJ;
}
/**
* @return Horizontal grid coordinate for the given neighbor.
*/
public int getNeighborI(int neighborIdx) {
return mI + NEIGHBORS_DI[neighborIdx];
}
/**
* @return Vertical grid coordinate for the given neighbor.
*/
public int getNeighborJ(int neighborIdx) {
return mJ + NEIGHBORS_DJ[mI % 2][neighborIdx];
}
/**
* Computes X and Y coordinates for all of the cell's 6 corners, clockwise,
* starting from the top left.
*
* @param cornersX Array to fill in with X coordinates of the cell's corners
* @param cornersX Array to fill in with Y coordinates of the cell's corners
*/
public void computeCorners(int[] cornersX, int[] cornersY) {
for (int k = 0; k < NUM_NEIGHBORS; k++) {
cornersX[k] = mX + CORNERS_DX[k];
cornersY[k] = mY + CORNERS_DY[k];
}
}
/**
* Sets the cell's horizontal and vertical grid coordinates.
*/
public void setCellIndex(int i, int j) {
mI = i;
mJ = j;
mX = i * SIDE;
mY = HEIGHT * (2 * j + (i % 2)) / 2;
}
/**
* Sets the cell as corresponding to some point inside it (can be used for
* e.g. mouse picking).
*/
public void setCellByPoint(int x, int y) {
int ci = (int)Math.floor((float)x/(float)SIDE);
int cx = x - SIDE*ci;
int ty = y - (ci % 2) * HEIGHT / 2;
int cj = (int)Math.floor((float)ty/(float)HEIGHT);
int cy = ty - HEIGHT*cj;
if (cx > Math.abs(RADIUS / 2 - RADIUS * cy / HEIGHT)) {
setCellIndex(ci, cj);
} else {
setCellIndex(ci - 1, cj + (ci % 2) - ((cy < HEIGHT / 2) ? 1 :
0));
}
}
}
有关代码如何工作的说明,请参阅链接文章。
【问题讨论】:
-
找到这个问题。可能足以让您开始:How to draw a line in android.
-
@ControlAltDel 我不相信 java.awt.Polygon 类包含在 android....
-
这是一个游戏。我正在为 android 设计我的第一个游戏引擎,我想使用 hexes 来确定玩家每回合可以移动多远。
-
@DJMethaneMan 我认为你对 android 没有 Polygon 对象是正确的。另外,我认为我链接到的 PolygonFactory 中的算法/解决方案与您已经找到的基本相同。但我认为您应该能够使用 Path 和 lineTo 调整这些解决方案中的任何一个以在 Android 中工作