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

发布于 2024-10-17 09:24:50 字数 3088 浏览 0 评论 0原文

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

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

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

In a drawing program like Microsoft’s PaintTM, one of the features available is drawing straight lines. You
can draw a straight line by pressing down on the mouse to indicate where the line should start. Then, with
the mouse still pressed down, you may move the mouse (a mouse drag), and the end point of the line is
created as you move. When you release the mouse, the line remains. You may repeat this process many
times, creating many different lines in your PaintTM document.

If you wish to emulate this effect in a java program, you may use MouseListener (the mousePressed
method), and MouseMotionListener (the mouseDragged method), to create a line segment. In addition, we
would like to be able to clear the paint area with a “clear button” at the top. Also, we would like to change
the colors of all of the lines by placing some “color buttons” at the top. In order to accomplish all of this,
you will need to use arrays of coordinates, because each time you call repaint, you will need to redraw all of
the lines stored.
*/

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2024-10-24 09:24:50

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

mouseDragged 应该在用户按下鼠标按钮并随后移动鼠标后触发。

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

mousePressed should fire when someone clicks (i.e. when they push down the button on the mouse - not release).

mouseDragged should fire after the person pushes the mouse button, and subsequently moves the mouse.

So, you might consider storing the x,y coordinates of the mouse on mousePressed, and then drawing a line between that x,y position and the current mouse x,y position when mouseDragged has fired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文