我正在向 JPanel
绘制一些 2D 图形。
如果更改 JPanel
大小,我不明白如何使用比例?
我有一个图像,它由多条直线和圆圈组成。 是否可以计算一个变换并将错误计算的值相乘?如何?能给我举个例子吗?
最佳答案
一个巧妙的方法(但不是最好的)是为宽度和高度设置一个最终变量,并且所有内容都是根据这些变量计算的。那么你可以有一个比例变量,它的大小是(JPanel/Final Variable),当你执行 g.draw() 时,你可以将宽度设置为 width * wScale,将高度设置为 height * hScale。这是一个例子
public final WIDTH = 1080;
public final HEIGHT = WIDTH * 16 / 9;
private float wScale = 1;
private float hScale = 1;
public static void mainLoop()
{
wScale = jpanelWidth / (WIDTH * 1.0);
hScale = jpanelHeight / (HEIGHT * 1.0);
//makes the image to draw using the final width and height for calculations
someclass.makeImage(WIDTH, HEIGHT);
//gets an image that has the dimentions of WIDTH and HEIGHT
someimage = someclass.getImage();
//draws the image using its width and scales it to the actual size by multiplying it by the scaler
//the two zeros are at position (0, 0), and null is just the observer.
somegraphics.draw(someimage, 0, 0, (WIDTH * wScale), (HEIGHT * hScale), null);
}
如果您需要更多信息或有疑问,可以要求澄清,或亲自与我联系。
关于java - 如何在java 2DG中使用比例尺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29217166/