Advertisement
TheBat

GE

Apr 11th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.Rectangle;
  5. import java.util.ArrayList;
  6.  
  7. import org.tribot.script.methods.Colour;
  8. import org.tribot.script.methods.General;
  9.  
  10. public class DTM2 {
  11. private final Color[] rgb;
  12. private final Point[] loc;
  13. private Rectangle box;
  14. private final String name;
  15. static ArrayList<Rectangle> dtmLocations = new ArrayList<Rectangle>();
  16.  
  17. public DTM2(final Point[] loc, final Color[] color, final String name) {
  18. this.loc = loc;
  19. this.rgb = color;
  20. this.name = name;
  21. box = createRectangleFromPoints(loc);
  22. }
  23.  
  24. public DTM2(final Point[] loc, final Color[] color) {
  25. this(loc, color, "DTM");
  26. }
  27.  
  28. /**
  29. * Given a set of points create a rectangle that encases all of those points
  30. * @param points : set of points to create a rectangle with
  31. * @return : A rectangle that encloses all points
  32. */
  33. private Rectangle createRectangleFromPoints(final Point[] points) {
  34. Point minPoint = new Point(800, 600), maxPoint = new Point(0, 0);
  35. for(int i = 0; i < loc.length; i++) {
  36. if(points[i].x < minPoint.x) minPoint.x = points[i].x;
  37. if(points[i].x > maxPoint.x) maxPoint.x = points[i].x;
  38. if(points[i].y < minPoint.y) minPoint.y = points[i].y;
  39. if(points[i].y > maxPoint.y) maxPoint.y = points[i].y;
  40. }
  41. return new Rectangle(minPoint.x, minPoint.y, (maxPoint.x - minPoint.x) + 1, (maxPoint.y - minPoint.y) + 1);
  42. }
  43.  
  44. /**
  45. * Looks for the DTM in the original spot specified
  46. * @param tol : tolerance between each RGB value when checking if colors are the same
  47. * @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
  48. */
  49. public boolean findOriginalDTM(final int tol) {
  50. return checkAllPoints(loc[0], tol);
  51. }
  52.  
  53. /**
  54. * Finds all DTMs on the game screen stores and stores there location to the dtmLocations ArrayList.
  55. * @param tol : tolerance between each RGB value when checking if colors are the same
  56. * @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
  57. */
  58. public boolean findDTMS(final int tol) {
  59. return findDTMS(tol, new Rectangle(0, 0, 765, 503));
  60. }
  61.  
  62. /**
  63. * Searches for DTMs in in the rectangle passed and stores all DTMs found to the dtmLocations ArrayList.
  64. * @param tol : tolerance between each RGB value when checking if colors are the same
  65. * @param rec : Rectangle to search for the DTM to be in.
  66. * @return <tt>true</tt> if a DTM was found; otherwise <tt>false</tt>
  67. */
  68. public boolean findDTMS(final int tol, final Rectangle rec) {
  69. boolean foundDTM = false;
  70. dtmLocations.clear();
  71.  
  72. for(int y = rec.y; y < rec.y + rec.height; y++) {
  73. for(int x = rec.x; x < rec.x + rec.width; x++) {
  74. if(checkColor(Colour.getColour(x, y), rgb[0], tol)) {
  75. final Rectangle curbox = new Rectangle(x - (loc[0].x - box.x), y - (loc[0].y - box.y), box.width, box.height);
  76. if(checkAllPoints(new Point(x, y), tol)) {
  77. dtmLocations.add(curbox);
  78. foundDTM = true;
  79. }
  80. }
  81. }
  82. }
  83. return foundDTM;
  84. }
  85.  
  86. /**
  87. * Grabs the DTM specified by index in the dtms found array
  88. * @param i : index in the dtm array to grab
  89. * @return : the center point of the rectangle for the DTM or null if not found
  90. */
  91. public Point getDTM(final int i) {
  92. return dtmLocations.isEmpty() ? null : new Point(dtmLocations.get(i).x + (dtmLocations.get(i).width / 2), dtmLocations.get(i).y + (dtmLocations.get(i).height / 2));
  93. }
  94. /**
  95. * Returns the all of the dtms in the list
  96. * @return : the center point of the rectangle for the DTM or null if not found
  97. */
  98. public ArrayList<Rectangle> getList() {
  99. return dtmLocations;
  100. }
  101. /**
  102. *
  103. * @param gameS game screen colors array
  104. * @param locM location of point that matches the first color in the array list colorB
  105. * @return true if all colors match
  106. */
  107. private boolean checkAllPoints(Point locM, final int tol) {
  108. try {
  109. final Point locI = loc[0];
  110. for(int i = 0; i < loc.length; i++) {
  111. // we can come back and shorten this, but for now this is to make sure we make no mistakes
  112. final Color initialColor = rgb[i];
  113. final Point initialColorPoint = loc[i];
  114. final int xshift = initialColorPoint.x - locI.x;
  115. final int yshift = initialColorPoint.y - locI.y;
  116. final Point pointToCheck = new Point(locM.x + xshift, locM.y + yshift);
  117. final Color colorToCheck = Colour.getColour(pointToCheck);
  118.  
  119. if(!checkColor(initialColor, colorToCheck, tol)) return false;
  120. }
  121. return true;
  122. } catch (Exception e) {
  123. return false;
  124. }
  125. }
  126.  
  127. /**
  128. * Checks to see if the 2 colors passed match
  129. * @param c : Color 1
  130. * @param c2 : Color 2
  131. * @param tol : tolerance between each RGB value when checking if colors are the same
  132. * @return <tt>true</tt> if colors match; otherwise <tt>false</tt>
  133. */
  134. private boolean checkColor(final Color c, final Color c2, final int Tol) {
  135. return (checkColor(c.getRed(), c2.getRed(), Tol)
  136. && checkColor(c.getGreen(), c2.getGreen(), Tol) && checkColor(c.getBlue(),
  137. c2.getBlue(), Tol));
  138. }
  139.  
  140. /**
  141. * Checks to see if the 2 RGB values match
  142. * @param RGB1 : RGB value 1
  143. * @param RGB2 : RGB value 2
  144. * @param tol : tolerance between the RGB values when checking if they are the same
  145. * @return <tt>true</tt> if RGB values match; otherwise <tt>false</tt>
  146. */
  147. private boolean checkColor(final int RGB1, final int RGB2, final int Tol) {
  148. return Math.abs(RGB1 - RGB2) < Tol;
  149. }
  150.  
  151. public void drawDTMs(Graphics g) {
  152. g.setColor(Color.RED);
  153. for(int i = 0; i < dtmLocations.size(); i++) {
  154. drawX(g, dtmLocations.get(i));
  155. }
  156. }
  157.  
  158. private void drawX(Graphics g, Rectangle rec) {
  159. g.setColor(Color.RED);
  160. g.drawLine(rec.x, rec.y, rec.x + rec.width, rec.y + rec.height);
  161. g.drawLine(rec.x, rec.y + rec.height, rec.x + rec.width, rec.y);
  162. }
  163.  
  164. public void drawDTMSquares(Graphics g, final Color c1, boolean addname) throws Exception {
  165. for(int i = 0; i < dtmLocations.size(); i++) {
  166. Rectangle dtm = dtmLocations.get(i);
  167. g.setColor(c1);
  168. g.drawRect(dtm.x, dtm.y, dtm.width, dtm.height);
  169. if(!addname) return;
  170. g.setColor(Color.BLACK);
  171. g.fill3DRect(dtm.x + dtm.width - 33, dtm.y + dtm.height + 4, 41, 11, true);
  172. g.setColor(Color.WHITE);
  173. g.drawString(name + " " + i, dtm.x + dtm.width - 30, dtm.y + dtm.height + 13);
  174. }
  175. }
  176.  
  177. public void fancydraw(Graphics g, Color recColor, boolean addname, boolean addX) {
  178. try {
  179. if(dtmLocations.isEmpty()) return;
  180. g.setFont(g.getFont().deriveFont((float) 9));
  181. for(int i = 0; i < dtmLocations.size(); i++) {
  182. drawDTMSquares(g, recColor, addname);
  183. }
  184. if(addX) drawX(g, dtmLocations.get(0));
  185. } catch (Exception e) {
  186. General.println("Exception occured in paint");
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement