java - JUnit testing lines drawn by user -
i'm having problem writing test code , don't have clue how it.
this code draws line when 1 of key pressed length of 1. when holding shift down create line length of 5. need test draw line correct length parameter each case.
class sketchpanel extends jpanel { private point2d last; private final arraylist<line2d> lines; private static final int small_increment = 1; private static final int large_increment = 5; public sketchpanel() { super(); last = new point2d.double(100, 100); lines = new arraylist<>(); keyhandler listener; listener = new keyhandler(); addkeylistener(listener); setfocusable(true); } public void add(final int dexx, final int dexy) { point2d end; end = new point2d.double(last.getx() + dexx, last.gety() + dexy); line2d line; line = new line2d.double(last, end); lines.add(line); repaint(); last = end; } @override public void paintcomponent(final graphics graph) { super.paintcomponent(graph); graphics2d graph2 = (graphics2d) graph; lines.foreach((s) -> graph2.draw(s)); } private class keyhandler implements keylistener { @override public void keypressed(final keyevent event) { final int keycode = event.getkeycode(); int dex = 0; if (event.isshiftdown()) { dex = large_increment; } else { dex = small_increment; } switch (keycode) { case keyevent.vk_left: add(-dex, 0); break; case keyevent.vk_right: add(dex, 0); break; case keyevent.vk_up: add(0, -dex); break; case keyevent.vk_down: add(0, dex); break; default: break; } } @override public void keyreleased(final keyevent event) {} @override public void keytyped(final keyevent event) { char keychar = event.getkeychar(); int dex; if (character.isuppercase(keychar)) { dex = large_increment; keychar = character.tolowercase(keychar); } else { dex = small_increment; } switch (keychar) { case 'h': add(-dex, 0); break; case 'l': add(dex, 0); break; case 'k': add(0, -dex); break; case 'j': add(0, dex); break; default: break; } } }
Comments
Post a Comment