Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. public PaintingPrinter(World x){
  2. hsb = new Turtle(x);
  3. hsb.setWidth(0);
  4. hsb.setHeight(0);
  5. hsb.setVisible(false);
  6. hsb.setPenWidth(1);
  7. k = new Turtle(x);
  8. k.setVisible(false);
  9. k.setWidth(0);
  10. k.setHeight(0);
  11. k.setPenWidth(1);
  12. }
  13. //methods
  14. /**
  15. * moves turtle object to a point
  16. * @param obj target turtle object to move
  17. * @param x x-coordinate of desired point
  18. * @param y y-coordinate of desired point
  19. */
  20. public void goTo(Turtle obj, int x, int y){
  21. int x1 = obj.getXPos();
  22. int y1 = obj.getYPos();
  23. obj.penUp();
  24. obj.setHeading(0);
  25. int dx = x-x1;
  26. int dy = y-y1;
  27. //translate along x
  28. obj.turnRight();
  29. if (dx>0) obj.forward(dx);
  30. else obj.backward(dx);
  31. //translate along y
  32. obj.turnLeft();
  33. if(dy>0) obj.forward(dy);
  34. else obj.backward(dy);
  35. }
  36.  
  37. /**
  38. * paints a picture using a black and color turtle, based on the input matrix.
  39. * @param in array of all pixels and the color that they are supposed to be
  40. */
  41. public void paint(Color[][] in){
  42. for(int i = 0; i < in.length; i++){
  43. for(int j = 0; j<in[i].length; j++){
  44. if(in[i][j] == Color.BLACK){
  45. goTo(k, i, j);
  46. k.penDown();
  47. k.penUp();
  48. } else{
  49. hsb.setPenColor(in[i][j]);
  50. goTo(hsb, i, j);
  51. hsb.penDown();
  52. hsb.penUp();
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement