2016-02-19 85 views
-1
public class Hexagon extends JPanel { 
    // Team that controls the hexagon 
    public int controller; // 0 neutral // 1 is team 1 // 2 is team 2 
    // Color of the hexagon 
    // /* All attributes of the board, used for size an boarder etc... */ Board board 
    // /* Determines where the hexagon sits on the game board */ int position 

public static void main(String args[]) 
{ 
    JFrame j = new JFrame(); 
    j.setSize(350, 250); 
    for(int i = 0; i < 121; i++) 
    { 
     Hexagon hex = new Hexagon(); 
     j.add(hex); 
    } 
    j.setVisible(true); 
} 

@Override 
public void paintComponent(Graphics shape) 
{ 
    super.paintComponent(shape); 

    Polygon hexagon = new Polygon(); 

    // x, y coordinate centers, r is radius from center 
    Double x, y; 
    // Define sides of polygon for hexagon 
    for(int i = 0; i < 6; i++) 
    { 
     x = 25 + 22 * Math.cos(i * 2 * Math.PI/6); 
     y = 25 + 22 * Math.sin(i * 2 * Math.PI/6); 
     hexagon.addPoint(x.intValue(), y.intValue()); 
    } 
    // Automatic translate 
    hexagon.translate(10, 10); 
    // How do I manually control translate? 
    shape.drawPolygon(hexagon); 
} 
} 

如何手动平移多边形?我需要做它来创建一个游戏板。到目前为止,我只完成了多边形的自动翻译,这绝对是我不需要的。手动平移多边形

+0

请勿在截图中发布代码。将其作为文本发布。 – khelwood

+0

这个问题还不清楚。更具体一些。 –

+0

它怎么可能更清楚?我正在尝试翻译多边形,并且我已经评论了自动翻译多边形的位置,这不是我正在尝试执行的操作。 –

回答

3

我的意思是像参数,以便它并不总是10

然后你需要为你的类参数:

  1. 创建类像`setTranslation(INT一个方法X ,int y)并将x/y值保存为实例变量。
  2. 在paintComponent()方法中引用这些实例变量
  3. 然后,当您创建Hexagon时,您可以手动设置翻译。

喜欢的东西:

public void setTranslation(int translationX, int translationY) 
{ 
    this.translationX = translationX; 
    this.translationY = translationY; 
} 

... 

//hexagon.translate(10, 10); 
hexagon.translate(translateX, translateY); 

... 

Hexagon hex = new Hexagon(); 
hex.setTranslation(10, 10); 

或者,你可以通过平移值作为参数传递给你的六角类的构造函数。重点是如果您希望每个Hexagon具有不同的翻译,则需要在Hexagon类中具有自定义属性。

+1

哇,我现在觉得很愚蠢,这很有道理。 –