Java 图形中的翻译方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6591832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:02:20  来源:igfitidea点击:

translate method in Graphics

javaprinting

提问by saplingPro

I cam across this recently : here

我最近遇到了这个:这里

 public int print(Graphics g, PageFormat pf, int page) throws
                                                    PrinterException {

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY()); // line 2

    g.drawString("Hello world!", 100, 100);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

I don't understand the line 2 (commented)in this snippet.(g2d.translate(pf.getImageableX(), pf.getImageableY());)g2d is a reference of Graphics2dand translateis a method found in Graphic class. Then how does it work ?

我不明白这个片段中的第 2 行(注释)(g2d.translate(pf.getImageableX(), pf.getImageableY());)g2d 是Graphic 类中的一个引用Graphics2d并且translate是一个方法。那么它是如何工作的呢?

采纳答案by Adam Smith

Edit:The translate method is found in both the Graphics2D class and in the Graphics class since Graphics2D is a child class of Graphics. Being a child of Graphics, it implements all of its methods (including translate), which is why it works.

编辑:在 Graphics2D 类和 Graphics 类中都可以找到 translate 方法,因为 Graphics2D 是 Graphics 的子类。作为 Graphics 的子代,它实现了它的所有方法(包括 translate),这就是它工作的原因。

The translate method in your example is used to move the g2d's origin point to pf's origin point.

您示例中的 translate 方法用于将 g2d 的原点移动到 pf 的原点。

Basically, what it does is tell the program to translate (move) every point from g2d to pf's corresponding point.

基本上,它所做的是告诉程序将每个点从 g2d 转换(移动)到 pf 的对应点。

Let's say g2d starts at (0,0) and pf starts at (100,100), after the translation, g2d's (0,100) point would now be at (100,200), which is pf's (0,100) point since it doesn't start at the same place.

假设 g2d 从 (0,0) 开始,pf 从 (100,100) 开始,在翻译之后,g2d 的 (0,100) 点现在位于 (100,200),这是 pf 的 (0,100) 点,因为它不是从一样的地方。

I'm having a hard time making it clear and easy to understand, but if you don't understand what I mean, I'll try explaining it better or just delete the answer altogether and let someone else explain it.

我很难说清楚和容易理解,但如果你不明白我的意思,我会试着解释得更好,或者干脆删除答案,让别人解释。

回答by Sam Barnum

The code looks like it comes from a java.awt.print.Printableimplementation. This is supposed to draw content to a Graphics object which is set to the printer. The translatecall is used if the PageFormat has top/left margins, so the content starts within the printable area of the PageFormat, instead of at 0,0 on the paper, which is not within the printable area.

代码看起来像是来自一个java.awt.print.Printable实现。这应该将内容绘制到设置到打印机的 Graphics 对象。translate如果 PageFormat 具有上/左页边距,则使用该调用,因此内容在 PageFormat 的可打印区域内开始,而不是在不在可打印区域内的纸张上的 0,0 处开始。