Graphics在java的哪个包_如何在Java中成功扩展Graphics
我正在嘗試創(chuàng)建一個通用的圖形導(dǎo)出工具,它通過實現(xiàn)Graphics接口來工作,并攔截和解釋對其各種方法的調(diào)用.但是,雖然我可以成功地為單個組件執(zhí)行此操作,但當我在包含其他組件的組件上使用它時,我的Graphics類正在被替換.奇怪的是,這只發(fā)生在
Windows和
Linux上.在OSX上它工作正常.
任何人都可以建議我如何確保它是我的原始Graphics類傳遞給所有子組件?
我有一個簡短的腳本,演示了根本問題.當我使用MyGraphics的實例顯式調(diào)用paint時,我沒有在JPanel中看到MyGraphics – 只是sun.java2d.SunGraphics2D.
import java.awt.*;
import java.awt.image.*;
import java.text.AttributedCharacterIterator;
import javax.swing.*;
public class GraphicsTest {
public static void main(String[] args) {
new GraphicsTest();
}
public GraphicsTest () {
JFrame frame = new JFrame();
frame.getContentPane().add(new MyPanel());
frame.setSize(500,500);
frame.setVisible(true);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
System.out.println("Using my graphics - expect to see 'MyGraphics' next");
frame.paint(new MyGraphics());
}
class MyPanel extends JPanel {
public void paint (Graphics g) {
super.paint(g);
System.out.println("Graphics is "+g);
g.fillRect(10, 10, 20, 20);
}
}
class MyGraphics extends Graphics {
public String toString () {
return "MyGraphics";
}
public Graphics create() {
return this;
}
// I've left out the huge list of abstract methods from the original script
// since they're unchanged from the defaults and don't really matter here.
}
在我的Windows系統(tǒng)上,輸出是:
Graphics is sun.java2d.SunGraphics2D[font=javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12],color=sun.swing.PrintColorUIResource[r=51,g=51,b=51]]
Using my graphics - expect to see 'MyGraphics' next
Graphics is sun.java2d.SunGraphics2D[font=javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12],color=sun.swing.PrintColorUIResource[r=51,g=51,b=51]]
而在OSX下,我得到了我的預(yù)期,即:
Graphics is sun.java2d.SunGraphics2D[font=apple.laf.CUIAquaFonts$DerivedUIResourceFont[family=LucidaGrande,name=LucidaGrande,style=plain,size=13],color=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]]
Using my graphics - expect to see 'MyGraphics' next
Graphics is MyGraphics
那么我能做些什么來確保在所有情況下我都將’MyGraphics’傳遞給相應(yīng)的子組件?
總結(jié)
以上是生活随笔為你收集整理的Graphics在java的哪个包_如何在Java中成功扩展Graphics的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习Python解决高等数学问题(sym
- 下一篇: 恋与制作人ios无限钻石