2016-11-23 65 views
2

對不起,完全初學java。我之前做過一個方形網格,對於跳棋遊戲,但是我在六角網格上遇到了麻煩。然而,我遵循與正方形網格相同的想法,但是我的JPanel上有小點,儘管我不確定這是什麼原因,但我環顧四周,試圖找出可能出錯的地方。JPanel不顯示組件? (Hexagon Grid)

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

GameHexagon.java;要被繪製到電路板

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六邊形部件;負責繪製六邊形 這段代碼是我從程序員那裏得到的,只是做了一些小修改,我自己試了一下,但是,我試過了,不知道是什麼錯,或者爲什麼它與我無關這樣做。

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;測試文件添加板到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); 
    } 
} 

我可以用一個方格奠定了六角圖標,但失敗的目的,因爲我想的網格對齊彼此

*-*-*-*-* 
-*-*-*-*- 
*-*-*-*-* 

或類似

東西在這個 ![Only used the size of 2 in this one] 1

+0

那是完整的代碼?這就是我使用的所有代碼 –

+0

你有很多嚴重的缺陷:bkgnd.draw(g2,x,y,2,true); x在這個方法裏面沒有關係......如果你是一個完全的開端者,你在用一個複雜的怪物做什麼? – gpasch

+0

不知道我自己,我想我會嘗試它作爲一個項目。我假設一旦構建了一個GameHexagon(row,col,x,y,true),就會設置x和y。在組件中,它將使用bkgnd.draw(g2,x,y,2,true)進行繪製。 –

回答

0

使用一個大小爲2所以,我注意到,你沒有用對多邊形和java.awt.*類中的.fill()方法。我建議使用它。以下是一些可以幫助您制定多邊形方法的網站鏈接。

http://mathbits.com/MathBits/Java/Graphics/GraphingMethods.htm http://www.java2s.com/Code/Java/2D-Graphics-GUI/DrawaPolygon.htm

希望這是有用的!

+0

添加了'g2.fill(bkgnd)'但是相同的圓點,而Hexagon.java是一個小孩Polygon的,不應該繼承嗎? –

+0

的確,我沒有想到,我會仔細閱讀您的代碼 –

+0

此問題已被詢問。這裏是一個鏈接到答案:http://stackoverflow.com/questions/20734438/algorithm-to-generate-a-hexagonal-grid-with-coordinate-system –