字体,如何扩展它们
我想知道是否有一种方法可以通过 createFont 和deriveFont 方法返回我自己的 Font 类来扩展 Font 类。我的意思是这样的......
public class MyFont extends Font {
// Constructor
public MyFont (...) {
super(...);
}
// createFont method
public static MyFont createFont (...) {
// body
}
// deriveFont method
public static MyFont deriveFont (...) {
// body
}
}
我已经尝试过,但我无法检索任何字体,并且在执行此操作时,我得到的字体是默认字体(我的意思是“对话框”)。
这样做的原因是为了最大限度地减少其 VM 的后续 Java 发行版的最终更改所产生的影响。
这是上面调用的代码:
MyFont onePoint=MyFont.createFont(MyFont.TRUETYPE_FONT,fontStream, size);
然后在 MyFont 中,我编码:
public static MyFont createFont (int i, InputStream io, int size) throws FontFormatException, IOException {
Font font = Font.createFont(i, io);
MyFont kfont = new
MyFont(font.getName(),font.getStyle(),font.getSize());
return kfont;
}
I was wondering if there is a way for extending Font class in a manner that I could return my own Font class by createFont and deriveFont methods. I mean something like this...
public class MyFont extends Font {
// Constructor
public MyFont (...) {
super(...);
}
// createFont method
public static MyFont createFont (...) {
// body
}
// deriveFont method
public static MyFont deriveFont (...) {
// body
}
}
I've tryied but I could not retrieve any font, and when doing it the font I got was the default one (I mean "Dialog").
The reason for doing is is to minimize the impact produced by an eventual change in later Java distributions of its VM.
This is the code summoned above:
MyFont onePoint=MyFont.createFont(MyFont.TRUETYPE_FONT,fontStream, size);
Then in MyFont, I coded:
public static MyFont createFont (int i, InputStream io, int size) throws FontFormatException, IOException {
Font font = Font.createFont(i, io);
MyFont kfont = new
MyFont(font.getName(),font.getStyle(),font.getSize());
return kfont;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
0号。如果有人想将通用 Font 传递给您的代码,并且您需要参数是 MyFont 的实例,该怎么办?这有什么实际意义吗?您尝试使用继承来调整一些不太适合 Java OO 范式的生命周期协议(在本例中为对象创建)。请改用工厂方法/原型。
第一。 createFont() 不可重写,因为它是静态的。您可以在您的类中提供您自己的版本,但使用相同的方法名称可能会使您的代码的其他用户(以及稍后的您)感到困惑。
第二。在提供一些 createFont() 和可能的新版本deriveFont() 的快捷方式时,您无法避免链接到您想要控制其更改的 API,因此,考虑到 java 实现的更改,您无论如何都必须重新编码/重新编译。正确的依赖避免涉及一些反射和字符串编码的类名,以捕获大量 ClassNotFound 异常以及 API 更改时可能发生的其他情况。你不想要这样。时期。
第三。放松一下,真的,这段代码非常稳定,您的应用程序过时并从头开始重写的可能性比任何人弃用这些方法都要大得多,更不用说删除它们了。这远远超出了 2012 年世界末日(或任何其他比宇宙热寂更接近的相关日期)。
最后,只需添加几个易于区分的静态快捷方式:链接到 Font.creatFont() 的 MyFonts.create() 和链接到 font.derive(otherArgs) 的另一个 MyFonts.derive(Font font, otherargs) 。保持简单,伙计,并密封 MyFonts() 构造函数,以表明这是一个工厂/实用程序类。
0th. What if someone wants to pass generic Font to your code, and you require argument being instance of MyFont. Does it make any practical sense? You try to use inheritance to adjust some lifecycle protocols (object creation in this case) which does not fit well into Java OO-paradigm. Use a factory method/prototype instead.
1st. createFont() is not overridable as it's static. You may provide your own version in your class, but using the same method name may confuse other users of your code (and, later on, you too).
2nd. while providing some shortcuts to createFont() and possible new versions of deriveFont() you don't avoid linking against the API you want to control changes of, so you'd have to recode/recompile anyway, given java implementations change. Proper dependency avoidance involves some reflection and String-coded class names with catching lots of ClassNotFound exceptions and other whatnot which may happen on an API change. You don't want that. Period.
3rd. just relax, really, this code is quite stable and chances your app gets outdated and rewritten from scratch are much greater than anyone even deprecates those methods, leave alone removing them. this is well beyond the end of world in 2012 (or any other more or less relevant date closer than heat death of the universe).
To wrap it up, just add couple of easily distinguishable static shortcuts: MyFonts.create() chained to the Font.creatFont(), and another MyFonts.derive(Font font, otherargs) chained to font.derive(otherArgs). Keep it simple, dude, and seal the MyFonts() constructor, to indicate this is a factory/utility class.