如何在java 2DG中使用scale

时间:2015-03-23 17:58:36

标签: java 2d

我正在向JPanel绘制一些2Dgraphics。 如果我更改JPanel尺寸,我不明白如何使用比例尺?

我有一个图像,它由多个线条和圆圈组成。 是否可以计算转换并将错误计算的值相乘?怎么样?你能举个例子吗?

1 个答案:

答案 0 :(得分:0)

一种巧妙的方法(但不是最好的方法)是为宽度和高度设置一个最终变量,并根据这些变量计算所有内容。那么你可以得到一个比例变量(JPanel / final变量),当你做g.draw()时你可以设置宽度为* wScale,高度设置为高度* 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);

}

如果您需要更多信息或有疑问,可以要求澄清,或亲自与我联系。