Java JPanel不显示组件?(六边形网格)

Java JPanel不显示组件?(六边形网格),java,hexagonal-tiles,Java,Hexagonal Tiles,对不起,我是java的初学者。我以前做过一个正方形格子,用于跳棋游戏,但是我在六边形格子上遇到了麻烦。我遵循同样的想法,使用正方形网格,但是,我的JPanel上有小点,虽然我不确定这是什么原因,但我环顾四周,试图找出可能的错误 GameBoard.java;负责绘制六边形 import java.awt.Color; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; impo

对不起,我是java的初学者。我以前做过一个正方形格子,用于跳棋游戏,但是我在六边形格子上遇到了麻烦。我遵循同样的想法,使用正方形网格,但是,我的JPanel上有小点,虽然我不确定这是什么原因,但我环顾四周,试图找出可能的错误

GameBoard.java;负责绘制六边形

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

//import CheckerBoard.GameSquareMouseListener;


public class GameBoard extends JPanel {
    protected final static int size = 8;
    protected final static int radius = 50;
    protected final static int padding = 5;
    private Position oldPosition = null;


    public GameBoard(){
        super ();
        int numPieceSpots = 0;
        int x = 0; int y = 0;
        int xOff = (int) Math.cos(Math.toRadians(30)) * (radius + padding);
        int yOff = (int) Math.sin(Math.toRadians(30)) * (radius + padding);
        int half = size / 2;
        GameHexagon hex = null;

        for (int r = 0; r < size; r++){
            for (int c = 0; c < size; c++)
            {
                if (((r + c) % 2 == 0) && (r % 2 == 1)){
                    hex = new GameHexagon(r, c, x, y);
                }
                else if (((r + c) % 2 == 0) && (r % 2 == 0))
                {
                    hex = new GameHexagon(r, c, x, y);
                }
                else {
                    hex = new GameHexagon(r, c, x, y);
                }
                if (c == (size - 1)){
                    x = 0;
                    y += yOff;
                }
                add (hex);
                x += xOff*c;
            }
        }
        repaint();
    }
}
Hexagon.java;负责绘制六边形 这段代码是我从stackoverflow上的一个程序员那里得到的,我做了一些小的编辑,我自己也试过了,它成功了,但是,我浏览了一遍,不确定是什么错了,或者为什么它与我正在做的不兼容

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Hexagon extends Polygon {

    private static final long serialVersionUID = 1L;

    public static final int SIDES = 6;

    private Point[] points = new Point[SIDES];
    private Point center = new Point(0, 0);
    private int radius;
    private int rotation = 90;


    public Hexagon(Point center, int xlabel, int ylabel, int radius) {
        npoints = SIDES;
        xpoints = new int[SIDES];
        ypoints = new int[SIDES];

        this.center = center;
        this.radius = radius;

        updatePoints();
    }

    public Hexagon(int x, int y, int xlabel, int ylabel, int radius) {
        this(new Point(x, y), xlabel, ylabel, radius);
    }


    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;

        updatePoints();
    }

    public int getRotation() {
        return rotation;
    }

    public void setRotation(int rotation) {
        this.rotation = rotation;

        updatePoints();
    }

    public void setCenter(Point center) {
        this.center = center;

        updatePoints();
    }

    public void setCenter(int x, int y) {
        setCenter(new Point(x, y));
    }

    private double findAngle(double fraction) {
        return fraction * Math.PI * 2 + Math.toRadians((rotation + 180) % 360);
    }

    private Point findPoint(double angle) {
        int x = (int) (center.x + Math.cos(angle) * radius);
        int y = (int) (center.y + Math.sin(angle) * radius);

        return new Point(x, y);
    }

    protected void updatePoints() {
        for (int p = 0; p < SIDES; p++) {
            double angle = findAngle((double) p / SIDES);
            Point point = findPoint(angle);
            xpoints[p] = point.x;
            ypoints[p] = point.y;
            points[p] = point;
        }
    }

    public void draw(Graphics2D g, int x, int y, int lineThickness, boolean border) {
        // Store before changing.
        Stroke tmpS = g.getStroke();
        Color tmpC = g.getColor();

        g.setStroke(new BasicStroke(lineThickness, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));

        if (border)
            g.fillPolygon(xpoints, ypoints, npoints);
        else
            g.drawPolygon(xpoints, ypoints, npoints);

        // Set values to previous when done.

        g.setColor(tmpC);
        g.setStroke(tmpS);

    }



}
我可以使用一个正方形网格来布置十六进制图标,但这并没有达到目的,因为我希望网格彼此对齐

*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
或者类似的东西

在本例中使用了2号
]

因此,我注意到您没有使用多边形和
java.awt.*
类中的
.fill()
方法。我建议用它来代替。这里有几个网站链接,可以帮助使用多边形方法


希望这是有用的

添加了
g2.fill(bkgnd)
但是相同的点,Hexagon.java是多边形的子对象,它不应该继承吗?是的,我没有想到,我会更仔细地阅读你的代码。这个问题已经被问过了。下面是答案的链接:我已经看过了。这就是我获得hexagon.java文件的地方,然而,我正在尝试使用JComponent,以便在游戏中使用六边形网格。考虑到我必须设置边框,引入带有形状的MouseEvent要困难得多,而且我还是新手,所以我仍在尝试获取基础知识。这是完整的代码吗?这就是我使用的所有代码。你有很多严重的缺陷:bkgnd.draw(g2,x,y,2,true);xy在这个方法中没有相关性。。。如果你是一个彻头彻尾的新手,你要对一个复杂的怪物做什么?我自己也不确定,我想我会把它作为一个项目来尝试。我假设一旦我构造了一个GameHexagon(行,列,x,y,true),x和y将被设置。在组件中,它将使用bkgnd.draw(g2,x,y,2,true)绘制。我想我假设基于x和y轴的绘制将更容易绘制组件,因为我不想使用网格
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

/**
   A program that allows users to edit a scene composed
   of items.
*/
public class GameTest
{

    public static void main(String[] args) {
        JFrame f = new JFrame();
        GameBoard p = new GameBoard();

        f.add (p, BorderLayout.CENTER);

        f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        f.setSize (600, 600);
        f.setVisible (true);
   }
}
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*