add lab11

This commit is contained in:
2022-05-11 23:36:36 +08:00
parent 855bc8cc3c
commit 243a44f7ed
15 changed files with 421 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
public class MyPanel extends JPanel {
private ArrayList<Point> point;
public MyPanel() {
point = new ArrayList<Point>();
// Anonymous class.
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// Check whether the left mouse button is clicked
if (e.getButton() == MouseEvent.BUTTON1) {
// store the point which been clicked
point.add(e.getPoint());
// get the coordinate
System.out.println("(" + e.getX() + "," + e.getY() + ")");
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
// must clean the panel before drawing on it.
super.paintComponent(g);
g.setColor(Color.RED);
for (int i = 0; i < point.size(); i++) {
g.drawRect((int) point.get(i).getX(), (int) point.get(i).getY(), 1, 1);
} // draw the red square
repaint();
}
}