【问题标题】:Manually translate polygon手动平移多边形
【发布时间】:2016-06-01 11:17:06
【问题描述】:
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);
}
}

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

【问题讨论】:

  • 不要在屏幕截图中发布代码。以文本形式发布。
  • 问题不清楚。更具体。
  • 怎么可能更清楚?我正在尝试翻译多边形,我已经评论了我在哪里自动翻译了多边形,这不是我想要做的。
  • So far I've only accomplished automatic translation of polygons which is definitely what I don't need. 为什么?如果它自动工作,为什么要浪费时间尝试手动做某事?我不明白问题的重点。
  • 如果你熟悉游戏的十六进制,那么所有的棋盘都已经移动了多边形,所以我不能让它自动。

标签: java swing jpanel awt


【解决方案1】:

我的意思是像参数化它,使它不总是 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 具有不同的翻译,您需要在 Hexagon 类中具有自定义属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2022-12-01
    • 2016-09-02
    • 1970-01-01
    • 2022-09-29
    • 2021-10-21
    • 2023-03-09
    相关资源
    最近更新 更多