0

我的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;

public class HexagonWDW extends JFrame {

    public HexagonWDW() {
        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("I am a duck!");
        add(new Hexas());
        setVisible(true);
    }

    public static void main(String[] args) {
        new HexagonWDW();
    }
}

class Hexas extends JPanel {

    Image[] img;
    int xmax;
    int ymax;
    int totaloff;
    ArrayList<Integer> xs;
    ArrayList<Integer> ys;
    ArrayList<Integer> ims;

    public Hexas() {
        totaloff = 0;
        img = new Image[3];
        xs = new ArrayList();
        ys = new ArrayList();
        ims = new ArrayList();
        xmax = 2;
        ymax = 2;
        img[0] = new ImageIcon("img.png").getImage();
        img[1] = new ImageIcon("blue.png").getImage();
        img[2] = new ImageIcon("green.png").getImage();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHints(rh);
        init(g2);
        ims.set(1, 1);
        updateIcons(g2);
    }

    public void init(Graphics2D g2) {
        int xr = 0;
        int yr = 0;
        setBackground(Color.white);
        Dimension size = getSize();
        double w = size.getWidth();
        double h = size.getHeight();
        int r = 20;
        double pi = Math.PI;
        int q = 1;
        for (int j = 0; j < ymax; j++) {
            System.out.println("Y: " + j);
            q = q == 1 ? 0 : 1;
            totaloff += q;
            int aq = q == 1 ? 0 : 1;
            int y = 50 + j * 20;
            for (int a = 0; a < (q + xmax); a++) {
                Polygon sprite = new Polygon();
                int x = 50 + a * 65 + aq * 32;
                System.out.println("Totaloff: " + totaloff + " and q: " + q);
                System.out.println("Poly: " + (int) j * xmax + totaloff + a + q + "\n");
                for (int i = 0; i < 6; i++) {
                    xr = (int) (x + r * Math.cos(i * 2 * pi / 6));
                    yr = (int) (y + r * Math.sin(i * 2 * pi / 6));
                    xs.add(j * xmax + totaloff + a + q, xr);
                    ys.add(j * xmax + totaloff + a + q, yr);
                    ims.add(j * xmax + totaloff + a + q, 0);
                    sprite.addPoint(xr, yr);
                    System.out.println("Point added at" + i + xr + yr);
                }
                g2.setColor(Color.black);
                g2.fillPolygon(sprite);
                g2.setColor(Color.blue);
                g2.drawPolygon(sprite);
                int cur = ims.get(j * ymax + totaloff + a + q);
                g2.drawImage(img[cur], xr - 15, yr + 15, null);
            }
        }
    }

    public void updateIcons(Graphics g2) {
        for (int i = 0; i < (xmax * ymax + totaloff); i++) {
            g2.drawImage(img[ims.get(1)], xs.get(1) - 15, ys.get(1) + 15, null);
        }
    }
}

简而言之,我从调试输出中得到了非常奇数的数字,但最奇怪的是,它似乎遍历了每个“行”两次,我不知道为什么。

Y: 0
Totaloff: 0 and q: 0
Poly: 0000
Point added at010250
Point added at19267
Point added at27267
Point added at36250
Point added at47132
Point added at59232
Totaloff: 0 and q: 0
Poly: 0010
Point added at016750
Point added at115767
Point added at213767
Point added at312750
Point added at413732
Point added at515732
Y: 1
Totaloff: 1 and q: 1
Poly: 2101
Point added at07070
Point added at16087
Point added at24087
Point added at33070
Point added at43952
Point added at56052
Totaloff: 1 and q: 1
Poly: 2111
Point added at013570
Point added at112587
Point added at210587
Point added at39570
Point added at410452
Point added at512552
Totaloff: 1 and q: 1
Poly: 2121
Point added at020070
Point added at119087
Point added at217087
Point added at316070
Point added at417052
Point added at519052
Y: 0
Totaloff: 1 and q: 0
Poly: 0100
Point added at010250
Point added at19267
Point added at27267
Point added at36250
Point added at47132
Point added at59232
Totaloff: 1 and q: 0
Poly: 0110
Point added at016750
Point added at115767
Point added at213767
Point added at312750
Point added at413732
Point added at515732
Y: 1
Totaloff: 2 and q: 1
Poly: 2201
Point added at07070
Point added at16087
Point added at24087
Point added at33070
Point added at43952
Point added at56052
Totaloff: 2 and q: 1
Poly: 2211
Point added at013570
Point added at112587
Point added at210587
Point added at39570
Point added at410452
Point added at512552
Totaloff: 2 and q: 1
Poly: 2221
Point added at020070
Point added at119087
Point added at217087
Point added at316070
Point added at417052
Point added at519052</code>

Poly 的输出应该是每个多边形的索引,从 0 开始,每次递增 1,但显然不是。我能做的任何事情都可以使循环运行得更准确,并且我的数字达到了他们应该很好的水平。

4

0 回答 0