paint 方法被一次又一次地调用,这个过程在 Applet 中没有停止,十六进制被重新绘制,我失去了所有的数字

The paint method is called again and again and the process is not stopping in Applet and the hex are repainted and i lose all the numbers

我正在尝试重新创建卡坦六角网格,但我是 applet 和 swing 的新手。我不清楚 paint() 的概念。我发现图形方法 drawPolygon 必须用于绘制六边形并且我实现了一个类似网格的结构并且数组列表中的值被填充在那里并刷新所以我在使用 drawString 方法绘制它们后删除它们导致我的索引超出范围并且我认为是其他一些问题的原因。但最后我注意到网格一次又一次地刷新我一无所知,因为我不明白这个概念。由于绘制方法被称为我的六边形网格连续渲染,请检查并帮助我如何有效地纠正这个问题。我有方法从数组列表中获取值并将其填充到六边形网格中。

    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;
        }

    }

绘画时不得修改 hexValueList 列表。

程序不会决定何时调用组件的 paint 方法。系统在控制它。

您的 paint 方法可能出于许多不同的原因被调用,所有这些都超出您的控制范围。一些可能的原因是:

  • 用户移动 window
  • 用户图标化或取消图标化 window
  • 用户在你的 window
  • 前面移动了另一个 window
  • 用户将您的 window 置于最前面
  • 用户将鼠标移到您的组件上
  • 用户锁定或黑屏
  • 用户滚动包含小程序的网页
  • 用户重新加载包含小程序的网页

绘画每秒可能发生多次。或者,它可能只是偶尔出现。

由于您无法控制调用绘画方法的时间,因此您只能在其中包含绘画逻辑。如果您从 List 中删除值,则这些值在后续调用 paint 方法期间将不可用。

这就是为什么您不能在 placeNumbers 方法中修改 hexValueList 列表的原因,因为该方法由 drawHexagonalGrid 调用,而 drawHexagonalGrid 又由 paint 调用。

不是从 hexValueList 中删除数字,而是使用 List 的 iterator() 方法对其进行迭代,并将每个迭代的 int 值作为参数传递给 placeNumbers:

private void placeNumbers(Graphics g, Polygon p, boolean flag, int hexValue) {
    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);
    if (flag) {
        g.drawString("" + hexValue, x + 10, y + 20);
    }
}

private Graphics drawHexagonalGrid() {
    Iterator<Integer> hexValueIterator = hexValueList.iterator();

    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, hexValueIterator.next());
    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, hexValueIterator.next());
    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, hexValueIterator.next());
    // 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, hexValueIterator.next());
    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, hexValueIterator.next());
    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, hexValueIterator.next());
    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, hexValueIterator.next());
    // 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, hexValueIterator.next());
    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, hexValueIterator.next());
    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, 0);
    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, hexValueIterator.next());
    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, hexValueIterator.next());
    // 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, hexValueIterator.next());
    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, hexValueIterator.next());
    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, hexValueIterator.next());
    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, hexValueIterator.next());
    // 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, hexValueIterator.next());
    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, hexValueIterator.next());
    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, hexValueIterator.next());
    // repaint();
    return g;
}

现在绘画逻辑只读取hexValueList,没有修改,所以paint方法调用多少次都没有关系

我还删除了 flag = false;,因为它没有任何用处。 flag 是方法参数,因此在方法内部更改其值在方法外部没有影响。