简体   繁体   English

使用JAVA AWT mousemotionlistener在Java Applet中绘制动态线条?

[英]Drawing dynamic lines in java applets using JAVA AWT mousemotionlistener?

So, here is the thing. 所以,这是东西。 My lecturer taught us how to get mouse coordinates by clicking on an applet. 我的讲师教我们如何通过单击小程序来获取鼠标坐标。 Now, i was thinking that may be i can try drawing dynamic lines using mousemotionlistener class using the mousedrag method and the mouse coordinates. 现在,我想可能是我可以尝试使用使用mousedrag方法和鼠标坐标的mousemotionlistener类绘制动态线。 I tried it but couldn't really make it work. 我尝试过,但无法真正使它起作用。 Can anyone please suggest me where i made the error. 任何人都可以建议我在哪里出错。 Thanks a lot. 非常感谢。

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.applet.*;

public class drawing extends Applet
implements MouseMotionListener {  

    int mx,my;
    Graphics2D g2;

    public void init()
    {
        this.addMouseMotionListener(this);

    }

    public void paint(Graphics g)
    {
        this.setSize(800,600);

         g2 = (Graphics2D) g;
         g2.setStroke(new BasicStroke(10));
         g2.drawLine(40,50,200,150); // I was experimenting to draw static lines using the drawLine() method. 
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

        mx = e.getX();
        my = e.getY();
        System.out.println(" "+ mx + " "+ my);
        g2.setStroke(new BasicStroke(10));
        g2.drawLine(mx,my,mx,my); 



    }

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

    }



}

In order to draw a line, you need to know the start point (which would be the point the user clicked) and the end point (the point where the user dragged to), from there is a simple matter to simple use Graphics#drawLine . 为了画一条线,您需要知道一个起点到终点(用户单击该点)和终点(用户拖动到的点),从简单的事情开始就可以使用Graphics#drawLine

Start by taking a look at How to Write a Mouse Listener , 2D Graphics and Painting in AWT and Swing for more details 首先查看如何 在AWT和Swing中 编写鼠标侦听器2D图形绘画以获取更多详细信息

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class Drawing extends Applet
                implements MouseMotionListener, MouseListener {

    private Point startPoint, endPoint;

    public void init() {
        this.addMouseMotionListener(this);
        addMouseListener(this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (startPoint != null && endPoint != null) {
            g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        endPoint = e.getPoint();
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent arg0) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        startPoint = e.getPoint();
        endPoint = e.getPoint();
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

}

Now, my advice, don't use Applet or AWT directly. 现在,根据我的建议,不要直接使用Applet或AWT。 Instead, start with a JPanel and override it's paintComponent method and use it perform your custom painting. 相反,从JPanel开始并覆盖它的paintComponent方法,并使用它执行您的自定义绘制。 Then add this panel to a JFrame to display it on the screen. 然后将此面板添加到JFrame以在屏幕上显示它。

AWT is more then 15 years out of date and applets have way to many gotchas which you just don't need to deal with right now. AWT已经过时15年了,Applet可以解决许多陷阱,您现在不需要处理这些陷阱。

Start by having a look at Creating a GUI With JFC/Swing or even do some research into JavaFX 首先看一下使用JFC / Swing创建GUI甚至对JavaFX进行一些研究

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

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