手动翻译多边形
Manually translate polygon
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);
}
}
如何手动平移多边形?我需要这样做来创建游戏板。到目前为止我只完成了多边形的自动翻译,这绝对是我不需要的。
I mean like parameterize it so that it's not always 10.
那么您需要 class:
的参数
- 在您的 class 中创建一个方法,例如 `setTranslation(int x, int y) 并将 x/y 值保存到实例变量。
- 在 paintComponent() 方法中引用这些实例变量
- 然后当您创建六边形时,您可以手动设置平移。
类似于:
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 class 的构造函数。关键是如果您希望每个六边形都有不同的翻译,您需要在六边形中具有自定义属性 class。
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);
}
}
如何手动平移多边形?我需要这样做来创建游戏板。到目前为止我只完成了多边形的自动翻译,这绝对是我不需要的。
I mean like parameterize it so that it's not always 10.
那么您需要 class:
的参数- 在您的 class 中创建一个方法,例如 `setTranslation(int x, int y) 并将 x/y 值保存到实例变量。
- 在 paintComponent() 方法中引用这些实例变量
- 然后当您创建六边形时,您可以手动设置平移。
类似于:
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 class 的构造函数。关键是如果您希望每个六边形都有不同的翻译,您需要在六边形中具有自定义属性 class。