当前位置: 技术问答>java相关
[源码分析]:能给我解释一下这段代码否?
来源: 互联网 发布时间:2015-09-08
本文导语: public int print(Graphics g, PageFormat pf, int page) throws PrinterException{ ////??? if (page >= 1) return Printable.NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D)g;///////?????? g2.setPaint(C...
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException{ ////???
if (page >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;///////??????
g2.setPaint(Color.black);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.draw(new Rectangle2D.Double(0, 0,
pf.getImageableWidth(), pf.getImageableHeight()));
drawPage(g2);
return Printable.PAGE_EXISTS;
}
就是标记????的两个地方!
一个地方20分!
[金牌承诺]在线等待,解决放分
throws PrinterException{ ////???
if (page >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;///////??????
g2.setPaint(Color.black);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.draw(new Rectangle2D.Double(0, 0,
pf.getImageableWidth(), pf.getImageableHeight()));
drawPage(g2);
return Printable.PAGE_EXISTS;
}
就是标记????的两个地方!
一个地方20分!
[金牌承诺]在线等待,解决放分
|
throws PrinterException语句负责抛出一个关于打印的异常错误
也就是你的代码可能会出现一些未知的情况导致你无法打印,通过这种捕获异常来保护代码
Graphics2D g2 = (Graphics2D)g 通过将参数g转化为Graphics2D类型来创建一个新的Graphics2D实例g2
也就是你的代码可能会出现一些未知的情况导致你无法打印,通过这种捕获异常来保护代码
Graphics2D g2 = (Graphics2D)g 通过将参数g转化为Graphics2D类型来创建一个新的Graphics2D实例g2
|
throws PrinterException把PrinterException异常向上抛出,抛给了这个方法的调用者,就是说public int print(Graphics g, PageFormat pf, int page)这个方法会引发一个PrinterException异常,而你在这个方法体中没有捕捉它(try catch),就是没有对它处理,Java为了代码的安全和稳定,要求编程者必须对异常做出处理,所以如果你不在方法体中处理,就必须显示的抛出(用throws语句),同样在此方法调用者中,如果你仍然不处理,则必须继续向上抛出此异常,直到你处理了为止,如果你一直没有处理最终,异常将会被抛到main(即程序入口)方法中,当然你还可以把它抛出,那么它就由JVM处理了:)。
一般处理异常有两种不同的思路:第一个是就近处理,哪抛出异常,就在那处理。
第二个是集中处理,就是把异常都向上抛出,在适当的地方用try加多个catch,集中处理。
一般处理异常有两种不同的思路:第一个是就近处理,哪抛出异常,就在那处理。
第二个是集中处理,就是把异常都向上抛出,在适当的地方用try加多个catch,集中处理。
|
The PrinterException
class and its subclasses are used to indicate that an exceptional condition has occurred in the print system.
This Graphics2D
class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform.
class and its subclasses are used to indicate that an exceptional condition has occurred in the print system.
This Graphics2D
class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform.