简体   繁体   English

用java中的mouseevent画线

[英]Draw Lines with mouseevent in java

I have a problem with my project, my project is draw lines (likes paint in windows). 我的项目有问题,我的项目是绘制线条(例如在Windows中绘制)。 I want to draw more one line with mouseDragged,mousePressed and mouseReleased. 我想用mouseDragged,mousePressed和mouseReleased画多条线。 But when I run to test, it showed a lot of errors, here my code 但是当我运行测试时,它显示了很多错误,这是我的代码

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

    }

}

} }

and here's my 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);
}
}

In your paintComponent method, change the line paintComponent方法中,更改行

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

to this: 对此:

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

This will get rid of the NullPointerException and the lines will be drawn correctly once you release the mouse button. 这将摆脱NullPointerException ,一旦释放鼠标按钮,线条将正确绘制。 (Before, you were not only trying to paint the same line in each iteration of the loop, but also a line that did not exist yet, thus the NullPointerException.) (之前,您不仅试图在循环的每次迭代中绘制同一行,而且还尝试绘制一条尚不存在的行,从而绘制NullPointerException。)

There's another problem: In your releaseMouse and mouseDragged methods, you are setting the end points for the line at index pointCount , but you are drawing only up to pointCount - 1 . 还有另一个问题:在releaseMousemouseDragged方法中,您将在mouseDragged处设置行的pointCount ,但是最多绘制到pointCount - 1 You have to increment the pointCount counter when you start drawing the lines, otherwise the new line will only be drawn when the mouse is released. 开始绘制线条时,您必须增加pointCount计数器,否则仅当释放鼠标时才绘制新的线条。 One way to fix this would be to change your mouse listener to this: 解决此问题的一种方法是将鼠标侦听器更改为此:

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
    }
}

You may try this: 您可以尝试以下方法:

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

and the main method: 和主要方法:

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM