2011-11-23 101 views
0

我想用我的程序做的事情是,當我點擊圖像時,矩形會與JOptionPane一起出來。但是,JOptionPane是唯一彈出的東西。爲什麼我的paintComponent不工作?

我試着改變方法並添加更多的類,沒有任何工作>。 <任何人都可以解決我的問題嗎?這是我的代碼片段。

下面是我所說的filechooser,它允許我選擇我的照片。另外,一些其他的東西,如標籤在這裏。

public Help(){ 

     fc.setDialogTitle("Choose an image file to begin:"); 
     int returnval = fc.showOpenDialog(null); 
     if (returnval == JFileChooser.APPROVE_OPTION){ //when user selects a file, value returned will be JFileChooser.APPROVE_OPTION 
      File file = fc.getSelectedFile(); //the File value of the selection is returned from a call on getSelectedFile 
      try{ 
       image = ImageIO.read(file); //reads and loads File as image 
      } 
      catch (IOException e){} 
       System.out.println("You chose to open this file: " + file.getName()); 
     }else 
      System.out.println("No file selected."); 

     icon = new ImageIcon(image); 
     label = new JLabel(icon); 
     tagName = new JLabel(input); 

     label.addMouseListener(new ImagePanel()); 
     label.addMouseMotionListener(new ImagePanel()); 
     panel.add(tagName); 
    } 

最後,我的ImagePanel類,其中包含麻煩的paintComponent。此外,還有一些mouseListeners。

class ImagePanel extends JPanel implements MouseListener, MouseMotionListener{ 

     @Override 
     public void mouseClicked(MouseEvent event) { 
      // TODO Auto-generated method stub 

       x = event.getX(); 
       y = event.getY(); 

       input = JOptionPane.showInputDialog("Enter tag name"); 
       tagName.setText("You have tagged: " + input); 
       System.out.println(input); 
     } 

     @Override 
     public void mouseEntered(MouseEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void mouseExited(MouseEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void mousePressed(MouseEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void mouseReleased(MouseEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void paintComponent(Graphics g){ 
      super.paintComponent(g); 

       if(image != null && isRectPresent){ 
        g.setColor(Color.DARK_GRAY); 
        g.drawRect(x-50, y-50, 100, 100); 
       } 
     } 

     @Override 
     public void mouseDragged(MouseEvent e) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void mouseMoved(MouseEvent e) { 
      // TODO Auto-generated method stub 
     } 
    } 

您可以編譯代碼並親自查看。如果你知道該怎麼做,請指教我:)非常感謝!

+1

只需要注意:確保你確實通過添加了@ @ Override註解來覆蓋'paintComponent'。另外,不要*在'paintComponent'中做任何事情,比如添加監聽器等等.-) – aioobe

+0

無論如何,壓倒性的做什麼?大聲笑我沒有得到這一點xD(去表明我真的是小白)。感謝您的答覆。 – alicedimarco

+1

測試時儘量簡化問題:刪除所有不必要的方法,像'g.setColor()一樣製作非常簡單的'paintComponent'; g.drawRect()'無條件地。告訴我們結果 – pajton

回答

1

一個注意事項:一個較小的例子會早些回答。

指定的鼠標事件的X和Y在ImagePanel自定義字段,與其他的名字,如:

int mx; 
int my; 

其他的東西來試驗,離開了super.paintComponent方法。 而且也許你想使用g以下方法:

Graphics2D g2 = (Graphics2D)g; 

(分配給基類x和y是不是一個好主意,這樣的事情改變座標更好地利用的setBounds。)

0
try{ 
     image = ImageIO.read(file); //reads and loads File as image 
    } 
    catch (IOException e){} 

這裏的代碼說:「讓我們嘗試讀取圖像,如果失敗(拋出異常),則忽略該問題並繼續不使用圖像。」忽略問題總是很糟糕。我們至少可以打印出問題並繼續。

try{ 
     image = ImageIO.read(file); //reads and loads File as image 
    } 
    catch (IOException e){ 
    e.printStackTrace();//print the exception 
    } 

或打印問題並停止:

try{ 
     image = ImageIO.read(file); //reads and loads File as image 
    } 
    catch (IOException e){ 
    e.printStackTrace();//print the exception 
    System.exit(0);//stop executing 
    } 

實際的問題是最有可能在這裏:

if(image != null && isRectPresent){ 
    g.setColor(Color.DARK_GRAY); 
    g.drawRect(x-50, y-50, 100, 100); 
} 

我認爲問題是,如果條件是false(有沒有圖像(可能有讀取它的例外...?)和/或isRectPresent是false),因此它什麼都不做!在if處包含一個斷點,在調試模式下啓動程序,並在程序到達此點時檢查變量imageisRectPresent。 (如果它沒有到達那裏,你知道你有一個不同的問題。)祝你好運!

2

各種奇怪的東西:

label.addMouseListener(new ImagePanel()); 
label.addMouseMotionListener(new ImagePanel()); 

你不應該創建一個新JPanel只需一個監聽器添加到組件。你已經有了一個面板的實例。

addMouseMotionListener(this); 

不要在繪畫方法中向組件添加偵聽器。你永遠無法控制何時調用繪畫方法,並且最終會多次添加同一個監聽器。

+0

瞭解。我其實改變了我的代碼。感謝您的小費 :) – alicedimarco