Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
KarelIconSwitcher.java

KarelIconSwitcher.java

So my CompSci teacher said changing the Karel icon couldn't be done. He was wrong. 😄

Usage

// Get an instance of the KarelIconSwitcher object
KarelIconSwitcher switcher = KarelIconSwitcher.getInstance();

// Set the icon of karel facing north while on to 'newkarel.png'
switcher.changeIcon(KarelIconSwitcher.KarelIcon.KAREL_NORTH_ON, "newkarel.png");

There are 6 icons that can (currently) be replaced.

  • KarelIconSwitcher.KarelIcon.KAREL_NORTH_ON
  • KarelIconSwitcher.KarelIcon.KAREL_SOUTH_ON
  • KarelIconSwitcher.KarelIcon.KAREL_EAST_ON
  • KarelIconSwitcher.KarelIcon.KAREL_WEST_ON
  • KarelIconSwitcher.KarelIcon.KAREL_NORTH_OFF
  • KarelIconSwitcher.KarelIcon.KAREL_SOUTH_OFF
  • KarelIconSwitcher.KarelIcon.KAREL_EAST_OFF
  • KarelIconSwitcher.KarelIcon.KAREL_WEST_OFF
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import kareltherobot.RobotWorldWindow;
import kareltherobot.World;
public class KarelIconSwitcher {
private static KarelIconSwitcher instance;
private Map<String, Field> fields;
public KarelIconSwitcher() {
fields = new HashMap<String, Field>();
for(KarelIcon icon : KarelIcon.values())
addField(icon.getFieldName());
}
public static KarelIconSwitcher getInstance() {
if(instance != null)
return instance;
return (instance = new KarelIconSwitcher());
}
public void changeIcon(KarelIcon karelState, String icon) {
try {
changeIcon(karelState, ImageIO.read(new File(icon)));
forceUpdate();
} catch (IOException e) { e.printStackTrace(); }
}
public void changeIcon(KarelIcon karelState, Image icon) {
try {
fields.get(karelState.getFieldName()).set(null, icon);
} catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); }
}
private void addField(String name) {
try {
Field field = RobotWorldWindow.class.getDeclaredField(name);
field.setAccessible(true);
fields.put(name, field);
} catch(Exception e) {}
}
private void forceUpdate() {
try {
Field view = World.class.getDeclaredField("view");
view.setAccessible(true);
RobotWorldWindow window = (RobotWorldWindow) view.get(null);
Method scaleAllImagesMethod = RobotWorldWindow.class.getMethod("scaleAllRobotImages");
scaleAllImagesMethod.setAccessible(true);
Method repaint = RobotWorldWindow.class.getMethod("repaint");
repaint.setAccessible(true);
Field oldScale = RobotWorldWindow.class.getDeclaredField("oldScale");
oldScale.setAccessible(true);
oldScale.set(null, 1);
window.scaleAllRobotImages();
window.repaint();
} catch(Exception e) { e.printStackTrace(); }
}
public enum KarelIcon {
KAREL_NORTH_ON("karelImageNOnBase"),
KAREL_SOUTH_ON("karelImageSOnBase"),
KAREL_EAST_ON("karelImageEOnBase"),
KAREL_WEST_ON("karelImageWOnBase"),
KAREL_NORTH_OFF("karelImageNOffBase"),
KAREL_SOUTH_OFF("karelImageSOffBase"),
KAREL_EAST_OFF("karelImageEOffBase"),
KAREL_WEST_OFF("karelImagWOffBase");
private String fieldName;
private KarelIcon(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldName() {
return this.fieldName;
}
}
}
The MIT License
Copyright (c) 2014 Will Franzen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.