锘??xml version="1.0" encoding="utf-8" standalone="yes"?> java.awt.Graphics 鏄竴涓娊璞$被錛屽叾浣滅敤鏄畾涔変竴涓湡姝g殑宸ュ叿錛岀敤鏉ユ帴鍙楀浘褰㈡搷浣溿?/strong> 琛ㄤ竴錛氫紶閫掍竴涓 Graphics 鐨勫紩鐢ㄧ殑 JDK 鏂規硶
* @param xscale 鍥懼儚 x 杞達紙瀹藉害錛変笂鐨勭殑緙╂斁姣斾緥銆?
* @param yscale 鍥懼儚 y 杞達紙楂樺害錛変笂鐨勭殑緙╂斁姣斾緥銆?
* @param hints 閲嶆柊緇樺浘浣跨敤鐨?RenderingHints 瀵硅薄銆?
* @return 緙╂斁鍚庣殑鍥懼儚瀵硅薄 闃呰鍏ㄦ枃
]]>
]]>
]]>
java.awt
Canvas
paint(Graphics g)
Component
paint(Graphics g)
Component
paintAll(Graphics g)
Component
print(Graphics g)
Component
printAll(Graphics g)
Component
update(Graphics g)
Container
paint(Graphics g)
Container
paintComponents(Graphics g)
Container
print(Graphics g)
Container
printComponents(Graphics g)
ScrollPane
printComponents(Graphics g)
java.beans
Property-Editor
paintValue(Graphics g, Rectangle r)
Property-EditorSupport
paintValue(Graphics g, Rectangle r)
琛ㄤ簩錛氳繑鍥?Graphics 寮曠敤鐨?JDK 鏂規硶
java.awt | Component | getGraphics() |
Image | getGraphics() | |
PrintJob | getGraphics() | |
Graphics | create() | |
Graphics | create(intx, int y, int w, int h) |
Graphics 綾誨飽琛?涓富瑕佺殑鑱岃矗錛?/strong>
· 璁劇疆鍜岃幏鍙栧浘褰㈠弬鏁般?/p>
· 鍦ㄨ緭鍑鴻澶囦腑鎵ц鍥懼艦鎿嶄綔銆?/p>
寰楀埌鏋勪歡鐨?Graphics 鐨勫紩鐢ㄦ湁2縐嶆柟娉曪細
· 閲嶈澆 琛ㄤ竴 涓殑鏂規硶錛堜紶閫?Graphics 鐨勫紩鐢級
· 璋冪敤 琛ㄤ簩 涓殑鏂規硶錛堣繑鍥?Graphics 鐨勫壇鏈級
Graphics 瀵硅薄鐨勫鍛?/strong>
闄や簡浣跨敤 琛ㄤ簩 鐨勬柟娉曞緱鍒扮殑 Graphics 鐨勫壇鏈錛屼嬌鐢?琛ㄤ竴 鐨勬柟娉曞緱鍒扮殑 Graphics 鐨勫紩鐢ㄥ彧鏈夊湪鏂規硶鐨勬墽琛岃繃紼嬩腑鎵嶆湁鏁堬紙渚嬪閲嶈澆鐨?paint() 鍜?update() 絳夋柟娉曪級銆備竴鏃︽柟娉曡繑鍥烇紝寮曠敤灝嗕笉鍐嶆湁鏁堛?/p>
閫氳繃浣跨敤 琛ㄤ簩 鐨勬柟娉曞緱鍒扮殑 Graphics 鐨勫璞′嬌鐢ㄥ畬鍚庨渶瑕佽皟鐢?Graphics.dispose() 鏂規硶澶勭悊銆?/p>
// 紼嬪簭鐗囨柇
public void someMethodInAComponent(){
Graphics g = getGraphics();
if(g != null){
try{
// ...
// ...
}
finally{
g.dispose();
}
}
}
Graphics 綾昏繕鎻愪緵2涓柟娉曞垱寤?Graphics 瀵硅薄錛?/strong>
· Graphics create()
鍒涘緩綺劇‘鐨?Graphics 鍓湰銆?/p>
· Graphics create(int x, int y, int w, int h)
鍒涘緩涓涓壇鏈紝浣嗘槸錛屽彉鍏冩寚瀹氫竴涓鉤縐婚噺 (x, y) 鍜屼竴涓柊鐨勫壀璐寸煩褰?(x, y, w, h)銆俢reate(int, int, int, int) 榪斿洖鐨?Graphics 鐨勫師鐐硅杞崲鎴?(x, y) 鐨勫潗鏍囷紝浣嗘槸鍓創鐭╁艦杞崲涓哄師鍓創鐭╁艦鍜屾寚瀹氱煩褰㈢殑浜ら泦銆?/p>
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class CreateTest extends Applet{
private Image image;
public void init(){
image = getImage(getCodeBase(),"lena.jpg");
try{
MediaTracker mt = new MediaTracker(this);
mt.addImage(image,0);
mt.waitForID(0);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
public void paint(Graphics g){
Graphics copy = g.create(image.getWidth(this),0,image.getWidth(this),image.getHeight(this));
try{
System.out.println("g: " + g.getClip().toString());
System.out.println("copy: " + copy.getClip().toString());
g.drawImage(image,0,0,this);
copy.drawImage(image,0,0,this);
}
finally{
copy.dispose();
}
}
}