2013-10-06 132 views
1

我的項目出現問題,我的項目是繪製線條(喜歡在Windows中繪製)。我想用mouseDragged,mousePressed和mouseReleased繪製更多的一行。但是,當我運行測試,它表現出了很多的錯誤,在這裏我的代碼在java中使用mouseevent繪製線條

package image; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Point; 
import java.awt.Graphics; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class paint extends JFrame{ 
private Point points[] = new Point[10000]; 
private Point pointends[] = new Point[10000]; 
private int pointCount = 0; 
public paint() 
{ 
    panel paint2 = new panel(); 
    add(paint2,BorderLayout.CENTER); 
} 
private class panel extends JPanel 
{ 

    public panel() 
    { 
     setBackground(Color.BLUE); 
     MouseHandler handler = new MouseHandler(); 
     this.addMouseMotionListener(handler); 

     this.addMouseListener(handler); 
    } 
    @Override 
    protected void paintComponent(Graphics g) 
    { 
     // TODO Auto-generated method stub 
     super.paintComponent(g); 
     for(int i = 0;i < pointCount;i++) 
     { 
      g.setColor(Color.RED); 
      g.drawLine(points[pointCount].x, points[pointCount].y, pointends[pointCount].x, pointends[pointCount].y); 
     }   
    } 
} 

private class MouseHandler extends MouseAdapter 
{ 
    @Override 
    public void mouseDragged(MouseEvent e) 
    { 
     // TODO Auto-generated method stub 
      pointends[ pointCount ] = e.getPoint(); 
      repaint(); 


    } 
    @Override 
    public void mousePressed(MouseEvent e) { 
     // TODO Auto-generated method stub 
     super.mousePressed(e); 
     if(pointCount < points.length) 
     { 
      points[ pointCount ] = e.getPoint(); 
     } 
    } 
    @Override 
    public void mouseReleased(MouseEvent e) { 
     // TODO Auto-generated method stub 
     super.mouseReleased(e); 
     pointends[pointCount]=e.getPoint(); 
     repaint(); 
     pointCount++; 

    } 

} 

}

,這裏是我的無效的主要

package image; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.BorderLayout; 
import java.awt.BorderLayout; 

public class test 
{ 

public static void main(String[] args) { 

paint paint1 = new paint(); 
/*paintP.add(paint1, BorderLayout.CENTER); 
paintP.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
paintP.setSize(400,400); 
paintP.setVisible(true);*/ 
paint1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
paint1.setSize(400,400); 
paint1.setVisible(true); 
} 
} 
+2

**很多的錯誤**?編譯時間?運行 ?請告訴我們錯誤! –

+1

[Java編碼標準§9](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367)建議類名稱應以大寫字母開頭。 –

回答

1

在你paintComponent方法,改線

g.drawLine(points[pointCount].x, points[pointCount].y, pointends[pointCount].x, pointends[pointCount].y); 

對此:

g.drawLine(points[i].x, points[i].y, pointends[i].x, pointends[i].y); 

這將擺脫NullPointerException,一旦鬆開鼠標按鈕,線條就會被正確繪製。 (之前,你不只是想畫在同一行中循環的每次迭代,也認爲還不存在一條線,從而NullPointerException異常。)

還有另外一個問題:在你releaseMousemouseDragged方法,您正在爲索引pointCount設置該行的結束點,但您只能繪製到pointCount - 1。開始繪製線條時,必須增加pointCount計數器,否則僅在釋放鼠標時繪製新線。解決這一問題將是一個辦法,你的鼠標監聽改成這樣:

private class MouseHandler extends MouseAdapter { 
    public void mouseDragged(MouseEvent e) { 
     pointends[ pointCount - 1 ] = e.getPoint(); // note the "- 1" 
     repaint(); 
    } 
    public void mousePressed(MouseEvent e) { 
     if(pointCount < points.length) { 
      points[ pointCount ] = e.getPoint(); 
      pointends[ pointCount ] = e.getPoint(); // add end point 
      pointCount++; 
      repaint(); 
     } 
    } 
    public void mouseReleased(MouseEvent e) { // do nothing 
    } 
} 
+0

噢謝謝@@,但是當我拖着鼠標,它不畫線,當我釋放鼠標時畫了一條線 –

+0

非常感謝你:) –

0

你可以試試這個:

public class myDrawLine extends JPanel { 

private static final long serialVersionUID = 1L; 

// These ArrayList will save all Points of Pressed and Released 
ArrayList<Point> pointStart = new ArrayList<Point>(); 
ArrayList<Point> pointEnd = new ArrayList<Point>(); 

// These single Points will save the point of Dragged 
Point startSinglePoint = new Point(); 
Point endSinglePoint = new Point(); 

public void paint(Graphics g) { 

    super.paint(g); 

    g.drawLine(startSinglePoint.x, startSinglePoint.y, endSinglePoint.x, 
      endSinglePoint.y); 

    for (int i = 0; i < pointStart.size() && i < pointEnd.size(); i++) { 
     g.drawLine(pointStart.get(i).x, pointStart.get(i).y, 
       pointEnd.get(i).x, pointEnd.get(i).y); 
    }// end for 
}// end paint 

{// start Block of Listeners 
    addMouseListener(new MouseAdapter() { 

     public void mousePressed(MouseEvent e) { 
      startSinglePoint = e.getPoint(); // used to the draw line when 
               // you drag 
      pointStart.add(e.getPoint()); // used to save all drew lines 
     }// end mousePressed 

     public void mouseReleased(MouseEvent e) { 
      pointEnd.add(e.getPoint()); // used to save all drew lines 
      repaint(); 
     }// end mouseReleased 
    });// end addMouseListener 

    addMouseMotionListener(new MouseAdapter() { 

     public void mouseDragged(MouseEvent e) { 
      endSinglePoint = e.getPoint(); // used to draw the line when you 
              // drag 
      repaint(); 
     }// end mouseDragged 
    });// end addMouseMotionListener 

}// end Block of Listeners 
}// end Class 

和主要方法:

public static void main(String[] args){ 
    JFrame frame = new JFrame("Draw Line"); 
    frame.setSize(300, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

    myDrawLine draw = new myDrawLine(); 
    frame.getContentPane().add(draw); 

}//end main