使用 mouseDragged 绘制圆圈轨迹 [英] Draw trail of circles with mouseDragged

查看:43
本文介绍了使用 mouseDragged 绘制圆圈轨迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,用 mouseDragged 绘制一条圆圈,就像 MS Paint 一样.当我点击时,我已经成功地让我的程序画了一个圆圈.当我拖动鼠标时,我也成功地让我的程序绘制了一个圆圈;但是,这不会在我拖动的任何地方留下一行圆圈.它只是拖动相同的圆圈.我试图让我的程序在我拖动的地方留下一串圆圈,但我很困惑为什么我的程序不会这样做.

I am trying to write a program that draws a line of circles with mouseDragged, like MS paint does. I have successfully gotten my program to draw a circle when I click. I have also been successful in getting my program to draw a circle when I drag the mouse; however, this doesn't leave a line of circles behind wherever I dragged. It simply drags the same circle around. I am trying to get my program to leave a trail of circles behind where I am dragging but I am pretty confused on why my program wont do so.

package assignment_11;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;


public class Canvas extends JComponent implements MouseListener, MouseMotionListener{

private int x, x1;
private int y, y1;

public Canvas() {
    addMouseMotionListener(this);
    addMouseListener(this);
}

public static void main(String[] args) {
    //creates new JFrame, sets Exit On Close, sets visible
    JFrame window = new JFrame();
    window.add(new Canvas());
    window.pack();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

public Dimension getPreferredSize() {
    return new Dimension(640,480);
}

@Override
public void mouseClicked(MouseEvent arg0) {
    System.out.println(arg0);
    x = arg0.getX();
    y = arg0.getY();
    repaint();

}

@Override
public void mouseEntered(MouseEvent arg0) {
    System.out.println(arg0);

}

@Override
public void mouseExited(MouseEvent arg0) {
    System.out.println(arg0);

}

@Override
public void mousePressed(MouseEvent arg0) {
    System.out.println(arg0);

}

@Override
public void mouseReleased(MouseEvent arg0) {
    System.out.println(arg0);

}

public void paintComponent(Graphics g) {
    g.fillOval(x, y, 10, 10);
    g.fillOval(x1, y1, 10, 10);
}

@Override
public void mouseDragged(MouseEvent arg0) {
    System.out.println(arg0);
    x1 = arg0.getX();
    y1 = arg0.getY();
    repaint();
}

@Override
public void mouseMoved(MouseEvent arg0) {
    System.out.println(arg0);
}


}

感谢任何帮助!

推荐答案

绘画具有破坏性.也就是说,每次调用 paintComponent 时,您都需要重新绘制组件的整个状态.

Painting is destructive. That is, every time paintComponent is called, you are expected to repaint the entire state of the component.

这引发了一个问题——每次调用 paintComponent 时,您都需要某种方式来存储要绘制的状态.

This raises the issue - you need some way to store the state you want to paint every time paintComponent is called.

为此,一个简单的 ArrayList 可以很好地完成这项工作.它允许您存储您感兴趣的所有点,并允许您在每次调用 paintComponent 时重新绘制它们,例如...

For this, a simple ArrayList would do the job nicely. It would allow you to store all the points you're interested and allow you to repaint them each time paintComponent is called, for example...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Canvas extends JComponent implements MouseListener, MouseMotionListener {

    private List<Point> points;

    public Canvas() {
        points = new ArrayList<>(25);
        addMouseMotionListener(this);
        addMouseListener(this);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater((new Runnable() {
            @Override
            public void run() {
                JFrame window = new JFrame();
                window.add(new Canvas());
                window.pack();
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                window.setVisible(true);
            }
        }));
    }

    public Dimension getPreferredSize() {
        return new Dimension(640, 480);
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        System.out.println(arg0);
        points.add(arg0.getPoint());
        repaint();

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        System.out.println(arg0);

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        System.out.println(arg0);

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        System.out.println(arg0);

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        System.out.println(arg0);

    }

    public void paintComponent(Graphics g) {
        for (Point p : points) {
            g.fillOval(p.x, p.y, 10, 10);
        }
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        System.out.println(arg0);
        points.add(arg0.getPoint());
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        System.out.println(arg0);
    }

}

现在,随着问题复杂性的增加,您可以将形状"存储在 List 中,这些形状"具有如何绘制自身的某种概念,从而允许添加更复杂的形状

Now, as the complexity of your problem grows, you could instead store "shapes" in the List which have some kind of notion of how to paint themselves, allowing to add in more complex shapes

您还应该查看在 AWT 和 Swing 中绘画 更好地了解 Swing 中绘画的实际工作原理

You should also have a look at Painting in AWT and Swing to gain a better understand of how painting in Swing actually works

这篇关于使用 mouseDragged 绘制圆圈轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆