2013-03-29 30 views
1

下面寫在BasicLinePix類中我嘗試創建一個程序,允許用戶在按住Shift鍵的同時按住鼠標按鍵並將其拖動到端點來繪製線。問題是,當鼠標被釋放時,該行消失。我試圖讓面板顯示該行,並且以相同的方式顯示多行。當鼠標釋放時,線程在繪圖程序中消失

我的代碼如下所示:

// this method overrides the paint method defined in JFrame 
    public void paint(Graphics g) { 
     super.paint(g); 


    } 


    // Inner class - instances of this class handle action events 
    private class EventHandler implements ActionListener, MouseListener, 
      MouseMotionListener { 

     private Point startPoint = null; // line's start point 
     private Point endPoint = null; // line's most recent end point 

     public void actionPerformed(ActionEvent arg0) { 
      if (arg0.getActionCommand().equals("Exit")) { 
       statusLabel.setText("Exiting program..."); 
       System.exit(0); 
      } 

     } 

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

     } 

     @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 e) { 

      if (e.isShiftDown()) { 

       // record starting point for line 
       startPoint = new Point(e.getX(), e.getY()); 

       // initialize endPoint 
       endPoint = startPoint; 
      } 

      if (e.isControlDown()) { 
       Graphics g = drawingPanel.getGraphics(); 
       g.drawString("Hello", e.getX(), e.getY()); 
      } 

     } 

     @Override 
     public void mouseReleased(MouseEvent arg0) { 
      //repaint the frame and its contents 
      //this executes the paint method defined above 
      repaint(); 
     } 

     @Override 
     public void mouseDragged(MouseEvent e) { 

      if (e.isShiftDown()) { 
       // Implement rubber-band cursor 
       Graphics g = drawingPanel.getGraphics(); 
       g.setColor(Color.black); 

       g.setXORMode(drawingPanel.getBackground()); 

       // REDRAW the line that was drawn 
       // most recently during this drag 
       // XOR mode means that yellow pixels turn black 
       // essentially erasing the existing line 
       drawLine(g, startPoint, endPoint); 

       // Update the end point of the line to current mouse position 
       endPoint = new Point(e.getX(), e.getY()); 

       // Draw line to current mouse position 
       // XOR mode: yellow pixels become black 
       // black pixels, like those from existing lines, temporarily 
       // become 
       // yellow 
       drawLine(g, startPoint, endPoint); 

      } 

     } 

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

     } 

     private void drawLine(Graphics g, Point start, Point end) { 
      if (startPoint != null && endPoint != null) { 
       int startX = ((Double) start.getX()).intValue(); 
       int startY = ((Double) start.getY()).intValue(); 

       int endX = ((Double) end.getX()).intValue(); 
       int endY = ((Double) end.getY()).intValue(); 

       g.drawLine(startX, startY, endX, endY); 
      } 
     } 

    } 

} 

任何幫助將不勝感激!

+1

請參閱[自定義繪畫方法](http://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/),以瞭解拖動鼠標時繪製矩形的示例。 – camickr

+1

如果您評論來自mouseReleased方法的repaint()調用 – slackmart

回答

6

不要使用組件的getGraphics()調用來獲取組件的圖形。在paintComponent(...)方法中繪製。您將在本網站和圖形教程中多次找到此建議。

問題是從組件上調用getGraphics()獲得的Graphics對象不是持久的Graphics對象,並且隨着下一次重繪而消失。

請注意,您發佈的代碼中有80%與您的問題無關,並且完全沒有必要且令人分心。

編輯:請注意,它看起來像你的程序可能受益於使用BufferedImage,繪製它,然後在JPanel的paintComponent方法中顯示BufferedImage。如果你確實走這條路線,那麼可以,只要你在完成處置後就可以通過getGraphics()得到BufferedImage的圖形對象。編輯2:儘管刪除不相關的代碼是一個好主意,但不要刪除太多讓代碼無法編譯和無法運行的代碼。看看,而不是創建一個sscce

+0

,那麼您會得到一個小小的進步謝謝我將使用該方法並瞭解它是如何工作的。我會編輯我的帖子以更相關。 –

+0

@WesJohnson:請參閱編輯和編輯2. –

+0

+1,它的更多次數,你會發現這四個建議。過去10年來,我一直在圍繞論壇提出這些建議。人們仍然在哪裏發現這個不好的建議? – camickr