Java Swing:JPanel不会画画

时间:2014-04-29 07:11:54

标签: java swing jpanel paintcomponent repaint

现在,我正在尝试制作一个Hex Grid GUI,突出显示鼠标悬停在其上的任何十六进制。但是,应该显示六边形的JPanel没有显示出来。

在这种情况下,我真的不知道repaint()和paintComponents()之间发生了什么。我试图查找解决方案,其中涉及将JPanel设置为可见的层次结构,我确信这样做了,但它仍然无效。

代码稍长

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

1 个答案:

答案 0 :(得分:4)

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

应该是:

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

尊重油漆链。不要覆盖任何方法,但paintComponent(Graphics)代替JComponent