[英]How to draw Graphics2D on JPanel which is inside another JPanel?
[英]Use a Graphics2D extended class to draw on JPanel
我的问题是,是否可以使用从Graphics2D
扩展的我自己的类来绘制JPanel
?
我尝试了一下,但是因为面板的paint方法给了我Graphics2D
对象。 我不能将其转换为我的Graphics
类。 我想在该扩展类中编写自己的图形方法以绘制不同的内容,并将所有方法放在一个有序的位置使用它。 例如, Graphics2D.fillRect()
方法。
有什么办法可以做我想要的吗?
我不建议在类中扩展Graphics2D。
我建议您做的是将public void paintComponent(Graphics g)
,然后在其中调用Graphics2D:
Graphics2D g2d = (Graphics2D) g;
您可以将g重命名为任意名称。
这是使用我的'g'变量的示例:
Graphics2D g2d = (Graphics2D) g;
g.fillRect(0, 0, 100, 100);
查看所有评论,我找不到真正回答问题的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.