Created
April 4, 2012 23:34
-
-
Save CalebWhiting/2306578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.dang.api.script.methods; | |
import org.runedream.api.methods.Game; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.util.LinkedList; | |
import java.util.List; | |
/** | |
* Note: It is faster if the piece size is bigger. It is highly recommended to make the Grid variable final. | |
* | |
* @author Caleb (Dang) | |
* @version: 0.9 | |
*/ | |
public class Grid { | |
private final List<Rectangle> rectangles = new LinkedList<Rectangle>(); | |
private final int pieceSize; | |
private final Rectangle bounds; | |
/** | |
* @param area_bounds The bounds in which to create the grid. | |
* @param piece_size The size of each piece of the grid. | |
*/ | |
public Grid(final Rectangle area_bounds, final int piece_size) { | |
this.bounds = area_bounds; | |
this.pieceSize = piece_size; | |
this.setRectangles(); | |
} | |
/** | |
* @param pieceSize the size of each piece of the grid. | |
*/ | |
public Grid(final int pieceSize) { | |
this(new Rectangle(0, 0, Game.getImage().getWidth(), Game.getImage().getHeight()), pieceSize); | |
} | |
/** | |
* Sets the grid. | |
*/ | |
private void setRectangles() { | |
this.rectangles.clear(); | |
for (int x = 0; x < this.bounds.width; x += this.pieceSize) { | |
if (x + this.pieceSize > bounds.width) { | |
break; | |
} | |
for (int y = 0; y < this.bounds.height; y += this.pieceSize) { | |
if (y + this.pieceSize > this.bounds.height) { | |
break; | |
} | |
this.rectangles.add(new Rectangle(x, y, this.pieceSize, this.pieceSize)); | |
} | |
} | |
} | |
/** | |
* @param index the index of the piece to get | |
* | |
* @return the piece with an index value of 'index' | |
*/ | |
public Rectangle getPiece(final int index) { | |
return this.rectangles.get(index); | |
} | |
/** | |
* @param rects add these to the grid. | |
*/ | |
public void add(final Rectangle... rects) { | |
for (final Rectangle rectangle : rects) { | |
this.rectangles.add(rectangle); | |
} | |
} | |
/** | |
* @param index the piece index value to remove. | |
*/ | |
public void remove(final int index) { | |
this.rectangles.remove(index); | |
} | |
/** | |
* @return the list of Rectangles in the grid. | |
*/ | |
public List<Rectangle> getPieces() { | |
return this.rectangles; | |
} | |
/** | |
* @return an array of Rectangles in the grid | |
*/ | |
public Rectangle[] getPiecesArray() { | |
return this.rectangles.toArray(new Rectangle[this.rectangles.size()]); | |
} | |
/** | |
* @param index the index value of the pieces | |
* | |
* @return an array of colors in the piece | |
*/ | |
public Color[] getColors(final int index) { | |
final BufferedImage image = Game.getImage(); | |
final Rectangle rect = this.getPiece(index); | |
final List<Color> colors = new LinkedList<Color>(); | |
for (int x = 0; x < rect.width; x += 1) { | |
for (int y = 0; y < rect.height; y += 1) { | |
colors.add(new Color(image.getRGB(x, y))); | |
} | |
} | |
return colors.toArray(new Color[colors.size()]); | |
} | |
/** | |
* @param indexes the index values of the pieces | |
* | |
* @return an array of colors in the pieces | |
*/ | |
public Color[] getColors(final int... indexes) { | |
final List<Color> colors = new LinkedList<Color>(); | |
for (final int index : indexes) { | |
final Color[] index_colors = this.getColors(index); | |
for (final Color c : index_colors) { | |
colors.add(c); | |
} | |
} | |
return colors.toArray(new Color[colors.size()]); | |
} | |
/** | |
* @param index the index value of the piece to check | |
* @param tolerance the tolerance to allow | |
* @param color the color to search for | |
* | |
* @return if the piece contains 'color' with a tolerance of 'tolerance' | |
*/ | |
public boolean contains(final int index, final int tolerance, final Color color) { | |
final BufferedImage image = getImageAt(index); | |
final int width = image.getWidth(); | |
final int height = image.getHeight(); | |
for (int x = 0; x < width; x += 1) { | |
for (int y = 0; y < height; y += 1) { | |
if (isColor(color, new Color(image.getRGB(x, y)), tolerance)) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* @param index the index value of the piece to check | |
* @param color the color to search for | |
* | |
* @return if the piece contains 'color' | |
*/ | |
public boolean contains(final int index, final Color color) { | |
return this.contains(index, 0, color); | |
} | |
/** | |
* @param index the index value of the piece to check | |
* @param tolerance the tolerance to allow | |
* @param colors the colors to search for | |
* | |
* @return if the piece contains 'colors' with a tolerance of 'tolerance' | |
*/ | |
public boolean contains(final int index, int tolerance, final Color... colors) { | |
for (final Color color : colors) { | |
if (! this.contains(index, tolerance, color)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* @param index the index value of the piece to check | |
* @param colors the colors to search for | |
* | |
* @return if the piece contains 'colors' | |
*/ | |
public boolean contains(final int index, final Color... colors) { | |
return this.contains(index, 0, colors); | |
} | |
/** | |
* @param index the index value of the piece to check | |
* @param point the Point to check for | |
* | |
* @return if the piece contains 'point' | |
*/ | |
public boolean contains(final int index, final Point point) { | |
return getPiece(index).contains(point); | |
} | |
/** | |
* @param index the index value of the piece to check | |
* @param points the Point's to check for | |
* | |
* @return if the piece contains all 'points' | |
*/ | |
public boolean contains(final int index, final Point... points) { | |
final Rectangle piece = getPiece(index); | |
for (final Point point : points) { | |
if (! piece.contains(point)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* @param tolerance the tolerance to allow | |
* @param colors the colors to search for | |
* | |
* @return an array of pieces containing all 'colors' with a tolerance of 'tolerance' | |
*/ | |
public Rectangle[] getContaining(final int tolerance, final Color... colors) { | |
final List<Rectangle> pieces = new LinkedList<Rectangle>(); | |
for (int index = 0; index < this.rectangles.size(); index += 1) { | |
if (this.contains(index, tolerance, colors)) { | |
pieces.add(this.rectangles.get(index)); | |
} | |
} | |
return pieces.toArray(new Rectangle[pieces.size()]); | |
} | |
/** | |
* @param colors the colors to search for | |
* | |
* @return an array of pieces containing all 'colors' | |
*/ | |
public Rectangle[] getContaining(final Color... colors) { | |
return this.getContaining(0, colors); | |
} | |
/** | |
* @param point the point the piece must contain | |
* | |
* @return the piece containing 'point', null if no pieces contain 'point' | |
*/ | |
public Rectangle getContaining(final Point point) { | |
for (final Rectangle rectangle : rectangles) { | |
if (rectangle.contains(point)) { | |
return rectangle; | |
} | |
} | |
return null; | |
} | |
/** | |
* @param points the points the piece must contain | |
* | |
* @return the piece containing all 'points', null if not found | |
*/ | |
public Rectangle getContaining(final Point... points) { | |
for (int index = 0; index < rectangles.size(); index += 1) { | |
if (contains(index, points)) { | |
return getPiece(index); | |
} | |
} | |
return null; | |
} | |
/** | |
* @return the size of each piece | |
*/ | |
public int getPieceSize() { | |
return this.pieceSize; | |
} | |
/** | |
* @param index the index value of the piece | |
* | |
* @return an image of the piece | |
*/ | |
public BufferedImage getImageAt(final int index) { | |
final Rectangle bound = getPiece(index); | |
final BufferedImage game = Game.getImage(); | |
return game.getSubimage(bound.x, bound.y, bound.width, bound.height); | |
} | |
/** | |
* @param indexes the index values of the pieces | |
* | |
* @return an array of images of the pieces | |
*/ | |
public BufferedImage[] getImageAt(final int... indexes) { | |
final List<BufferedImage> images = new LinkedList<BufferedImage>(); | |
for (final int index : indexes) { | |
images.add(getImageAt(index)); | |
} | |
return images.toArray(new BufferedImage[images.size()]); | |
} | |
/** | |
* @param g Graphics2D Object | |
*/ | |
public void draw(final Graphics2D g) { | |
for (final Rectangle r : this.rectangles) { | |
g.drawRect(r.x, r.y, r.width, r.height); | |
} | |
} | |
/** | |
* @param g Graphics Object | |
*/ | |
public void draw(final Graphics g) { | |
for (final Rectangle r : this.rectangles) { | |
g.drawRect(r.x, r.y, r.width, r.height); | |
} | |
} | |
/** | |
* @param g Graphics Object | |
*/ | |
public void drawPoints(final Graphics g) { | |
for (final Rectangle r : this.rectangles) { | |
final int x = r.x, y = r.y; | |
g.drawLine(x, y, x, y); | |
} | |
} | |
/** | |
* @param g Graphics2D Object | |
*/ | |
public void drawPoints(final Graphics2D g) { | |
for (final Rectangle r : this.rectangles) { | |
final int x = r.x, y = r.y; | |
g.drawLine(x, y, x, y); | |
} | |
} | |
/** | |
* @param colorOne the first color to compare | |
* @param colorTwo the color to compare 'colorOne' to | |
* @param tolerance the tolerance to allow between each color | |
* | |
* @return if the color's RGS values are within the tolerance given | |
*/ | |
private static boolean isColor(final Color colorOne, final Color colorTwo, final int tolerance) { | |
final int[] one = { colorOne.getRed(), colorOne.getGreen(), colorOne.getBlue() }; | |
final int[] two = { colorTwo.getRed(), colorTwo.getGreen(), colorTwo.getBlue() }; | |
return isWithinTolerance(one[0], two[0], tolerance) && isWithinTolerance(one[1], two[1], tolerance) && isWithinTolerance(one[2], two[2], tolerance); | |
} | |
/** | |
* @param num the first number to compare | |
* @param num2 the number to compare to 'num' | |
* @param tolerance the amount to allow between the values | |
* | |
* @return if num and num2 are within the given tolerance of each other | |
*/ | |
private static boolean isWithinTolerance(final int num, final int num2, final int tolerance) { | |
return num <= num2 + tolerance && num >= num2 - tolerance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment