2017-05-16 66 views
3

我寫了一個短的遊戲。在現有的實現中,我有一個GridBagLayout,按鈕位於棋盤上。每個按鈕佔據整個網格。遊戲工作正常。我的下一個任務是將板改爲由六角形按鈕組成,而不是像當前那樣的矩形。我完全不知道該怎麼做。按鈕應該像這些關於圖片: target如何製作六角JButton

+2

那些不是六面體。 –

+2

其實,有很多關於JButton的形狀的帖子,但我不想通過它們全部找到答案。 [這些可能有所幫助](http://stackoverflow.com/search?q=%5Bjava%5D+%5Bjbutton%5D+shape) – CodingNinja

+0

[矩形以外形狀中的按鈕]的可能重複(http://stackoverflow.com/ questions/10785416/buttons-in-shapes-other-than-rectangles) – CodingNinja

回答

0

這是不是最漂亮的方式,但它至少給你一個想法:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class HexagonPattern extends JPanel { 
    private static final long serialVersionUID = 1L; 
    private static final int ROWS = 7; 
    private static final int COLUMNS = 7; 
    private HexagonButton[][] hexButton = new HexagonButton[ROWS][COLUMNS]; 


    public HexagonPattern() { 
     setLayout(null); 
     initGUI(); 
    } 


    public void initGUI() { 
     int offsetX = -10; 
     int offsetY = 0; 

     for(int row = 0; row < ROWS; row++) { 
      for(int col = 0; col < COLUMNS; col++){ 
       hexButton[row][col] = new HexagonButton(row, col); 
       hexButton[row][col].addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         HexagonButton clickedButton = (HexagonButton) e.getSource(); 
         System.out.println("Button clicked: [" + clickedButton.getRow() + "][" + clickedButton.getCol() + "]"); 
        } 
       }); 
       add(hexButton[row][col]); 
       hexButton[row][col].setBounds(offsetY, offsetX, 105, 95); 
       offsetX += 87; 
      } 
      if(row%2 == 0) { 
       offsetX = -52; 
      } else { 
       offsetX = -10; 
      } 
      offsetY += 76; 
     } 
    } 

    public static void main(String[] args) { 
     HexagonPattern hexPattern = new HexagonPattern(); 
     JFrame frame = new JFrame(); 
     frame.setTitle("Hexagon Pattern"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocation(new Point(700, 300)); 
     frame.add(hexPattern); 
     frame.setSize(550, 525); 
     frame.setResizable(false); 
     frame.setVisible(true); 
    } 

    //Following class draws the Buttons 
    class HexagonButton extends JButton { 
     private static final long serialVersionUID = 1L; 
     private static final int SIDES = 6; 
     private static final int SIDE_LENGTH = 50; 
     public static final int LENGTH = 95; 
     public static final int WIDTH = 105; 
     private int row = 0; 
     private int col = 0; 

     public HexagonButton(int row, int col) { 
      setContentAreaFilled(false); 
      setFocusPainted(true); 
      setBorderPainted(false); 
      setPreferredSize(new Dimension(WIDTH, LENGTH)); 
      this.row = row; 
      this.col = col; 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Polygon hex = new Polygon(); 
      for (int i = 0; i < SIDES; i++) { 
       hex.addPoint((int) (50 + SIDE_LENGTH * Math.cos(i * 2 * Math.PI/SIDES)), //calculation for side 
         (int) (50 + SIDE_LENGTH * Math.sin(i * 2 * Math.PI/SIDES))); //calculation for side 
      }  
      g.drawPolygon(hex); 
     } 

     public int getRow() { 
      return row; 
     } 

     public int getCol() { 
      return col; 
     } 
    } 
} 

測試吧!

此程序由2類:

  1. HexagonButton,它使用圖形繪製六邊形成JButton。當調用getRowgetCol時,它還返回行和列值。

  2. HexagonPattern,這是主要的類。它通過將它們與setBounds(x, y, width, height)一起排列而形成模式。它使用ActionListener來打印點擊的Hexagon的座標,通過調用getRowgetCol

就像我說的,這不是最棒的程序。如果你想讓六邊形變小,那麼你將不得不改變許多變量。