2014-02-28 33 views
1

我創建一個程序,您可以借鑑使用的MouseListener和的MouseMotionListener一個JPanel內矩形但是當我運行該程序,我得到由不輸入查詢任何參數來繪製矩形造成空指針異常畫。問題在於程序不允許用戶在屏幕上繪製矩形以給出矩形的參數。如何使用的MouseListener和的MouseMotionListener

至於現在,我只是想畫1個​​矩形,但最終該計劃將需要繪製多個矩形所以在這方面的任何幫助將是巨大的。

下面是代碼:

public class RectangleFrame extends JFrame implements ActionListener { 


JPanel buttonPanel; 
JButton saveImage; 
JButton clearImage; 
JCheckBox intersections; 
JCheckBox union; 
JPanel drawingArea; 

public RectangleFrame() 
{ 
    super(); 
    setTitle("Rectangles"); 
    setSize(600,600); 
    setResizable(false); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    buttonPanel = new JPanel(); 
    buttonPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
    this.add(buttonPanel, BorderLayout.SOUTH); 

    intersections = new JCheckBox("Draw Intersections"); 
    buttonPanel.add(intersections); 

    union = new JCheckBox("Draw Union"); 
    buttonPanel.add(union); 

    saveImage = new JButton("Save Image"); 
    saveImage.setMargin(new Insets(0,0,0,0)); 
    buttonPanel.add(saveImage); 

    clearImage = new JButton("Clear Image"); 
    clearImage.setMargin(new Insets(0,0,0,0)); 
    buttonPanel.add(clearImage); 

    drawingArea = new RectanglePanel(); 
    drawingArea.setBorder(BorderFactory.createLineBorder(Color.blue)); 
    this.add(drawingArea, BorderLayout.CENTER); 
    drawingArea.setVisible(true); 
    drawingArea.addMouseListener((MouseListener) drawingArea); 
    drawingArea.addMouseMotionListener((MouseMotionListener) drawingArea); 
} 

    @Override 
    public void actionPerformed(ActionEvent arg0) 
    { 
     // TODO Auto-generated method stub 

    } 





} 

class RectanglePanel extends JPanel implements MouseListener, MouseMotionListener { 


Rectangle rectangle; 
int x2, y2; 

public RectanglePanel() 
{ 
    super(); 
} 

@Override 
public void mouseDragged(MouseEvent arg0) 
{ 

    // TODO Auto-generated method stub 
} 



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

} 



@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 arg0) 
{ 
    rectangle.setX(arg0.getX()); 
    rectangle.setY(arg0.getY()); 

    repaint(); 

} 



@Override 
public void mouseReleased(MouseEvent arg0) 
{ 
    x2 = arg0.getX(); 
    y2 = arg0.getY(); 

    rectangle.setWidth(Math.abs(rectangle.getX() - x2)); 
    rectangle.setHeight(Math.abs(rectangle.getY() - y2)); 

    repaint(); 
} 



@Override 
public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    g.setColor(Color.BLUE); 
    g.fillRect(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight()); 
} 

} 

回答

3

您需要創建的mousePressed上一個新Rectangle對象,然後將其分配給矩形變量。然後你可以在mouseDragged方法中改變它的狀態。

或者更好的,使用的鼠標事件設置Point對象:

// variable declarations 
Point initialPoint = null; 
Rectangle rectangle = null; 

@Override 
public void mousePressed(MouseEvent mEvt) { 
    initialPoint = mEvt.getPoint(); 
    rectangle = null; 
    repaint(); 
} 

mouseDragged(MouseEvent mEvt) { 
    // use initialPoint, mEvt.getPoint(), 
    // Math.abs(...), Math.min(...), and Math.max(...) 
    // to calculate x, y, w, and h 
    rectangle = new Rectangle(x, y, w, h); 
    repaint(); 
} 

還在的paintComponent,只繪製矩形,如果它不是空。

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if (rectangle != null) { 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setColor(Color.BLUE); 
    g2.fill(rectangle); 
    } 
} 

至於,

至於現在,我只是想抽1個長方形,但最終該計劃將需要繪製多個矩形所以在這方面的任何幫助將很棒。

這很容易。創建一個ArrayList<Rectangle>,並在mouseReleased上將創建的Rectangle對象放入列表中。在paintComponent方法中,使用for循環遍歷列表,繪製它包含的每個Rectangle。

相關問題