我的程序中mousePressed和mouseDragged有什么区别?

时间:2011-02-14 04:16:36

标签: java applet

在像Microsoft的PaintTM这样的绘图程序中,可用的功能之一是绘制直线。您   可以通过按下鼠标指示直线,以指示线应该从哪里开始。然后,用   鼠标仍然按下,你可以移动鼠标(鼠标拖动),行的终点是   在你移动时创建。释放鼠标时,线条仍然存在。您可以多次重复此过程   次,在PaintTM文档中创建许多不同的行。

如果您希望在java程序中模拟此效果,可以使用MouseListener(mousePressed   方法)和MouseMotionListener(mouseDragged方法),以创建线段。另外,我们   我希望能够通过顶部的“清除按钮”清除油漆区域。另外,我们想改变   通过在顶部放置一些“颜色按钮”来显示所有线条的颜色。为了完成所有这些,   你需要使用坐标数组,因为每次调用repaint时,你都需要重绘所有的坐标   存储的行。 * /

import java.awt.*;
import java.awt.event.*; //brings in the ability to read loops by 'event' of something
import javax.swing.*; //interface that extends both the MouseMotionListener and MouseListener interfaces

public class Draw extends JApplet implements MouseListener, MouseMotionListener
{
    private int [] x;
    private int [] y;
    private boolean changed = true;
    Display draw;

    public void init()
    {
        draw = new Display(); //use Display not draw
        setContentPane(draw); //lets you draw the stuff
        draw.setBackground(Color.green); //sets the background color to whatever you want
        draw.setFont (new Font("SansSerif", Font.BOLD, 24)); //this sets the font size and type
        draw.addMouseListener(this);
        draw.addMouseMotionListener(this); //adds the MouseMotionListener
    }                                      //to read in actions of the mouse       

    class Display extends JPanel
    {
        public void paintComponent(Graphics g) //Graphics __ <--name can be anything
        {
            super.paintComponent(g); //paintComponent(__) <-- __ has to match w/ the one above

            g.setColor(Color.black); //these are the 5 buttons at the top
            g.fillRect(2, 2, 95, 70); //
            g.setColor(Color.red); //
            g.fillRect(100, 2, 95, 70); //
            g.setColor(Color.blue); //
            g.fillRect(198, 2, 95, 70); //
            g.setColor(Color.gray); //
            g.fillRect(296, 2, 95, 70); //
            g.setColor(Color.cyan); //
            g.fillRect(394, 2, 95, 70); //
            g.setColor(Color.white);
            g.drawString("RESET", 10, 45);
            g.drawString("RED", 125, 45);
            g.drawString("BLUE", 215, 45);
            g.drawString("GRAY", 310, 45);
            g.drawString("CYAN", 410, 45);
        }
    }

    public void mousePressed(MouseEvent evt) 
    {
         int x = evt.getX();
         int y = evt.getY();
         changed = false;
         if (y > 2 && y < 70)
         {
             changed = true;
             if (x > 2 && x < 100)
             {
                 draw.repaint();
             }
             else 
                 changed = false;
         }
    }

    public void mouseDragged(MouseEvent evt) 
    {

    }

    public void mouseReleased(MouseEvent evt) {}
    public void mouseMoved(MouseEvent evt) {}
    public void mouseEntered(MouseEvent evt) {} // Some empty routines.
    public void mouseExited(MouseEvent evt) {} // (Required by the MouseListener
    public void mouseClicked(MouseEvent evt) {} // and MouseMotionListener interfaces).
}

1 个答案:

答案 0 :(得分:4)

mousePressed应该在有人点击时触发(即当他们按下鼠标上的按钮时 - 不释放)。

mouseDragged应在用户按下鼠标按钮后触发,然后移动鼠标。

因此,您可以考虑将鼠标的x,y坐标存储在mousePressed上,然后在mouseDragged触发时在该x,y位置和当前鼠标x,y位置之间绘制一条线。