0

My code:

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

So put simply, I get really odd number from the debugging output, but the weirdest thing is, it appears to be going over each "row" twice, I have no idea why.

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>

What is output as Poly is supposed to be an index for each polygon starting at 0 and incrementing by 1 each time, but clearly, it isn't. Anything I can do to make the loops run more accurately and my number come up to what they're supposed to would be great.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
user2649681
  • 750
  • 1
  • 6
  • 23
  • 1
    I'm sure you don't write code left justified, so don't expect us to read left justified code. Edit your question and fix the formatting of the code. You should not be overriding the paint() method of a frame to do custom painting. Read the Swing tutorial on [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for the proper approach. – camickr Oct 03 '13 at 01:13
  • Cache the results of generating the `Polygon` and simple redraw them each time `paintComponent` is called... – MadProgrammer Oct 03 '13 at 01:17
  • 1) add spaces in your debugging output so we can tell which digits belong to which variable 2) please comment/ use more verbose variable names so it is easier to understand what you are trying to do. – vandale Oct 03 '13 at 02:19

0 Answers0