出口尚哉 No title
No License Java
2020年07月22日
Copy
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;

public class MyPanel extends JPanel {
	private List<Shape> shapes;

	public MyPanel() {
		super();
		this.shapes = new ArrayList<>();
		MouseAdapter myMcl = new MyMouseClickListener(this);  // change here
		// insert here
		this.addMouseListener(myMcl);
		}

	public void addShape(Shape s) {
		this.shapes.add(s);
		this.repaint();
		}

	public void panelClicked(int x, int y){
		for (Shape s : this.shapes) {
			if (s != null) {
				s.select(x, y);
				}
			}// insert here
		this.repaint();
		}

	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		System.out.println("MyPanel repainting ...");
		for (Shape s : this.shapes) {
			if (s != null) {
				s.draw(g);
				}
			}
		}
	}
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;

public class MyPanel extends JPanel {
	private List<Shape> shapes;

	public MyPanel() {
		super();
		this.shapes = new ArrayList<>();
		MouseAdapter myMcl = new MyMouseClickListener(this);  // change here
		// insert here
		this.addMouseListener(myMcl);
		}

	public void addShape(Shape s) {
		this.shapes.add(s);
		this.repaint();
		}

	public void panelClicked(int x, int y){
		for (Shape s : this.shapes) {
			if (s != null) {
				s.select(x, y);
				}
			}// insert here
		this.repaint();
		}

	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		System.out.println("MyPanel repainting ...");
		for (Shape s : this.shapes) {
			if (s != null) {
				s.draw(g);
				}
			}
		}
	}

No one still commented. Please first comment.