import java.awt.*; import javax.swing.*; public class WrappedImage { private Image image; private int wW; private int wH; private boolean debug; public WrappedImage(Image img) { image = img; wW = img.getWidth(null); wH = img.getHeight(null); debug = false; } public WrappedImage(Image img, boolean debug) { image = img; wW = img.getWidth(null); wH = img.getHeight(null); this.debug = debug; } //draws the image with the given viewing width and height and where //(sX, sY) is the location of the ship within the world public void draw(Graphics g, int vW, int vH, int sX, int sY) { draw(g, 0, 0, vW, vH, sX, sY); } //draws the image with the given viewing width and height and where //(sX, sY) is the location of the ship within the world //(vX, vY) is the location of the viewing screen withint the full scren window public void draw(Graphics g, int vX, int vY, int vW, int vH, int sX, int sY) { int[] xDistances = new int[]{vW/2 - sX, sX, wW-sX, vW/2-(wW - sX)}; int[] yDistances = new int[]{vH/2 - sY, sY, wH-sY, vH/2-(wH - sY)}; if(xDistances[0] < 0) xDistances[0] = 0; if(xDistances[1] > vW/2) xDistances[1] = vW/2; if(xDistances[2] > vW/2) xDistances[2] = vW/2; if(xDistances[3] < 0) xDistances[3] = 0; if(yDistances[0] < 0) yDistances[0] = 0; if(yDistances[1] > vH/2) yDistances[1] = vH/2; if(yDistances[2] > vH/2) yDistances[2] = vH/2; if(yDistances[3] < 0) yDistances[3] = 0; int cummulativeX = 0; for(int i = 0; i < xDistances.length; i++) { int worldLocX = sX - vW/2; if(worldLocX < 0) worldLocX += wW; int cummulativeY = 0; for(int j = 0; j < yDistances.length; j++) { int worldLocY = sY - vH/2; if(worldLocY < 0) worldLocY += wH; g.drawImage(image, vX + cummulativeX, vY + cummulativeY, vX + cummulativeX + xDistances[i], vY + cummulativeY + yDistances[j], (worldLocX + cummulativeX) % wW, (worldLocY + cummulativeY) % wH, (worldLocX + cummulativeX) % wW + xDistances[i], (worldLocY + cummulativeY) % wH + yDistances[j], null); if(debug) { ((Graphics2D)g).setStroke(new BasicStroke(4)); g.setColor(Color.BLACK); g.drawString(j + " " + i, vX + cummulativeX + 20, vY+cummulativeY + 20); g.drawRect(vX + cummulativeX, vY + cummulativeY, vX + cummulativeX + xDistances[i], vY + cummulativeY + yDistances[j]); ((Graphics2D)g).setStroke(new BasicStroke(1)); } cummulativeY += yDistances[j]; } cummulativeX += xDistances[i]; } } }