2013-02-19 55 views
0

請看看下面的代碼的Java 2D API:空指針異常

package java2D; 

import java.awt.BasicStroke; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.*; 
import java.awt.*; 

public class Joins extends JFrame 
{ 
    private JPanel centerPanel; 

    public Joins() 
    { 
     this.setLayout(new BorderLayout()); 
     this.add(createCenterPanel(),"Center"); 

     this.setSize(200,250); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private JPanel createCenterPanel() 
    { 
     centerPanel = new JPanel(); 
     //centerPanel.setLayout(new FlowLayout()); 

     Graphics g = centerPanel.getGraphics(); 

     Graphics2D g2d = (Graphics2D)g; 

     BasicStroke bs1 = new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL); 
     g2d.setStroke(bs1); 
     g2d.drawRect(15, 15, 80, 50); 

     BasicStroke bs2 = new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); 
     g2d.setStroke(bs2); 
     g2d.drawRect(125, 15, 80, 50); 

     BasicStroke bs3 = new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 
     g2d.setStroke(bs3); 
     g2d.drawRect(235, 15, 80, 50); 


     return centerPanel; 
    } 

    public static void main(String[]args) 
    { 
     SwingUtilities.invokeLater(new Runnable(){ 

      @Override 
      public void run() { 
       new Joins(); 
      } 
     }); 
    } 
} 

此代碼生成NullPointerException異常。我需要繪製centerPanel中的圖形。當我開發Swing應用程序時,我通常將JFrame區域分成JPanel數量,而且我也是在這裏完成的,但最終發生了NullPointerException!

以下是錯誤

run: 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at java2D.Joins.createCenterPanel(Joins.java:34) 
    at java2D.Joins.<init>(Joins.java:17) 
    at java2D.Joins$1.run(Joins.java:55) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) 
    at java.awt.EventQueue.access$000(EventQueue.java:101) 
    at java.awt.EventQueue$3.run(EventQueue.java:666) 
    at java.awt.EventQueue$3.run(EventQueue.java:664) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 
BUILD SUCCESSFUL (total time: 6 seconds) 

請幫幫忙!

+0

您的堆棧跟蹤添加到這個問題吧。 – Perception 2013-02-19 19:54:53

+0

@Perception:對不起,我加了 – 2013-02-19 19:58:16

+0

哎呀! http://stackoverflow.com/questions/8356111/drawing-on-a-jpanel你真的應該考慮延長JPanel並通過繪製的paintComponent(特別是這個原因)。你正在尋找很多馬車行爲,如果你直接繪製它(重繪等...) – 2013-02-19 20:07:14

回答

2

正如已經指出的那樣,Graphics g = centerPanel.getGraphics();是你的問題的根源。

你不應該使用的getGraphics進行自定義繪圖。充其量getGraphics是最後的油漆週期的快照,在最壞的情況,好了,你已經看到了最壞的結果,這是null

進行自定義繪圖,你應該自己創建一個自定義組件(從像一個JPanel例如)並覆蓋其paintComponent方法(不要忘記調用super.paintComponet

看看Performing Custom Painting更多細節

+0

謝謝你的回覆。在那種情況下,我必須擴展JPanel的權利?嗯,不喜歡擺動:( – 2013-02-19 20:22:53

+0

是的,自定義繪畫必須在繪製重繪管理器的繪製週期內完成。請查看[在AWT和Swing中繪製](http://www.oracle.com/technetwork/java/painting- 140037.html)。鞦韆具有相當高的學習曲線,但一旦你瞭解它,這是一個,如果身邊最強大的UI工具箱中 - 恕我直言 – MadProgrammer 2013-02-19 20:48:29

+0

確定。謝謝 :) – 2013-02-19 20:54:25

1

的錯誤是在:

Graphics g = centerPanel.getGraphics(); 

據的javadocs:

爲組件創建一個圖形上下文。如果此組件當前不可顯示,此方法將返回null

也許你需要首先添加JPanelJoins幀,這樣以後就可以使用getGraphics()

+0

嗨,感謝您的答覆。但即使是現在,情況也是如此 – 2013-02-19 20:03:36

+1

您的評估是正確的,您的建議不是。我不應該使用的getGraphics進行自定義繪圖 – MadProgrammer 2013-02-19 20:07:42

-1

沒有什麼是可見的,因此的getGraphics()返回null。請嘗試:

centerPanel = new JPanel(); 
    JFrame frame = new JFrame(); 
    frame.add(centerPanel); 
    frame.setVisible(true); 
    centerPanel.setVisible(true); 
3

請勿使用getGraphics。創建一個新的JPanel,把你的油漆的功能集成到paintComponent方法記住調用super.paintComponent(g)

+0

+1打我給它 – MadProgrammer 2013-02-19 20:13:08