2017-05-16 6 views
3

나는 짧은 게임을 썼다. 기존 구현에서는 체스 보드에있는 버튼이있는 GridBagLayout이 있습니다. 각 버튼은 전체 그리드를 차지합니다. 게임은 정상적으로 작동합니다. 내 다음 과제는 보드를 현재처럼 사각형이 아닌 육각 버튼으로 변경하는 것입니다. 나는이 일을하는 법을 완전히 모른다. target육각 JButton을 만드는 방법

+2

이들은 육면체가 아닙니다. –

+2

사실, JButton의 모양에 관한 많은 게시물이 있지만 답변을 찾기 위해 모든 것을 통해 물고기를 먹고 싶지 않았습니다. [도움이 될 수도] (http://stackoverflow.com/search?q=%5Bjava%5D+%5Bjbutton%5D+shape) – CodingNinja

+0

[직사각형 이외의 모양의 단추] 가능한 중복 (http://stackoverflow.com/ 질문/10785416/buttons-in-shapes-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 종류로 구성하십시오 JButton으로 육각형을 그리는 그래픽을 사용

  1. HexagonButton. 또한 getRow 또는 getCol이 호출 될 때 행 및 열 값을 반환합니다.

  2. HexagonPattern 주 클래스입니다. setBounds(x, y, width, height)으로 레이아웃하여 패턴을 만듭니다. ActionListener을 사용하여 클릭 한 육각형의 좌표를 인쇄합니다. getRowgetCol을 호출합니다.

내가 말했듯이, 이것은 최고의 프로그램이 아닙니다. 육각형을 작게 만들려면 많은 변수를 변경해야합니다.