我正在尝试使用mouseDragged编写一个绘制一行圆圈的程序,就像MS绘制一样。点击后,我已经成功获得了绘制圆圈的程序。当我拖动鼠标时,我也成功地让我的程序画圆圈;但是,这不会在我拖动的任何地方留下一圈圆圈。它只是拖动同一个圆圈。我试图让我的程序在我拖动的地方留下一些圆圈,但我很困惑为什么我的程序不会这样做。
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);
}
}
感谢任何帮助!
答案 0 :(得分:4)
绘画具有破坏性。也就是说,每次调用paintComponent
时,都需要重新绘制组件的整个状态。
这引发了一个问题 - 每次调用paintComponent
时,你需要一些方法来存储你想要绘制的状态。
为此,一个简单的ArrayList
可以很好地完成工作。它允许您存储您感兴趣的所有点,并允许您在每次调用paintComponent
时重新绘制它们,例如......
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
中存储“形状”,它们具有如何绘制自己的某种概念,允许添加更复杂的形状
你还应该看看Painting in AWT and Swing,以便更好地了解Swing中的绘画实际上是如何工作的