Skip to content

Instantly share code, notes, and snippets.

@jarhoads
Last active February 6, 2016 20:48
Show Gist options
  • Save jarhoads/ca946cbf2c78c653955f to your computer and use it in GitHub Desktop.
Save jarhoads/ca946cbf2c78c653955f to your computer and use it in GitHub Desktop.
/*
I did this while I was drinking wine after my kids went to bed.
My wine brain added an extra challenge to most of the problems that should have been easier.
This is why the code probably doesn't look as clean as it should.
I'm sure there are probably some edge cases somewhere that I'm not seeing and I didn't write any unit tests for any of it.
I could probably update the splitQuakes() method to return a new ArrayList<List<Marker>> instead of mutating the private one.
Also, that method looks goofy anway and there is probably a better way to do it.
My wine brain says it looks legit though so I'm just going with it. Thanks wine brain!
I thought this extension would be easier than it turned out to be.
I thought it would be pretty neat to be able to scroll through the list of earthquakes.
I'm sure some people will probably think it's dumb though. If you're one of those people... sorry?
*/
// code from the beginning of the class
// ...
// these private fields were added to the EarthquakeCityMap class
Marker[] quakes;
private List<List<Marker>> sortedRanges;
private int rangeIndex;
private int range;
private static final int PRINT_RANGE = 20;
// other code in the class
public void setup(){
// rest of the code
// ...
// i pulled out the sort of the quakes array
// from the sortAndPrint method and sorted it f
// so my extension can use the sorted array
// you could always just make another array and keep
// the sort in the sortAndPrint method
Arrays.sort(quakes);
sortAndPrint(PRINT_RANGE);
range = PRINT_RANGE;
rangeIndex = 0;
sortedRanges = new ArrayList<List<Marker>>();
splitQuakes();
}
// i used this for debugging
// just uncomment it to see each list
/*
System.out.println("Earthquakes split into ranges: ");
for(List<Marker> quakesList : sortedRanges){
System.out.println("Next range: ");
for(Marker q : quakesList){
System.out.println("quake: " + q);
}
}
*/
// method to split the list of markers
// into a list with a list of markers
// for each range
private void splitQuakes(){
int rangeCount = 0;
List<Marker> rangeList = new ArrayList<Marker>();
for(int i = 0; i < quakes.length; i++){
Marker quake = quakes[i];
rangeList.add(quake);
rangeCount++;
if(rangeCount >= range){
sortedRanges.add(rangeList);
rangeList = null;
rangeList = new ArrayList<Marker>();
rangeCount = 0;
}
}
if(!rangeList.isEmpty()){ sortedRanges.add(rangeList); }
}
// keyPressed() overridden method
// scrolls through the list of markers depending on whether
// 'u', 'd', or 'a' is pressed
@Override
public void keyPressed()
{
if(key == 'u'){
System.out.println("up key pressed!");
rangeIndex--;
if(rangeIndex < 0){ rangeIndex = sortedRanges.size() - 1; }
// show only the next top earthquakes
showNextMarkers();
}
if(key == 'd'){
System.out.println("down key pressed!");
rangeIndex++;
if(rangeIndex > sortedRanges.size() - 1){ rangeIndex = 0; }
showNextMarkers();
}
if(key == 'a'){
rangeIndex = 0;
System.out.println("show all pressed!");
for(Marker m : quakeMarkers){ m.setHidden(false); }
sortAndPrint(range);
}
}
// showNextMarker helper method
// this will show the next range of markers on the map
// and print the information to the console
private void showNextMarkers(){
List<Marker> showQuakes = sortedRanges.get(rangeIndex);
//System.out.println("Size of quakes list: " + showQuakes.size());
Marker[] printMarkers = new Marker[showQuakes.size()];
int printIndex = 0;
for(Marker quake : quakeMarkers){
if(showQuakes.contains(quake)){
quake.setHidden(false);
printMarkers[printIndex] = quake;
printIndex++;
}
else{
quake.setHidden(true);
}
}
Arrays.sort(printMarkers);
for(int i = 0; i < printMarkers.length; i++){ System.out.println(printMarkers[i]); }
}
// helper method to draw key in GUI
private void addKey() {
// rest of the code
// ...
// my updates to update the key
fill(0, 0, 0);
textAlign(LEFT, CENTER);
text("Keybord Shortcuts", xbase+25, ybase+240);
text("u - scroll up", xbase+25, ybase+260);
text("d - scroll down", xbase+25, ybase+280);
text("a - show all", xbase+25, ybase+300);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment