Right now, I am trying to make a Hex Grid GUI that highlights whatever hex that the mouse is hovering over. However, the JPanel which is supposed to display the hexagons are not showing up.

I seriously have no idea what is happening between repaint() and paintComponents() in this case. I tried to look up solutions, which involve setting the JPanel into a visible hierachy, which I made sure I did, and it still is not working.

Code is slightly long

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;


public class HexGridTester implements ActionListener, MouseListener, MouseMotionListener
{

private JFrame frame;
private TestPanel panel;
private double radius;
private double width;
private double side;
private double height;
private Location mouseover;

private class TestPanel extends JPanel
{
    @Override
    public void paintComponents(Graphics g)
    {
        System.out.println("CALLED");
        super.paintComponents(g);
        int[] xpoints = new int[6];
        int[] ypoints = new int[6];

        for(int c = 0; c < 15; c++)
        {
            for(int r = 0; r < 21; r++)
            {
                int dx = c * (int) width;
                int dy = r * (int) height;

                if(new Location(c, r).equals(mouseover))
                {
                    g.setColor(Color.blue);
                }
                else
                {
                    g.setColor(Color.black);
                }

                if(c % 2 != 0)
                {
                    dy += height / 2;
                }

                for(int i = 0; i < 6; i++)
                {
                    xpoints[i] = (int) ((int) 10 * Math.cos(i * 2 * Math.PI / 6) + dx);
                    ypoints[i] = (int) ((int) 10 * Math.sin(i * 2 * Math.PI / 6) + dy);
                }

                Polygon hex = new Polygon (xpoints, ypoints, 6);
                g.fillPolygon(hex);
                System.out.print("Drawing " + r + "," + c);
            }
        }
    }
}

public HexGridTester(int r)
{
    radius = r;
    side = radius * 3/2;
    width = radius * 2;
    height = Math.sqrt(3) * radius;
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(0, 1));
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    panel = new TestPanel();
    panel.setLayout(new GridLayout(0,1));
    panel.addMouseListener(this);
    panel.addMouseMotionListener(this);
    frame.add(panel);
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
    panel.repaint();
}

@Override
public void actionPerformed(ActionEvent ae)
{

}

@Override
public void mouseClicked(MouseEvent me)
{

}

@Override
public void mousePressed(MouseEvent me)
{

}

@Override
public void mouseReleased(MouseEvent me)
{

}

@Override
public void mouseEntered(MouseEvent me)
{

}

@Override
public void mouseExited(MouseEvent me)
{

}

@Override
public void mouseDragged(MouseEvent me)
{

}

@Override
public void mouseMoved(MouseEvent me)
{
    //System.out.println(me.getPoint().x + " " +  me.getPoint().y + " " + mouseover);
    mouseover = convert(me.getPoint().x, me.getPoint().y);
    panel.repaint();
}

private Location convert(double x, double y)
{       
    double xt;
    double yt;

    double r;
    double c;

    double rt;
    double ct;

    double dr;

    ct = x / side;
    if(ct % 2 == 0)
    {
        rt = y / height;
    }
    else
    {
        rt =  (y - ( height) / 2) / (height);
    }

    xt = x - ct * side;
    yt = y - rt * height;

    if(yt > (height /2))
    {
        dr = 1;
    }
    else
    {
        dr = 0;
    }

    if(xt > (radius * Math.abs(.5 - yt/height)))
    {
        c = ct;
        r = rt;
    }
    else
    {
        c = ct - 1;
        r = rt - c%2 + dr;
    }
    return new Location((int) c, (int) r);
}

private static void runTestWindow()
{
    HexGridTester tester = new HexGridTester(25);
}

    public static void main(String[] args)
{
    Runnable run = new Runnable()
    {
        @Override
        public void run()
        {
            runTestWindow();
        }
    };

    javax.swing.SwingUtilities.invokeLater(run);
}
}
有帮助吗?

解决方案

@Override
public void paintComponents(Graphics g)
{
    System.out.println("CALLED");
    super.paintComponents(g);

Should be:

@Override
public void paintComponent(Graphics g)
{
    System.out.println("CALLED");
    super.paintComponent(g);

Respect the paint chain. Don't override any method but paintComponent(Graphics) for any JComponent.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top