fork download
  1. package Lec10;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*; //課題2で追加
  6. import javax.swing.*;
  7.  
  8. class MyMouseSample03 extends JPanel { //課題2で変更
  9.  
  10. java.util.List<Point> pointList = new LinkedList<Point>(); //課題2で追加
  11. int xPanelSize;
  12. int yPanelSize;
  13. int x, y;
  14. Dimension size;
  15.  
  16. //コンストラクタ
  17. MyMouseSample03(int xPanelSize, int yPanelSize) { //課題2で変更
  18. this.xPanelSize = xPanelSize;
  19. this.yPanelSize = yPanelSize;
  20.  
  21. addMouseMotionListener(new MyMouseMotionListener());
  22. addMouseListener(new MyMouseActionListener());
  23. }
  24.  
  25. class MyMouseMotionListener extends MouseMotionAdapter {
  26.  
  27. public void mouseMoved(MouseEvent evt) {
  28. x = evt.getX();
  29. y = evt.getY();
  30. System.out.println("( " + x + " , " + y + " )");
  31.  
  32. repaint();
  33. }
  34. }
  35.  
  36. class MyMouseActionListener extends MouseAdapter {
  37.  
  38. private int clickNumber = 0;
  39.  
  40. public void mouseClicked(MouseEvent evt) {
  41. clickNumber++;
  42. System.out.println("( " + evt.getX() + " , " + evt.getY() + " ) " + clickNumber + "回目のクリック");
  43.  
  44. //課題2で追加
  45. pointList.add(evt.getPoint());
  46. repaint();
  47. }
  48. }
  49.  
  50. protected void paintComponent(Graphics g) {
  51. super.paintComponent(g);
  52.  
  53. size = getSize();
  54.  
  55. g.setColor(Color.BLACK);
  56. g.fillRect(0, 0, size.width, size.height);
  57.  
  58. //課題2で追加
  59. Iterator<Point> it = pointList.iterator();
  60. while (it.hasNext()) {
  61. Point p = (Point) it.next();
  62.  
  63. g.setColor(Color.BLUE);
  64. g.fillOval(p.x - 5, p.y - 5, 10, 10);
  65. }
  66.  
  67. g.setColor(Color.GREEN);
  68. g.drawLine(x, 0, x, size.height);
  69. g.drawLine(0, y, size.width, y);
  70. }
  71.  
  72. public static void main(String[] args) {
  73. MyMouseSample03 panel = new MyMouseSample03(500, 400); //課題2で変更
  74. panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
  75.  
  76. //JFrame fr = new JFrame();
  77. //MyMenuFrame02 fr = new MyMenuFrame02("MyMouseSample01");
  78. MyMenuFrame03 fr = new MyMenuFrame03("MyMouseSample03"); //課題2で変更
  79.  
  80. fr.add(panel);
  81. fr.pack();
  82. fr.setVisible(true);
  83. }
  84. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty