通俗来讲,不可能将一只是猫的动物强转为狗
再说Graphics2D:
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 96);
g2.setFont(font);
g2.drawString("jade", 40, 120);
}
}
g之所以能强转为Graphics2D,多是因为它的运行时实例应是两者(Graphics,Graphics2D)的共同子类,当然Graphics2D本身是Graphics的子类,这里就像将一只是猫的动物强转为四脚兽
。
只有强转后,方可调用自Graphics2D特有的方法如setRenderingHint
另外一点只有向下转型才需强转(explicit casting),向上转型是自动完成的,比如当你将一个子类对象赋给一个父类型变量时。
from: http://topic.csdn.net/u/20110223/22/a00f64a0-c7ba-47fa-a4b1-4a703cc66df2.html?seed=1767655541&r=71791492#r_71791492
分享到:
相关推荐
If an inline function definition is short, with very little, if any, logic in it, you should put the code in your .h file. For example, accessors and mutators should certainly be inside a class ...
1. What is DevOps and where did it come from? 2. How does DevOps differ from Agile? 3. How does DevOps differ from ITIL or ITSM? 4. How does DevOps fit with VisibleOps? 5. What are the unpinning ...
If you are a software tester or a developer who has learnt the basics of Selenium using the WebDriver API and is now ready to take the next step, then this is the book for you. What You Will Learn ...
As a result, an explicit inverse of a pentadiagonal Toeplitz matrix is obtained under certain assumptions. And numerical experiments are given to show the effectiveness of our results.
基于显式相关模型的贝叶斯分类器融合_Bayesian Classifier Fusion with an Explicit Model of Correlation.pdf
and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions...
you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that ...
you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that ...
If your project is not in an existing workingcopy you are now asked where to store your project If some of the projects do not show glyphs Check if the projects are connected in File -> Subversion -> ...
An end-to-end TextSpotter with Explicit Alignment and Attention论文
前端开源库-is-explicit是显式的,以与对象和文本无缝工作的方式组合运算符实例和类型运算符。
For example, if T1 writes some object that is in T2’s input or output set, there is a dependency between T1 and T2. Bad Dependencies These include lost updates, dirty reads, non-repeatable reads, ...
And a complete region of disturbance parameters is solved to ensure the considered system with perturbations stability. Different from any other traditional algebraic methods,...
opencv-2.4.9/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(51): error: a storage class is not allowed in an explicit specialization 这是由于NCVPixelOperations.hpp的问题,只要替换opencv-2.4.9/...
seems that some servers can‘t deglut the info too fast, causing problems with sockets buffers and leading to crash the client machine, I don‘t know if is a Borland bug or Microsoft bug. (thanks to ...
The WebML web modelling language is one example that does not yet rely on an explicit meta-model in the sense of MDE. Instead, it is defined in terms of a document type definition (DTD), and ...
An explicit focus on understanding how the two tools fit together gives this book a unique insight on how to make SPAs for you and your clients that just work This book covers all the major topics you...
An Implicit-Explicit Runge--Kutta--Chebyshev Scheme for Diffusion-Reaction Equations用matlab编写的
Option Explicit Public txtSQL As String Dim mrc As adodb.Recordset '用户的读写权限标识 Dim mintRW As Integer '状态条中显示的时间信息 Public msBarText As String Public reportSQL As...
C++ explicit关键字在刚学的时候就接触到了,也从各处了解了一些,但始终云里雾里,在闲来无事的时候再仔细研究了一下,自己消化理解了一下,自认为比其他网友总结的精简而不失准确。