Mouse와 MouseMotion 이벤트

·2025년 6월 1일

Power Java

목록 보기
60/75

10.5 Mouse와 MouseMotion 이벤트

마우스 이벤트

-사용자가 마우스 버튼을 누르거나 마우스를 움직일 때 발생한다.

*마우스에서 발생하는 이벤트는 8가지이다.

-> 5가지는 MouseListener 안에 정의
-> 2가지는 MouseMotionLisner 안에
-> 1가지는 MouseWhellListener 안에 정의된다.

마우스에 대한 이벤트를 분리하는 이유는
마우스의 움직임을 추적하는 것이 시스템의 오버헤드를 증가시키기 때문이다.

MouseListener 인터페이스

MouseMotionListener 인터페이스


-마우스의 현재 좌표는 이벤트 객체의 getX()와 getY()를 호출하면 알 수 있다.


MouseEvent 객체(클래스)

보유 메소드



1. MouseListener or MouseAdapter를 구현하고
mouseClicked(MouseEvent e)에서 다음 조건을 확인한다.
2. MouseEvent 객체에서 getX(), getY() 메소드를 사용한다.
3.

Random rand = new Random();
int r = rand.nextInt(256); // 0~255
int g = rand.nextInt(256);
int b = rand.nextInt(256);
Color randomColor = new Color(r, g, b);

int radius = rand.nextInt(50) + 10;
g.setColor(randomColor);
g.fillOval(x, y, radius, radius);

0개의 댓글