Sorry, ein kompletter Anfänger zu Java. Ich habe vorher ein quadratisches Gitter für ein Dame Spiel gemacht, aber ich habe Probleme für ein hexagonales Gitter. Ich folgte der gleichen Idee mit dem quadratischen Raster, aber ich habe kleine kleine Punkte auf meinem JPanel, obwohl ich nicht sicher bin, was der Grund dafür ist, habe ich mich umgesehen und versucht zu sehen, was falsch sein könnte.JPanel zeigt keine Komponenten an? (Hexagon Grid)
GameBoard.java; verantwortlich für die Hexagons Zeichnung
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();
}
}
GameHexagon.java; die Sechseck-Komponenten auf die Leiterplatte
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
public class GameHexagon extends JComponent
{
private int x;
private int y;
private Position pos = null;
//private GamePiece piece = null;
public GameHexagon (int row, int col, int xp, int yp)
{
super();
pos = new Position (row, col);
x = xp;
y = yp;
}
public void paintComponent (Graphics g)
{
super.paintComponents (g);
Graphics2D g2 = (Graphics2D) g;
int height = getHeight();
int width = getWidth();
//Rectangle2D.Double bkgnd = new Rectangle2D.Double (x, y, width, height);
Hexagon bkgnd = new Hexagon(x, y, pos.r, pos.c, 50);
bkgnd.draw(g2, x, y, 2, true);
g.setColor(new Color(0xFFFFFF));
g.drawString("TEST", x - 25, y + 25);
}
}
Hexagon.java gezogen werden; verantwortlich für das Zeichnen der Sechsecke Dieser Code bekam ich von einem Programmierer auf Stackoverflow mit kleinen Änderungen, und ich habe es selbst versucht und es funktionierte, aber ich überflog es und nicht sicher, was falsch ist, oder warum es nicht kompatibel mit dem was ich bin tun.
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);
}
}
GameTest.java; Testdateien, die Hex-Icons Layout hinzufügen Platte auf JFrame
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);
}
}
Ich konnte ein quadratisches Gitter verwenden, aber das den Zweck, weil ich das Gitter will miteinander
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
oder auszurichten etwas ähnliches
verwendet, um eine Größe von 2 in dieser ] 1
das ist der vollständige Code? Das ist der Code, den ich benutzt habe –
Sie haben viele schwerwiegende Fehler: bkgnd.draw (g2, x, y, 2, wahr); das x y hat keine Relevanz in dieser Methode ... Wenn du ein Anfänger bist, was machst du mit einem komplizierten Monster? – gpasch
Ich bin mir nicht sicher, ich dachte, ich würde es als Projekt versuchen. Und ich nehme an, dass x und y gesetzt werden, sobald ich ein GameHexagon konstruiere (row, col, x, y, true). Und innerhalb der Komponente wird mit bkgnd.draw gezeichnet (g2, x, y, 2, true). –