日期:2014-05-20 浏览次数:20825 次
import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JPanel; public class MouseRe { public static void main(String[] args){ JFrame frame = new JFrame(); QQ a =new QQ(); frame.add(a); frame.setBounds(200, 100, 700, 500); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class QQ extends JPanel implements MouseListener{ /** * */ private static final long serialVersionUID = 1L; private int i = 30; private int j = 30; public QQ(){ addMouseListener(this); } @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub System.out.println("tadfa"); picture(i,j); i = i + 20; j = j + 20; } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } public void picture(int i,int j){ Graphics g = this.getGraphics(); g.fillOval(i, j, 20,20); } }
import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class MouseRe { public static void main(String[] args){ JFrame frame = new JFrame(); QQ a =new QQ(); frame.add(a); frame.setBounds(200, 100, 700, 500); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class QQ extends JPanel implements MouseListener{ /** * */ private static final long serialVersionUID = 1L; private int i = 30; private int j = 30; private List<Point> dots = new ArrayList<Point>(); public QQ(){ addMouseListener(this); } @Override public void mouseClicked(MouseEvent arg0) { picture(i,j); i = i + 20; j = j + 20; } @Override public void paint(Graphics g) { super.paint(g); for(Point p:dots) g.fillOval(p.x, p.y, 20, 20); } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } public void picture(int i,int j){ dots.add(new Point(i,j)); repaint(); } }