Java �C�x���g���荞��

�ڎ�

���ǂ�

�C�x���g�����̊�{

�C�x���g�Ƃ�

�v���O�����̎��s���Ƀ{�^�����N���b�N���ꂽ�Ƃ��A�}�E�X���N���b�N���ꂽ�A�L�[�{�[�h�̃L�[���@���ꂽ�A�Ȃǂ̃��[�U�[�̍s���� Java �̃V�X�e���ɃC�x���g�ƌĂ΂�銄���ݏ����v���V�O�i������������B��������m���A�K�؂ȏ����v���O������g�ݍ���ł����΁A���I�ȏ�񏈗����”\�ɂȂ�B

�C�x���g�ɂ́AActionEvent�i�{�^�����N���b�N���ꂽ�A�Ȃǁj�AMouseEvent�i�}�E�X���N���b�N���ꂽ�A�h���b�O���ꂽ�A�Ȃǁj�AWindowEvent�i�E�B���h�E�́u�‚���v�{�^��������������ꂽ�A�Ȃǁj�AKeyEvent�i�L�[�{�[�h����^�C�v���ꂽ�A�Ȃǁj�ATextEvent�i�e�L�X�g���ύX���ꂽ�A�Ȃǁj�Ȃǂ�����B

�C�x���g����

�C�x���g�����̃v���O�����i���\�b�h�j�̓��X�i�[�ƌĂ΂��C���^�[�t�F�[�X�Ŏd�l�����߂��Ă���̂ŁA���̃��X�i�[���������A���̒��̃��\�b�h�̒�`��^����΁A�����”\�ɂȂ�B

�Ⴆ�΁A�}�E�X�N���b�N�C�x���g�̏ꍇ�A�C�x���g����������̂� mouseClicked() ���\�b�h�ŁA������܂� MouseListener �C���^�[�t�F�[�X���������iimplements MouseListener�j�A���ۂɃC�x���g����������Ƃ���ɂ����m�点�iaddMouseListener�j�A�������\�b�h mouseClicked() ���`����B

public class MyFrame3 extends Frame implements MouseListener {
	MyFrame3() {
...
this.addMouseListener(this);
...
} ...
public void mouseClicked(MouseEvent me) { ... }
}

�v���O�����̗���ڂ���B

public class MyFrame3 extends Frame implements MouseListener {
	MyFrame3() {
		this.setSize(200, 200);
		this.addMouseListener(this);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new MyFrame3();
	}
	public void mouseClicked(MouseEvent me) {
		System.out.println("�}�E�X���N���b�N���ꂽ" + me.getX() + "," + me.getY());
	}
	public void mouseEntered(MouseEvent me) {
		System.out.println("�}�E�X���E�B���h�E�ɓ�����");
	}
	public void mouseExited(MouseEvent me) {
		System.out.println("�}�E�X���E�B���h�E����o��");
	}
	public void mousePressed(MouseEvent me) {
		System.out.println("�}�E�X���v���X���ꂽ");
	}
	public void mouseReleased(MouseEvent me) {
		System.out.println("�}�E�X�������[�X���ꂽ");
	}
}

���s���ʂ̗�F
�}�E�X���E�B���h�E�ɓ����� �}�E�X���v���X���ꂽ �}�E�X�������[�X���ꂽ �}�E�X���N���b�N���ꂽ�F45,80 �}�E�X���E�B���h�E����o��

mouseClicked() �ȉ��̂T�‚̃��\�b�h���C�x���g�����p�̃v���O�����ŁA�����g�ݍ��ނ��߂ɁA�t���[���̃R���X�g���N�^�� addMouseListener(this) ���\�b�h�����s���Ă���B������”\�ɂ��邽�߂ɁA�N���X�ɂ� MouseListener �C���^�[�t�F�[�X����������Ă���BMouseEvent �N���X�� getX(), getY() ���\�b�h���g���āA�N���b�N���ꂽ���W��m�邱�Ƃ��ł���B

���s���āA�}�E�X���E�B���h�E�̒��ɓ������A�N���b�N���A�E�B���h�E�̊O�ɏo��A�Ƃ�������ɑ΂��錋�ʂ����s���ʂ̗�ɏ�����Ă���B�N���b�N����ƃ{�^�����v���X�����A�����[�X�����A�N���b�N�����A�Ƃ����R�‚̃C�x���g�����̏��Ԃɔ�������̂ŁA�����̏����v���O�������N���������ʂ��̂悤�ȕ\���������邱�ƂɂȂ�B

���X�i�[�ƃA�_�v�^�[

�C�x���g�̓��X�i�[�Ō��m�����̂ŁA�uimplements ���X�i�[�v�Ƃ����L�q���K�v�ɂȂ邪�A�����Ȃ�ƁA�C���^�[�t�F�[�X�̈�ʋK���Ƃ��āA�����Œ�`����Ă��邷�ׂẴ��\�b�h���������Ȃ���΂����Ȃ��B��̗�� MouseListener �C���^�[�t�F�[�X�ɂ͂T�‚̃��\�b�h������A���ۂɕK�v�Ƃ���̂� mouseClicked() �����Ƃ����ꍇ�ɁA���̂S�‚̃��\�b�h�ɂ����̂�^���Ȃ��ꂢ���Ȃ��Ƃ�������͂��Ȃ�ς킵���B

�������Ȃ��Ă��ǂ��悤�ɁA���X�i�[�� implement �����N���X���p�ӂ���Ă���B������A�_�v�^�[�Ƃ����B�A�_�v�^�[�̓N���X�Ȃ̂ŁA�K�v�ȃ��\�b�h�����㏑������Ηǂ����ƂɂȂ�B���̑㏞�Ƃ��āA�Q�‚̃N���X��e�N���X�Ƃ��邱�Ƃ͂ł��Ȃ��Ƃ�����������ɂ��A�A�_�v�^�[���p�������N���X��V���ɒ�`���Ȃ���΂Ȃ�Ȃ��B

�����N���X

��������|����łȂ���΁A�C�x���g������������s�v���O�����i�N���X�j�̒��œƎ��ɃN���X���`����Ƃ������@���L���ł���B���̂悤�ɒ�`���ꂽ�N���X�͓����N���X�ƌĂ΂��B�ʏ�̃N���X�̒�`�Ɠ����ɂ���΂悢�B�����N���X���܂ރN���X�ŗL���ȕϐ��⃁�\�b�h�͓����N���X�̒��ł��g�����Ƃ��ł���B�����N���X�ŃC�x���g���������s���邱�Ƃ��������߂ɁAaddMouseListener() �̈����ɁA���̓����N���X�̃C���X�^���X���w�肷��B

�Ⴆ�΁A���͏�̃v���O��������A�_�v�^�[���g���ď������������̂ł���BMouseAdapter ���p������ MyMouseAdapeter �N���X���`���A���̒��� mouseClicked() ���\�b�h�������I�[�o�[���C�h����B�R���X�g���N�^�ŁAaddMouseListener(new MyMouseAdapter()) �����s���邱�Ƃɂ��A�}�E�X�C�x���g������ MyMouseAdapter �N���X�Ŏ��s���邱�Ƃ��w�肵�Ă���B

public class MyFrame3 extends Frame {
	MyFrame3() {
		this.setSize(200, 200);
		this.addMouseListener(new MyMouseAdapter());
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new MyFrame3();
	}
	class MyMouseAdapter extends MouseAdapter {
		public void mouseClicked(MouseEvent me) {
			System.out.println("�}�E�X���N���b�N���ꂽ�F" + me.getX() + "," + me.getY());
		}
	}
}

�C���^�[�t�F�[�X���������邱�Ƃ͂Ȃ��̂ŁAimplements �������Ă���B���s����ƁA�m���ɁA�N���b�N���ꂽ�Ƃ�����񂾂����\�������B

MyMouseAdapter �Ƃ����N���X�̖��O�� addMouseListener() �̈����Ƃ��Ďg���Ă��邾���Ȃ̂ŁA���̖��O�������Ƃ������ɁA��`���̂��̂������Ƃ���Ƃ�������������B��̃v���O����������������Ǝ��̂悤�ɂȂ�B

	this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) { System.out.println("�}�E�X���N���b�N���ꂽ�F" + me.getX() + "," + me.getY()); } });

���܂茩�₷���Ȃ����A���̏����ł����g��Ȃ��N���X�ł���΁A���̒�`�̎d���������͂Ȃ��B����𖳖������N���X�Ƃ����B�E�B���h�E�́u�‚���v�{�^���Ώ��̃v���O���� WindowAdapter �̒��� windowClosing() ���\�b�h�j�ł́A�P�� System.exit() ���\�b�h�����s���邾���Ȃ̂ŁA���̖��������N���X���p���X�}�[�g�ł���B

	this.addWindowListener(new WindowAdapter(){
	   	public void windowClosing(WindowEvent we) { System.exit(0); }
	});


�C�x���g�̎�ނƃ��X�i�[�A�A�_�v�^�[

�C�x���g�ɂ͈ȉ��̂悤�Ȏ�ނ�����B���X�i�[�� xxxListener�A�A�_�v�^�[�� xxxAdapter�A�C�x���g��xxxEvent �Ɩ�������Ă���B

�����C�x���g ��� ���X�i�[ �A�_�v�^�[ ���\�b�h
ActionEvent
�{�^���ق� ActionListener
actionPerformed()
MouseEvent
�}�E�X MouseListener MouseAdapter mouseCLicked() �Ȃ�(*!)
MouseEvent �}�E�X�̓��� MouseMotionListener MouseMotionAdapter
mouseDragged(), mouseMoved()
WindowEvent
�E�B���h�E�֘A WindowListener WindowAdapter windowClosed() �Ȃ�(*2)
KeyEvent
�L�[�{�[�h KeyListener KeyAdapter keyTyped(), keyPressed(), keyReleased()
TextEvent
�e�L�X�g�ύX TextListener TextAdapter textValueChanged()

MouseListener �̃��\�b�h�͂��̑��� mouseEnterd(), ...Exited(), ...Pressed(), ... Released() �̂S�BWindowListener �̃��\�b�h�͂��̑��� windowActivated(), ...Deactivated(), ...Opened(), ...Closing(), ...Iconified(), ...Deiconified() �̂U�‚�����B


�}�E�X�C�x���g

���s���̃f�[�^���͂̈�‚Ƃ��āA�}�E�X���g�����@������B�}�E�X�̃{�^���̃A�b�v�_�E����ʒu���𗘗p���ē��I�ȏ������ł���B�d�q�ł̃L�[�{�[�h��\�������āA������N���b�N�����邱�Ƃɂ��A����������͂���Ȃǂ̕��@������B

�}�E�X�C�x���g�ɂ‚��Ă܂Ƃ߂�B�ÓI�ȓ����i�{�^���������A�����A�N���b�N����A����̈�ɓ���A����̈悩��o��j�ɂ‚��Ă� MouseListener�A���I�ȓ����i�}�E�X�𓮂����A�{�^���������Ȃ��瓮�����j�ɂ‚��Ă� MouseMotionListener ���Ή�����B�܂Ƃ߂�Ƃ‚��̂悤�ɂȂ�B

�C�x���g ���\�b�h ���X�i�[ �A�_�v�^�[
�}�E�X�{�^�����N���b�N���ꂽ�� mouseClicked MouseListener MouseAdapter
�}�E�X�{�^���������ꂽ�Ƃ� mousePressed
�}�E�X�{�^���������ꂽ�Ƃ� mouseReleased
�}�E�X������̈�ɓ������Ƃ� mouseEntered
�}�E�X������̈悩�痣�ꂽ�Ƃ� mouseExited
�}�E�X���h���b�O����Ă���Ƃ� mouseDragged MouseMotionListener MouseMotionAdapter
�}�E�X���������Ƃ� mouseMoved
�}�E�X�z�C�[���������� mouseWheelMoved MouseWheelListener

�C�x���g�����v���O���������ɂ́A���X�i�[�������i implements �j���AaddMouseListener() ���邢�� addMouseMotionLitener(), addMouseWheelLitener() ���\�b�h�ŃC�x���g���󂯎��ꏊ��ݒ肵�A���\�b�h�̒�`��^����B�C�x���g�̏ڍׂȏ��� MouseEvent �N���X�̃��\�b�h���Q�Ƃ���Ηǂ��B���Ƃ��΁A�}�E�X�̈ʒu�� getX(), getY() �Œm�邱�Ƃ��ł���B

���X�i�[����������ƁA���ׂẴ��\�b�h���`���Ȃ���΂����Ȃ��Ȃ�̂ŁA�s�v�Ȃ��̂������ꍇ�� Adapter ���g���̂����ʁBAdapter�̓N���X�Ȃ̂ŁA�K�v�ȃ��\�b�h�����I�[�o�[���C�h����΂悢�B

MouseAdapter �̎g�p��

���́A�������͗p�̃L�[�{�[�h��\�����A�N���b�N�����L�[�{�[�h�̐�����\�����Ă���BMyMouseAdapter �� MouseAdapter ���p�������T�u�N���X�ŁA�����N���X�Ƃ��Ē�`���Ă���Bnew MyMouseAdapter() �� addMouseListener �̈����Ƃ��邱�ƂŁA�}�E�X�N���b�N���̏����v���O������g�ݍ���ł���Bthis �͂��̖��ߕ��̂���N���X�A�Ƃ����Ӗ��ł���B

public class MyMouseEvent extends Frame {
	Graphics gr;
	
	MyMouseEvent() {
		this.setSize(550, 300);
		this.addMouseListener(new MyMouseAdapter());		// MouseListener ���p�錾
		this.setVisible(true);
		gr = this.getGraphics();					// �����Ղ̕\��
		for(int i=0; i<10; i++) {
			gr.drawString(""+i, 50*i+40, 150);
			gr.drawRect(50*i+20, 100, 50, 80);
		}
	}
	public static void main(String[] args) {
		new MyMouseEvent();
	}
	class MyMouseAdapter extends MouseAdapter {			// �N���b�N���̏����v���O����
		public void mouseClicked(MouseEvent me) {
			System.out.println((int)((me.getX()-20)/50) + " ���N���b�N���ꂽ");
			
		}
	}
}

�����ł̓N���b�N���ꂽ���̃C�x���g�݂̂ɔ������� mouseClicked() ���\�b�h�������㏑�����Ă���Bme.getX(), me.getY() �̓N���b�N���ꂽ�_�� x, y ���W�����o���B

���s���ĕ\������鐔���̂ǂꂩ���N���b�N����ƁA���̐������R���\�[���ɕ\�������B

MouseMotionListener �̎g�p��

���́A�}�E�X�������Ƃ��̉��ʒu�̋O�Ղ����n��ŕ\�����A�h���b�O����ƒǐ��������`���Ƃ����v���O�����BMouseMotionListener �̂Q�‚̃��\�b�h���g���Ă���̂ŁA�C���^�[�t�F�[�X���������A�Q�‚̃��\�b�h�̒�`�������Ă���BaddMouseMotionListener �̈����� this �ł���B

public class MyMouseEvent2 extends Frame implements MouseMotionListener {
	Graphics gr;
	int x=0, y=0;
	
	MyMouseEvent2() {
		this.setSize(400, 400);
		this.addMouseMotionListener(this);		// �����v���O�����͂����Œ�`����Ă���
		this.setVisible(true);
		gr = this.getGraphics();
	}

	public static void main(String[] args) {
		new MyMouseEvent2();
	}

	public void mouseMoved(MouseEvent me) {
		gr.copyArea(0, 0, 400, 400, 0, 5);		// ��ʑS�̂����ɂ��炵�Ă���
		gr.clearRect(0, 0, 400, 5);
		gr.fillOval(me.getX(), 0, 5, 5);
	}
	public void mouseDragged(MouseEvent me) {
		gr.drawLine(x, y, me.getX(), me.getY());		// ���O�̓_�ƌ��݂̓_������
		x = me.getX();
		y = me.getY();
	}
}

copyArea() �͎w�肳�ꂽ�����`���R�s�[���A�w�肳�ꂽ���W������̓_�Ƃ��ăy�[�X�g����B clearRect() �͎w��̒����`��n�̐F�ɓh��‚Ԃ��B

�{�^���N���b�N�C�x���g

���s���ɁA�{�^�����N���b�N���ꂽ��w�肳�ꂽ�v���O���������s����A�Ƃ����ꍇ�ɕK�v�ȏ����v���O�������q�ׂ�B�{�^�����N���b�N����Ƃ����C�x���g���Ď�����̂� ActionListener �Ȃ̂ŁA������������AactionPerformed() ���\�b�h�ł��̏����v���O�������`����B���̂Ƃ��A�C�x���g�̏��� ActionEvent �N���X�̃��\�b�h���g���Ĉ����o�����Ƃ��ł���B

(1)	class ... implements ActionListener {  		// ActionListener �̎���
	
(2) Button mb = new Button("test"); // Button �̒�`�Ɠ\��t�� mb.addActionListener(new MyActionListener()); // �C�x���g�����v���O�����g�ݍ���
(3) class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent ae){ if(ae.getActionCOmmand() == "test") {
�����ɏ����v���O�������L�q
}
} }

getActionCommand() ���N���b�N���ꂽ�{�^���̃��x�������o�� ActionEvent �̃��\�b�h�ł���B

�{�^�����N���b�N����ƁA�w�i�F�������_���ɕς��A�Ƃ����v���O�����̗�B
public class MyButton2 extends Frame implements ActionListener {
	Button btn;
	MyButton2() {
		this.setSize(400, 400);
		btn = new Button("change");
		btn.addActionListener(this);
		this.add(btn, "North");
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new MyButton2();
	}

	public void actionPerformed(ActionEvent ae) {
		int j = (int)(256 * Math.random());
		this.setBackground(new Color(j,j,0));			
	}
}

�s�������̑Ώ��@

���s�����ߒ��ŃG���[���������ꍇ�́A�ʏ�A�G���[���b�Z�[�W�����s����Ď��s���~�܂�B�������A���̃��b�Z�[�W���󂯎~�߂đΏ�����v���O�����������Ă����΁A���s���p�������邱�Ƃ��o����B���̎d�g�݂��utry ... catch�v�\���ł���B

�G���[�̋N����”\���̂��鏈���i���̏ꍇ�́uThread.sleep()�v���utry{ }�v �ň͂݁A�G���[���N�����Ƃ��̏������ucatch( ) { }�v�ɏ����B�������G���[�̓��e�́uException ex�v�ɂ���Ēm�邱�Ƃ��o����B

�Ⴆ�΁A�X���b�h�̏����Ŕ�������G���[���`�F�b�N���邽�߂ɁA���̂悤�ȃv���O���� �������B

while(true) {
	repaint();
	try{
		Thread.sleep(100);
	} catch(Exception ex) {
		System.out.println("�G���[���荞�݂��������܂����F"+ex);
	}
}

���ǂ�