這是一個非常常用的東西
主要是重寫JPanel中的paint(Graphics g)方法實現的。
0 寫一個類繼承JPanel
public class WSModePlane extends JPanel
1 獲取緩衝區
首先需要獲得JPanel的緩衝區,這樣才能在JPanel中畫圖:
bfimage = (BufferedImage) this.createImage(this.getWidth(),this.getHeight());
2 實現圖像處理
而後我們用BufferImage提供的相應函數畫圖,或者是執行圖像處理算法
設置像素點顏色:
bfimage.setRGB(pln, pcol, 0xff0000)
獲取像素點顏色:
public int getRGB(int x,int y)
注:ARBG使用一個32位整數保存的,每個參數8位等等,通過API手冊可以查詢到相應函數
當然我們也可用Grahics 畫圖
Graphics bfg = bfimage.getGraphics();
比如畫直線
bfg.drawLine(x1,y1,x2,y2);
注意這裏實在緩衝區畫的,並沒有畫到Jpanel上面
3 傳入Jpanel
重寫Jpanel 的 paint函數,將緩衝區中的東西畫到Jpanel對應的Graphics上面就行啦
@Override
public void paint(Graphics g) {
g.drawImage(bfimage, 0, 0, null);
}
4 圖像變化
如果需要變化圖像,只要改動緩衝區 就是這裏的 bfimage,然後調用Jpanel的repaint()函數,就行。非常方便!
wsmodePlane.repaint();
看完後評價是美德~
可以加QQ 993830638討論 ,很高興能幫助你!