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