日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Graphics在java的哪个包_如何在Java中成功扩展Graphics

發(fā)布時間:2023/12/15 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Graphics在java的哪个包_如何在Java中成功扩展Graphics 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我正在嘗試創(chuàng)建一個通用的圖形導(dǎo)出工具,它通過實現(xiàn)Graphics接口來工作,并攔截和解釋對其各種方法的調(diào)用.但是,雖然我可以成功地為單個組件執(zhí)行此操作,但當(dāng)我在包含其他組件的組件上使用它時,我的Graphics類正在被替換.奇怪的是,這只發(fā)生在

Windows和

Linux上.在OSX上它工作正常.

任何人都可以建議我如何確保它是我的原始Graphics類傳遞給所有子組件?

我有一個簡短的腳本,演示了根本問題.當(dāng)我使用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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。