用 mouseDragged 绘制圆圈轨迹
Draw trail of circles with mouseDragged
我正在尝试编写一个程序,用 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);
}
}
感谢任何帮助!
绘画具有破坏性。也就是说,每次调用 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);
}
}
现在,随着问题的复杂性增加,您可以将 "shapes" 存储在 List
中,它具有某种关于如何绘制自身的概念,允许添加更复杂的形状
您还应该看看 Painting in AWT and Swing 以更好地了解 Swing 中绘画的实际工作原理
我正在尝试编写一个程序,用 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);
}
}
感谢任何帮助!
绘画具有破坏性。也就是说,每次调用 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);
}
}
现在,随着问题的复杂性增加,您可以将 "shapes" 存储在 List
中,它具有某种关于如何绘制自身的概念,允许添加更复杂的形状
您还应该看看 Painting in AWT and Swing 以更好地了解 Swing 中绘画的实际工作原理