- 자바 애플릿(Java Applet)에서 마우스 이벤트 구현하기
1. mouseMoved() 메서드 구현
사용자가 마우스를 움직일 때 이벤트 핸들러에 의해 mouseMoved()가 호출되면 지진 마커의 경우 지진 제목을, 도시의 경우 이름, 국가 및 인구를 표시한다.
- EarthquakeCityMap.java
/** Event handler that gets called automatically when the
* mouse moves.
*/
@Override
public void mouseMoved() {
// clear the last selection
if (lastSelected != null) {
lastSelected.setSelected(false);
lastSelected = null;
}
selectMarkerIfHover(quakeMarkers);
selectMarkerIfHover(cityMarkers);
//loop();
}
// If there is a marker selected
private void selectMarkerIfHover(List<Marker> markers) {
// Abort if there's already a marker selected
if (lastSelected != null) {
return;
}
for (Marker m : markers) {
CommonMarker marker = (CommonMarker)m;
if (marker.isInside(map, mouseX, mouseY)) {
lastSelected = marker;
marker.setSelected(true);
return;
}
}
}
- CommonMarker.java
// Common piece of drawing method for markers;
// YOU WILL IMPLEMENT.
// Note that you should implement this by making calls
// drawMarker and showTitle, which are abstract methods
// implemented in subclasses
public void draw(PGraphics pg, float x, float y) {
// For starter code just drawMaker(...)
if (!hidden) {
drawMarker(pg, x, y);
if (selected) {
showTitle(pg, x, y);
}
}
}
public abstract void drawMarker(PGraphics pg, float x, float y);
public abstract void showTitle(PGraphics pg, float x, float y);
- EarthquakeMarker.java
/** Show the title of the earthquake if this marker is selected */
public void showTitle(PGraphics pg, float x, float y) {
String title = getTitle();
pg.pushStyle();
pg.rectMode(PConstants.CORNER);
pg.stroke(110);
pg.fill(255,255,255);
pg.rect(x, y + 15, pg.textWidth(title) +6, 18, 5);
pg.textAlign(PConstants.LEFT, PConstants.TOP);
pg.fill(0);
pg.text(title, x + 3 , y +18);
pg.popStyle();
}
- CityMarker.java
/** Show the title of the city if this marker is selected */
public void showTitle(PGraphics pg, float x, float y) {
String name = getCity() + " " + getCountry() + " ";
String pop = "Pop: " + getPopulation() + " Million";
pg.pushStyle();
pg.fill(255, 255, 255);
pg.textSize(12);
pg.rectMode(PConstants.CORNER);
pg.rect(x, y-TRI_SIZE-39, Math.max(pg.textWidth(name), pg.textWidth(pop)) + 6, 39);
pg.fill(0, 0, 0);
pg.textAlign(PConstants.LEFT, PConstants.TOP);
pg.text(name, x+3, y-TRI_SIZE-33);
pg.text(pop, x+3, y - TRI_SIZE -18);
pg.popStyle();
}
2. mouseClicked() 메서드 구현
지진 표시를 선택하면 해당 지진의 위협 범위 내에 있는 모든 도시가 지도에 표시되고 다른 모든 도시와 지진은 숨겨지며, 도시의 마커를 선택하면 위협 범위에 해당 도시가 포함된 모든 지진이 지도에 표시되고 다른 모든 도시와 지진은 숨겨진다.
- EarthquakeCityMap.java
/** The event handler for mouse clicks
* It will display an earthquake and its threat circle of cities
* Or if a city is clicked, it will display all the earthquakes
* where the city is in the threat circle
*/
@Override
public void mouseClicked() {
if (lastClicked != null) {
unhideMarkers();
lastClicked = null;
}
else if (lastClicked == null) {
checkEarthquakesForClick();
if (lastClicked == null) {
checkCitiesForClick();
}
}
}
// Helper method that will check if a city marker was clicked on
// and respond appropriately
private void checkCitiesForClick() {
if (lastClicked != null) return;
// Loop over the earthquake markers to see if one of them is selected
for (Marker marker : cityMarkers) {
if (!marker.isHidden() && marker.isInside(map, mouseX, mouseY)) {
lastClicked = (CommonMarker)marker;
// Hide all the other earthquakes and hide
for (Marker mhide : cityMarkers) {
if (mhide != lastClicked) {
mhide.setHidden(true);
}
}
for (Marker mhide : quakeMarkers) {
EarthquakeMarker quakeMarker = (EarthquakeMarker)mhide;
if (quakeMarker.getDistanceTo(marker.getLocation()) > quakeMarker.threatCircle()) {
quakeMarker.setHidden(true);
}
}
return;
}
}
}
// Helper method that will check if an earthquake marker was clicked on
// and respond appropriately
private void checkEarthquakesForClick() {
if (lastClicked != null) return;
// Loop over the earthquake markers to see if one of them is selected
for (Marker m : quakeMarkers) {
EarthquakeMarker marker = (EarthquakeMarker)m;
if (!marker.isHidden() && marker.isInside(map, mouseX, mouseY)) {
lastClicked = marker;
// Hide all the other earthquakes and hide
for (Marker mhide : quakeMarkers) {
if (mhide != lastClicked) {
mhide.setHidden(true);
}
}
for (Marker mhide : cityMarkers) {
if (mhide.getDistanceTo(marker.getLocation()) > marker.threatCircle()) {
mhide.setHidden(true);
}
}
return;
}
}
}
// loop over and unhide all markers
private void unhideMarkers() {
for(Marker marker : quakeMarkers) {
marker.setHidden(false);
}
for(Marker marker : cityMarkers) {
marker.setHidden(false);
}
}
- EarthquakeMarker.java
/**
* Return the "threat circle" radius, or distance up to
* which this earthquake can affect things, for this earthquake.
* DISCLAIMER: this formula is for illustration purposes
* only and is not intended to be used for safety-critical
* or predictive applications.
*/
public double threatCircle() {
double miles = 20.0f * Math.pow(1.8, 2*getMagnitude()-5);
double km = (miles * kmPerMile);
return km;
}