日期:2014-05-20 浏览次数:20661 次
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Exercise16_23 extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new Exercise16_23();
frame.pack();
frame.setTitle("Exercise16_23");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Exercise16_23() {
add(new DrawPoint());
}
class DrawPoint extends JPanel {
private ArrayList<Point> list = new ArrayList<Point>();
private int x ;
private int y ;
public DrawPoint() {
setPreferredSize(new Dimension(600,600));
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
x = (int)(Math.random()*400);
y = (int)(Math.random()*400);
list.add(new Point(x,y));
for(int i=0;i<list.size();i++) {
g.drawOval(list.get(i).x, list.get(i).y, 10, 10);
}
System.out.println(list);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Exercise16_23 extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new Exercise16_23();
frame.pack();//估计也算一次
frame.setTitle("Exercise16_23");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);//这里绝对是一次
}
public Exercise16_23() {
add(new DrawPoint());
}
class DrawPoint extends JPanel {
private ArrayList<Point> list = new ArrayList<Point>();
private int x ;
private int y ;
public DrawPoint() {
setPreferredSize(new Dimension(600,600));
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
x = (int)(Math.random()*400);
y = (int)(Math.random()*400);
list.add(new Point(x,y));//你在这里添加Point,每次调用paintComponent都会添加一个新的Point。
for(int i=0;i<list.size();i++) {
g.drawOval(list.get(i).x, list.get(i).y, 10, 10);
}
System.out.println(list);
}
}
}
x = (int)(Math.random()*400);
y = (int)(Math.random()*400);
list.add(new Point(x,y));//你在这里添加Point,每次调用paintComponent都会添加一个新的Point。