NickMeinholdItem v0.2


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // added picked() method that returns true if the item was selected with a mouse click
  2.  
  3. package project;
  4.  
  5. import java.awt.Image;
  6. import java.io.File;
  7.  
  8. import javax.imageio.ImageIO;
  9.  
  10. /**
  11.  *
  12.  * @author Mihir
  13.  */
  14. public class Item {
  15.  
  16. private boolean visible;
  17. private String itemtype;
  18. private String tooltip;
  19. private String imgpath;
  20. // added these two for drawing the item (Nick)
  21. private Image image;
  22. private Position pos;
  23.  
  24. /** Creates a new instance of Item */
  25. public Item() {
  26. pos = new Position(); // added this so position is not null, otherwise throws null pointer exception (Nick)
  27. // please add super(); to all subclasses
  28. }
  29. // added copy constructor for creating a new item to add to the canvas_items array list (Nick)
  30. public Item(Item original) {
  31. this.visible = original.visible;
  32. this.itemtype = new String(original.itemtype);
  33. this.tooltip = new String(original.tooltip);
  34. this.imgpath = new String(original.imgpath);
  35. this.image = original.image;
  36. this.pos = new Position(original.pos);
  37. }
  38.  
  39. public boolean isVisible() {
  40. return visible;
  41. }
  42.  
  43. public void setVisible(boolean visible) {
  44. this.visible = visible;
  45. }
  46.  
  47. public String getItemtype() {
  48. return itemtype;
  49. }
  50.  
  51. public void setItemtype(String itemtype) {
  52. this.itemtype = itemtype;
  53. }
  54.  
  55. public String getTooltip() {
  56. return tooltip;
  57. }
  58.  
  59. public void setTooltip(String tooltip) {
  60. this.tooltip = tooltip;
  61. }
  62.  
  63. public String getImgpath() {
  64. return imgpath;
  65. }
  66.  
  67. public void setImgpath(String imgpath) {
  68. this.imgpath = imgpath;
  69. // added this so here so that it's needs to be called once (Nick)
  70. try{
  71. this.image = ImageIO.read(new File (imgpath));
  72. }
  73. catch(Exception e) {
  74. System.out.println("Problem reading iage path" + e);
  75. }
  76. }
  77. // added these next three methods to draw the item (Nick)
  78. public Image getImg() {
  79. return image;
  80. }
  81. public void setPos(int x, int y) {
  82. this.pos.x = x;
  83. this.pos.y = y;
  84. }
  85. Position getPos() {
  86. return this.pos;
  87. }
  88. boolean picked(int x, int y) {
  89. if(x > this.pos.x && x < (this.pos.x+image.getWidth(null)) && y > this.pos.y && y < (this.pos.y+image.getHeight(null)))
  90. return true;
  91. else
  92. return false;
  93. }
  94.  
  95. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.