2013-11-02 153 views
-1

在MouseDragged函數中獲取空指針異常。一直試圖找出是什麼原因導致它,但我不知道是什麼。任何幫助,將不勝感激。我的程序應該添加一個圖像文件並能夠在其上繪製。獲取空指針異常

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 
import javax.imageio.*; 
import java.awt.image.BufferedImage; 
import javax.swing.colorchooser.ColorSelectionModel; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 
import java.awt.event.AdjustmentEvent; 
import java.awt.event.AdjustmentListener; 
import java.awt.geom.*; 
import java.util.Vector; 
import java.util.*; 
import java.awt.Point; 
import java.awt.Stroke; 
import java.awt.Toolkit; 
public class Lab3 { 


public static void main(String[] args) 
    { 

    new Lab3Frame(); 

} 


} 
class Lab3Frame extends JFrame implements ActionListener, MouseListener,  MouseMotionListener, ChangeListener 
{ 

JFrame frame; 
JPanel toolbar; 
JMenuBar menuBar; 
JMenu tool, file; 
JLabel image; 
JToggleButton b1, b2, b3, b4, bcol; 

JMenuItem pencil, eraser, brush, line, open; 
final JFileChooser fc = new JFileChooser(); 
BufferedImage img; 
JColorChooser tcc; 
    JScrollBar hbar, vbar; 


    PaintPanel inkPanel; 
    private Point[] stroke; 
public Color ink_color = Color.black; 
public Stroke ink_stroke = new BasicStroke(5.0f, 
     BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 
    final int MAX_SAMPLES = 500; 
    private int sampleCount; 
//private Vector<Line2D.Double> v; 

Lab3Frame(){ 
    frame = new JFrame(); 
    menuBar = new JMenuBar(); 
    image = new JLabel(); 
    tool = new JMenu("Tools"); 
    file = new JMenu("File"); 
    pencil = new JMenuItem("Pencil"); 
    eraser = new JMenuItem("Eraser"); 
    brush = new JMenuItem("Brush"); 
    line = new JMenuItem("Line"); 
    tool.add(pencil); 
    tool.add(line); 
    tool.add(eraser); 
    tool.add(brush); 
    open = new JMenuItem("Open"); 
    file.add(open); 
    menuBar.add(file); 
    menuBar.add(tool); 
    frame.setJMenuBar(menuBar); 
    tcc = new JColorChooser(); 

    inkPanel = new PaintPanel(); 
    toolbar = new JPanel(); 
    toolbar.setSize(100,300); 

    image.setBackground(Color.blue); 
    //TOOLBAR BUTTONS 
    toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.Y_AXIS)); 
     b1 = new JToggleButton("1"); 
    toolbar.add(b1); 
    b2 = new JToggleButton("2"); 
    toolbar.add(b2); 
    b3 = new JToggleButton("3"); 
    toolbar.add(b3); 
    b4 = new JToggleButton("4"); 
    toolbar.add(b4); 
    bcol = new JToggleButton(" "); 
    toolbar.add(bcol); 
    tcc.setPreviewPanel(new JPanel()); 

    //LISTENERS 
    pencil.addActionListener(this); 
    open.addActionListener(this); 
    image.addMouseMotionListener(this); 
    pencil.addMouseMotionListener(this); 
    tcc.getSelectionModel().addChangeListener(this); 
    inkPanel.addMouseMotionListener(this); 
    inkPanel.addMouseListener(this); 
    //hbar.addAdjustmentListener(this); 
    //vbar.addAdjustmentListener(this); 
    //tcc.addChangeListener(this); 
    //ADD ELEMENTS TO FRAME 
    frame.setSize(600, 600); 
    hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300); 
    vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300); 

    frame.setLayout(new BorderLayout()); 
    image.setLayout(new BorderLayout()); 

    image.setBackground(Color.white); 
    frame.add(toolbar, BorderLayout.WEST); 
    frame.add(tcc, BorderLayout.SOUTH); 
    frame.add(image, BorderLayout.CENTER); 
    frame.setGlassPane(inkPanel); 
    frame.setVisible(true); 
} 




public void actionPerformed(ActionEvent e) { 
if (e.getSource() == open){ 
int returnVal = fc.showOpenDialog(Lab3Frame.this); 

if (returnVal == JFileChooser.APPROVE_OPTION) { 
      File file = fc.getSelectedFile(); 
      try { 
        // frame.add(inkPanel); 
        img=ImageIO.read(file); 
        ImageIcon icon=new ImageIcon(img); // ADDED 
        image.setIcon(icon); // ADDED 

        Dimension imageSize = new Dimension(icon.getIconWidth(),icon.getIconHeight()); // ADDED 
        image.setPreferredSize(imageSize); // ADDED 
        image.setVisible(true); 
        // 
        //image.add(hbar, BorderLayout.SOUTH); 
        //image.add(vbar, BorderLayout.EAST); 
        //image.revalidate(); // ADDED 
        //image.repaint(); // ADDED 

       } 
       catch(IOException e1) { 
       e1.printStackTrace(); 
       } 
      } 
} 

} 

    public void stateChanged(ChangeEvent changeEvent) { 
    Color customCol = tcc.getColor(); 
    bcol.setBackground(customCol); 
    } 

/* 
AdjustmentListener adjustmentListener = new AdjustmentListener(){ 

public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) { 
    System.out.println("Adjusted: " + adjustmentEvent.getValue()); 
    } 
}; 
*/ 
    public void mouseClicked(MouseEvent me) { 
    int x = me.getX(); 
int y = me.getY(); 

    if (SwingUtilities.isLeftMouseButton(me)) 
     { 
     stroke[sampleCount] = new Point(x, y); 
     int x1 = (int)stroke[sampleCount - 1].getX(); 
     int y1 = (int)stroke[sampleCount - 1].getY(); 
     int x2 = (int)stroke[sampleCount].getX(); 
     int y2 = (int)stroke[sampleCount].getY(); 
     if (sampleCount < MAX_SAMPLES - 1) 
      ++sampleCount; 

     // draw ink trail from previous point to current point 
     inkPanel.drawInk(x1, y1, x2, y2); 
     } 
    } 
    public void mouseEntered(MouseEvent me) {} 
    public void mouseExited(MouseEvent me) {} 
     public void mousePressed(MouseEvent me) { 
    int x = me.getX(); 
    int y = me.getY(); 

    stroke[sampleCount] = new Point(x, y); 
     if (sampleCount < MAX_SAMPLES - 1) 
     ++sampleCount; 
} 

    public void mouseDragged(MouseEvent me) { 

sampleCount = 1; 
int x = me.getX(); 
    int y = me.getY(); 
System.out.println("y2:" + y); 
if (SwingUtilities.isLeftMouseButton(me)) 
    { 
// try{ 
    stroke[sampleCount] = new Point(x, y); 
    int x2 = (int)stroke[sampleCount].getX(); 
    int y2 = (int)stroke[sampleCount].getY(); 
    int x1 = (int)stroke[sampleCount - 1].getX(); 
    int y1 = (int)stroke[sampleCount - 1].getY(); 

    if (sampleCount < MAX_SAMPLES - 1){ 
     ++sampleCount;} 
     //draw.Line2D. 
     inkPanel.drawInk(x1, y1, x2, y2); 
     //inkPanel.repaint(); 

     } 
    /*catch (Exception err1) { 
     System.out.println(err1.getMessage()); 
    }*/ 


    // draw ink trail from previous point to current point 

// } 
} 

public void mouseReleased(MouseEvent me) { 
} 

public void mouseMoved(MouseEvent me) { 
} 





class PaintPanel extends JPanel 
{ 

private Vector<Line2D.Double> v; 
private final Stroke INK_STROKE = new BasicStroke(5.0f, 
     BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 

PaintPanel() 
{ 
    v = new Vector<Line2D.Double>(); 

this.setBackground(Color.white); 

} 
@Override 
    public void paintComponent(Graphics gd) { 

    super.paintComponent(gd); 
paintImage(gd); 



} 


public void paintImage(Graphics gd){ 
    Graphics2D g2 = (Graphics2D)gd; 
    //Rectangle aa = new Rectangle(10, 10, 100, 100); 
    //g2.draw(aa); 
    g2.setColor(Color.red); 
    Stroke s = g2.getStroke(); // save current stroke 
    g2.setStroke(INK_STROKE); 

    for (int i = 0; i < v.size(); ++i) 
    g2.draw((Line2D.Double)v.elementAt(i)); 

    g2.setStroke(s); 
    repaint(); 
} 

public void drawInk(int x1, int y1, int x2, int y2) 
    { 
    // get graphics context 
    Graphics2D g2 = (Graphics2D)this.getGraphics(); 
    //System.out.println("dn " + g2 + "method was called"); 
    // create the line 
    Line2D.Double inkSegment = new Line2D.Double(x1, y1, x2, y2); 

    g2.setColor(Color.red); // set the inking color 
    Stroke s = g2.getStroke(); // save current stroke 
    g2.setStroke(INK_STROKE); // set desired stroke 
    g2.draw(inkSegment);  // draw it! 
    g2.setStroke(s);   // restore stroke 
    v.add(inkSegment);   // add to vector 
    repaint(); 
    } 
} 

} 
+1

你不能運行一個調試器,它會告訴你它在哪一行嗎? – bitwise

+1

我有一個預感,你應該仔細看看sampleCount和stroke變量 - 它們的初始化遍佈整個地方,有時還未定義(例如:你訪問'mouseClicked'中的sampleCount,而它沒有值,I沒有看到你定義中風的大小,但你在'mouseDragged'中訪問它的第一個元素(我找不到初始化的)) – UnholySheep

回答

1
stroke[sampleCount] 

我沒有看到VAR stroke來分配,所以這可能是你的空指針異常。

+0

我初始化了它,現在它給出了一個超出界限的數組錯誤 – user2934944

+1

然後你沒有啓動它正確。它是一個數組,所以你必須確保這些inizies在這個範圍內。 – Devolus

0

您嘗試使用stroke但您從未初始化它。用新陣列初始化!