gpt4 book ai didi

java - 如何仅对某些事物使用 Graphics2D g.scale() 而对其他事物不使用?

转载 作者:行者123 更新时间:2023-12-02 04:46:14 24 4
gpt4 key购买 nike

我希望能够放大我的项目,并且它与 g.scale(x, x) 和 g.translate(x, y) 的组合完美配合。但是,我想要一些不与其他所有东西一起缩放的东西(小 map )。具体来说,我正在制作一个小 map ,它将显示整个屏幕和 1x1 像素的单位。

   for(Unit u : units) { //cycle through arraylist of units
u.drawUnit(g, selectedUnits.contains(u)); //draws the unit, scales perfectly
g.setColor(Color.blue);
g.fillOval(rnd((u.getX()/4)/scale), rnd((u.getY()/4)/scale), rnd(1 + 1/scale), rnd(1 + 1/scale));
//rnd() is just a shorter (int)Math.round()
//The minimap's dimensions are width/4 x height/4
}

所以我想知道是否可以用更简单的方法来做到这一点,因为这使得它在某些缩放比例下看起来非常奇怪。

最佳答案

您可以获取/设置与 Graphics2D 对象关联的 AffineTransform 对象。这使您可以轻松地在两个或多个变换之间来回切换,而无需通过 Graphics2D 对象的缩放/平移/旋转方法进行数学计算。例如:

//render something default transform
AffineTransform defaultTransform = g.getTransform();
AffineTransform newTransform = new AffineTransform(defaultTransform);
newTransform.setScale(xScale, yScale);
g.setTransform(newTransform);
//render something with the new transform
g.setTransform(oldTransform);
//render something with the original transform

关于java - 如何仅对某些事物使用 Graphics2D g.scale() 而对其他事物不使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29639331/

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