使用鼠标事件的徒手画线

时间:2011-05-18 04:20:46

标签: java swing

我可以知道是否有任何绘制线的公式?目前我在java中实现了一个写意画线,但是下面的代码在绘制时并不是我所期待的。 我试过g.drawLine(arg0.getX(),arg0.getY(),arg0.getX(),arg0.getY());但是,绘制的线不是连续的,而是绘制点,我读到这是因为鼠标拖动是间隔发生的,如果是这样,我应该如何记录点?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

public class STDrawingArea extends JPanel implements MouseListener, MouseMotionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    int xPressed,yPressed;
    int xReleased,yReleased;
    int xDragged,yDragged;
    public STDrawingArea()
    {
        setPreferredSize(new Dimension(1280, 700));
        setBounds(0, 0, 1280, 700);
        setBackground(Color.WHITE);
        addMouseListener(this);
        addMouseMotionListener(this);

    }



    @Override
    public void mouseDragged(MouseEvent arg0) {
        Graphics g = getGraphics();
        xDragged = xPressed;
        yDragged = yPressed;
        g.drawLine(xPressed, yPressed, arg0.getX(), arg0.getY());
        xDragged = arg0.getX();
        yDragged = arg0.getY();
    }

    @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) {
        // TODO Auto-generated method stub
        xPressed = arg0.getX();
        yPressed = arg0.getY();
        System.out.println("xPressed: "+xPressed+" ,yPressed: "+yPressed);
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
        xReleased = arg0.getX();
        yReleased = arg0.getY();
        System.out.println("xReleased: "+xPressed+" ,yReleased: "+yPressed);
    }


}

3 个答案:

答案 0 :(得分:4)

执行此操作的简单方法可能是:

  1. 在您的组件中保留List Point

  2. 覆盖JPanel

    paintComponent()方法
    • 迭代点列表中的所有点
    • 在每对点之间画线(当然除了第一个和最后一个)

  3. 例如,您可以进行以下更改:

    private ArrayList<Point> points = new ArrayList<Point>();
    
    //...
    
    public void mouseDragged(MouseEvent arg0) {
        points.add(arg0.getPoint());
        repaint(); //request Swing to refresh display as soon as it can
    }
    
    //...
    
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
    
        for (int i = 0; i < points.size() - 2; i++)
        {
            Point p1 = points.get(i);
            Point p2 = points.get(i + 1);
    
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
    }
    

答案 1 :(得分:1)

需要进行一些细微的更改

 @Override
public void mouseDragged(MouseEvent arg0) {
    Graphics g = getGraphics();

    g.drawLine(xDragged, yDragged, arg0.getX(), arg0.getY());
    xDragged = arg0.getX();
    yDragged = arg0.getY();
}

 @Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
    xPressed = arg0.getX();
    yPressed = arg0.getY();
    System.out.println("xPressed: "+xPressed+" ,yPressed: "+yPressed);
    xDragged=xPressed;
    yDragged=yPressed;
}

我编译并执行了这段代码,发现它完全正常工作..: - )

答案 2 :(得分:1)

试试这个。它正在工作。没有必要使用Point类并迭代&#39; for&#39;循环,如果你想简单地做一个写意画。

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    class STDrawingArea extends JPanel implements MouseListener,MouseMotionListener,ActionListener
    {
        int xPressed,yPressed;
        int xReleased,yReleased;
        int xDragged,yDragged;
        private JButton clear;
        public STDrawingArea()
        {
            setPreferredSize(new Dimension(1200, 500));
            setBounds(0, 0, 480, 500);
            //setBackground(Color.YELLOW);
            clear=new JButton("CLEAR");
            add(clear);
            clear.setBounds(540, 5, 100, 25);
            clear.addActionListener(this);
            addMouseListener(this);
            addMouseMotionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) 
        {
             if(e.getSource()==clear)
            {
                setOpaque(false);
                repaint();
            }
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            g.drawLine(xPressed,yPressed,xDragged,yDragged);
            xPressed=xDragged;
            yPressed=yDragged;
        }

        @Override
        public void mouseDragged(MouseEvent arg0) {
            Graphics g = getGraphics();
            g.drawLine(xPressed, yPressed, arg0.getX(), arg0.getY());
            xDragged = arg0.getX();
            yDragged = arg0.getY();
            repaint();
        }

        @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) {
            // TODO Auto-generated method stub
            setOpaque(true);
            xPressed = arg0.getX();
            yPressed = arg0.getY();
        }

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

        }
    }
    class Frame4 extends JFrame
    {
        private STDrawingArea da;
        public Frame4()
        {
            da=new STDrawingArea();
            da.setBackground(Color.YELLOW);
            da.setLayout(new BorderLayout());
            add(da);
        }
    }
    public class FreeHandDrwing 
    {
        public static void main(String s[])
        {
            Frame4 ob=new Frame4();

            ob.setVisible(true);
            ob.setBounds(100, 100, 1200, 500);
            ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }