gpt4 book ai didi

java - 使用 JavaFX 8 Canvas 缩放级别

转载 作者:行者123 更新时间:2023-12-01 11:17:46 26 4
gpt4 key购买 nike

我的 JavaFX 8 应用程序中有以下代码,用于控制添加到场景中的 Canvas 元素的变换比例:

import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.transform.Affine;
import me.mazeika.dengine.editor.input.Mouse;

import java.util.List;

// classes that implement Transformer are ones that are meant to simply modify
// the canvas's transform based on some input and nothing else
public class ScrollTransformer implements Transformer
{
private static final double MIN_SCALE = .1;
private static final double MAX_SCALE = 5;

private double currentScale = 1;

// called every frame from an AnimationTimer's handle()
@Override
public void update(Canvas canvas, Affine transform, long now, long delta)
{
// when you scroll up, for example, the integer +1 is added to this list
// (and -1 for down). When this method is called, that list is cleared.
List<Integer> pendingScrolls = Mouse.getAndRemovePendingScrolls();

if (! pendingScrolls.isEmpty())
{
// runs 'transform.inverseTransform(positionOfMouseRelativeToCanvas)'
Point2D pivot = Mouse.getTransformedPosition();

for (int direction : pendingScrolls)
{
boolean up = direction > 0;

// the purpose of this is to keep scaling by a minuscule amount
// and check each time if we've went out of the bounds of
// MIN_SCALE or MAX_SCALE
for (int i = 0; i < 1000; i++)
{
double toAppend = (up ? 1.0001 : .9999);

currentScale += toAppend - 1;

if (currentScale <= MIN_SCALE)
{
currentScale = MIN_SCALE;
break;
}
else if (currentScale >= MAX_SCALE)
{
currentScale = MAX_SCALE;
break;
}

transform.appendScale(toAppend, toAppend, pivot);
}
}
}
}
}

问题是 transform.appendScale() 就像它所说的那样,附加到变换中,并且由于浮点精度,每次滚动时缩放级别都会略有不同。

由于某些原因,我必须修改 GraphicsContext变换,而不是直接 Canvas (换句话说,要翻译,我必须使用 transform.setTx(...) 而不是 canvas.setTranslateX(...)

解决方案是设置变换的比例,同时还要考虑枢轴点(即鼠标光标)。所以,我的问题是我该怎么做?

最佳答案

问题不一定在于浮点精度,而是附加 0.9 的小数位数并不会减少与 1.1 增加的小数位数相同的量。那么,解决方案就是简单地将滚出功能替换为滚入功能的,如下所示:

import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.transform.Affine;
import javafx.scene.transform.NonInvertibleTransformException;
import javafx.scene.transform.Scale;
import me.mazeika.dengine.editor.input.Mouse;

import java.util.List;

public class ScrollTransformer implements Transformer
{
private static final int MAX_SCALE_LEVELS = 80;

private int scaleLevel = 30;

@Override
public void update(Canvas canvas, Affine transform, long now, long delta)
{
List<Integer> pendingScrolls = Mouse.getAndRemovePendingScrolls();

if (! pendingScrolls.isEmpty())
{
Point2D pivot = Mouse.getTransformedPosition();

for (int direction : pendingScrolls)
{
boolean up = direction > 0;

if (! up && scaleLevel == 0 || up && scaleLevel == MAX_SCALE_LEVELS) continue;

scaleLevel += up ? 1 : -1;

Scale scale = new Scale(1.1, 1.1, pivot.getX(), pivot.getY());

if (up)
{
transform.append(scale);
}
else
{
try
{
transform.append(scale.createInverse());
}
catch (NonInvertibleTransformException e)
{
e.printStackTrace();
return;
}
}
}
}
}
}

关于java - 使用 JavaFX 8 Canvas 缩放级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com