1

我的项目有问题,我的项目是画线(喜欢在窗口中绘画)。我想用 mouseDragged、mousePressed 和 mouseReleased 多画一条线。但是当我运行测试时,它显示了很多错误,这里是我的代码

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++;

    }

}

}

这是我的 void main

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);
}
}
4

1 回答 1

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。)

还有另一个问题:在您的releaseMouseandmouseDragged方法中,您将线的端点设置在 index 处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
    }
}
于 2013-10-06T09:15:55.367 回答