我正在尝试创建一个应用程序,当我单击背景纹理图像时,我会放置一个点(它是一个带有点图像的图像按钮,并且仅用于显示)。现在,单击后,当我将鼠标指针移离该点时,我希望有一条细线从该点开始并在当前指针所在的位置结束。一旦我第二次单击背景,该线就会固定,起点和终点作为第一个和第二个点。
我能够画点,但我无法画线:
1.当我移动鼠标时,我无法绘制从第一个点到鼠标指针的移动线。 (帮助我更好地放置线路)
2.当我第二次按下鼠标时,我能够创建一个点,但不能创建一条线。
我尝试在鼠标移动的输入监听器内创建一个 Actor (具有由线条图像制成的纹理的图像按钮),但 Actor 只有原始x,y点和宽度/高度。我无法提及结束 x/y 坐标:
actor.addListener(new InputListener() {
@Override
public boolean mouseMoved(InputEvent event, float xSecond, float ySecond) {
dot1.setPosition(xSecond, ySecond); //dot imagebutton actor
line.setPosition(xFirst, yFirst); //Line starting points(
stage.addActor(dot1);
stage.addActor(line);
return false;
}
});
我需要这方面的帮助,因为我被困在这一步了。请引用截图:Line between the dots 1 and 2
最佳答案
使用 ShapeRenderer 在 2 点之间绘制线条:
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(1, 1, 0, 1);
shapeRenderer.line(x, y, x2, y2);
shapeRenderer.end();
如果你想选择线条的粗细,你可以使用 ShapeRenderer rectLine 方法:
rectLine(float x1, float y1, float x2, float y2, float width)
Draws a line using a rotated rectangle, where with one edge is centered at x1, y1 and the opposite edge centered at x2, y2.
关于java - Libgdx 在两次鼠标点击之间绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36545643/