Я пытался воссоздать гексагональную сетку катана, и я новичок в апплетах и качелях. Мне не понятно понятие краски (). Я обнаружил, что графический метод drawPolygon должен использоваться для рисования шестиугольника, и я реализовал структуру, подобную сетке, и значения из массива заполнены и обновлены, поэтому я удалил их после того, как они нарисованы с помощью метода drawString, в результате чего я получил индекс за пределами границ и Я думал, что это является причиной некоторых других проблем. Но в конце концов я заметил, что сетка снова и снова освежается, и я не понимаю, как это сделать. Так как метод рисования называется, моя шестиугольная сетка отображается непрерывно, и, пожалуйста, проверьте и помогите мне, как эффективно исправить эту проблему. У меня есть метод, чтобы получить значения от arraylist и заполнить его в сетке шестиугольника.
package catan.board.game.utilities;
public class Utilities {
public static int[] getInitialXPoints(int increment){
final int[] xPoints = { 0 + increment, 45 + increment, 90 + increment, 90 + increment, 45 + increment,
0 + increment };
return xPoints;
}
public static int[] getInitialYPoints(int increment){
final int[] yPoints = { 45 + increment, 0 + increment, 45 + increment, 90 + increment, 135 + increment,
90 + increment };
return yPoints;
}
public static int[] getMoreXPoints(int[] xPoints, int increment) {
//System.out.println("The x points before the increment[" + increment + "]-->" + Arrays.toString(xPoints));
int[] newXPoints = new int[xPoints.length];
for (int i = 0; i < xPoints.length; i++)
newXPoints[i] = xPoints[i] + increment;
//System.out.println("The x points after the increment[" + increment + "]-->" + Arrays.toString(newXPoints));
return newXPoints;
}
public static int[] getMoreYPoints(int[] yPoints, int increment) {
//System.out.println("The y points before the increment[" + increment + "]-->" + Arrays.toString(yPoints));
int[] newYPoints = new int[yPoints.length];
for (int i = 0; i < yPoints.length; i++)
newYPoints[i] = yPoints[i] + increment;
//System.out.println("The y points after the increment[" + increment + "]-->" + Arrays.toString(newYPoints));
return newYPoints;
}
}
package catan.board.game;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import catan.board.game.utilities.Utilities;
public class CatanBoardApplet extends Applet {
private static final long serialVersionUID = 1L;
static final int HEX_CONSTANT = 840;
int xOval = 720;
int yOval = 60;
int widthOval = 600;
int heightOval = 600;
int nPoints = 6;
int angle = 45;
int gridSpace = 20;
int row = 3;
int maxLength = 5;
int[] xPoints = Utilities.getInitialXPoints(0);
int[] yPoints = Utilities.getInitialYPoints(0);
static ConcurrentHashMap<Integer, Integer> expectedHexValuesMap = new ConcurrentHashMap<Integer, Integer>();
static ConcurrentHashMap<Integer, Integer> actualHexValuesMap = new ConcurrentHashMap<Integer, Integer>();
static ArrayList<Integer> hexValueList = new ArrayList<Integer>();
@Override
public void init() {
System.out.println("Init");
setHexNumbers();
Graphics g = getGraphics();
System.out.println("graphics--->" + g);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawString("Welcome to the Dripura's catan game", 200, 20);
g.draw3DRect(30, 30, 1300, 650, true);
g.drawOval(xOval, yOval, widthOval, heightOval);
g.setColor(new Color(42, 179, 231));
g.fillOval(xOval, yOval, widthOval, heightOval);
drawHexagonalGrid();
rollDice();
}
private void rollDice() {
System.out.println("rolldice");
}
private static void setHexNumbers() {
expectedHexValuesMap.put(2, 1);
expectedHexValuesMap.put(12, 1);
expectedHexValuesMap.put(3, 2);
expectedHexValuesMap.put(4, 2);
expectedHexValuesMap.put(5, 2);
expectedHexValuesMap.put(6, 2);
expectedHexValuesMap.put(7, 0);
expectedHexValuesMap.put(8, 2);
expectedHexValuesMap.put(9, 2);
expectedHexValuesMap.put(10, 2);
expectedHexValuesMap.put(11, 2);
actualHexValuesMap.put(2, 0);
actualHexValuesMap.put(12, 0);
actualHexValuesMap.put(3, 0);
actualHexValuesMap.put(4, 0);
actualHexValuesMap.put(5, 0);
actualHexValuesMap.put(6, 0);
actualHexValuesMap.put(7, 0);
actualHexValuesMap.put(8, 0);
actualHexValuesMap.put(9, 0);
actualHexValuesMap.put(10, 0);
actualHexValuesMap.put(11, 0);
Random r = new Random();
int min = 2;
int max = 12;
while (!expectedHexValuesMap.equals(actualHexValuesMap)) {
int hexValue = r.nextInt((max - min) + 1) + min;
System.out.println("value--->" + hexValue);
if (hexValue != 0 && actualHexValuesMap.get(hexValue) < expectedHexValuesMap.get(hexValue)) {
hexValueList.add(hexValue);
actualHexValuesMap.put(hexValue, actualHexValuesMap.get(hexValue) + 1);
}
System.out.println("actualHexValuesMap: " + actualHexValuesMap);
}
System.out.println("hexValueList: " + hexValueList);
System.out.println("hexValueList size: " + hexValueList.size());
}
private void placeNumbers(Graphics g, Polygon p, boolean flag) {
int x = p.getBounds().x + 30;
int y = p.getBounds().y + 50;
int width = p.getBounds().width / 3;
int height = p.getBounds().width / 3;
g.drawOval(x, y, width, height);
g.setColor(Color.WHITE);
// g.setColor(new Color((int)(Math.random() * 0x1000000)));
g.fillOval(x, y, width, height);
g.setColor(Color.BLACK);
System.out.println(hexValueList);
if (flag) {
int hexValue = hexValueList.get(0);
g.drawString("" + hexValue, x + 10, y + 20);
hexValueList.remove(0);
flag = false;
}
}
private Graphics drawHexagonalGrid() {
System.out.println("Drawing the hexagonal grid");
Graphics g = getGraphics();
System.out.println("graphics---->" + g);
Boolean isValidHex = true;
// row 1 - three grids
Polygon p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 45),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 20), nPoints);
//
g.drawPolygon(p);
g.setColor(Color.GREEN);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 135),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 20), nPoints);
g.drawPolygon(p);
g.setColor(Color.BLUE);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 225),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 20), nPoints);
g.drawPolygon(p);
g.setColor(Color.RED);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
// row 2 - four grids
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 0),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 110), nPoints);
g.drawPolygon(p);
// System.out.println(Arrays.toString(p.xpoints) + " " +
// Arrays.toString(p.ypoints));
g.setColor(Color.GRAY);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 90),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 110), nPoints);
g.drawPolygon(p);
// System.out.println(Arrays.toString(p.xpoints) + " " +
// Arrays.toString(p.ypoints));
g.setColor(Color.ORANGE);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 180),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 110), nPoints);
g.drawPolygon(p);
// System.out.println(Arrays.toString(p.xpoints) + " " +
// Arrays.toString(p.ypoints));
g.setColor(Color.YELLOW);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 270),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 110), nPoints);
g.drawPolygon(p);
// System.out.println(Arrays.toString(p.xpoints) + " " +
// Arrays.toString(p.ypoints));
g.setColor(Color.YELLOW);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
// row 3 - five grids
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT - 45),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 200), nPoints);
g.drawPolygon(p);
g.setColor(Color.PINK);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 45),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 200), nPoints);
g.drawPolygon(p);
g.setColor(Color.GREEN);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 135),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 200), nPoints);
g.drawPolygon(p);
g.setColor(Color.BLACK);
g.fillPolygon(p);
placeNumbers(g, p, !isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 225),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 200), nPoints);
g.drawPolygon(p);
g.setColor(Color.GRAY);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 315),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 200), nPoints);
g.drawPolygon(p);
g.setColor(Color.RED);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
// row 4 - four grids
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 0),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 290), nPoints);
g.drawPolygon(p);
g.setColor(Color.GRAY);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 90),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 290), nPoints);
g.drawPolygon(p);
g.setColor(Color.RED);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 180),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 290), nPoints);
g.drawPolygon(p);
g.setColor(Color.PINK);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 270),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 290), nPoints);
g.drawPolygon(p);
g.setColor(Color.GREEN);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
// row 5 - three grids
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 45),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 380), nPoints);
g.drawPolygon(p);
g.setColor(Color.YELLOW);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 135),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 380), nPoints);
g.drawPolygon(p);
g.setColor(Color.ORANGE);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
p = new Polygon(Utilities.getMoreXPoints(xPoints, HEX_CONSTANT + 225),
Utilities.getMoreYPoints(yPoints, (HEX_CONSTANT / 9) + 380), nPoints);
g.drawPolygon(p);
g.setColor(Color.GRAY);
g.fillPolygon(p);
placeNumbers(g, p, isValidHex);
// repaint();
return g;
}
}