/** loads the sprites to be used for the bots */ public static void loadSprites() { try { mainImageEast = ImageIO.read(new File("char/archer_east.png")); mainImageWest = ImageIO.read(new File("char/archer_west.png")); north = new BufferedImage[frameCount]; south = new BufferedImage[frameCount]; east = new BufferedImage[frameCount]; west = new BufferedImage[frameCount]; north_east = new BufferedImage[frameCount]; north_west = new BufferedImage[frameCount]; south_east = new BufferedImage[frameCount]; south_west = new BufferedImage[frameCount]; for (int i = 0; i < frameCount; i++) { north[i] = mainImageEast.getSubimage(0 * width, height * i, width, height); north_east[i] = mainImageEast.getSubimage(1 * width, height * i, width, height); east[i] = mainImageEast.getSubimage(2 * width, height * i, width, height); south_east[i] = mainImageEast.getSubimage(3 * width, height * i, width, height); south[i] = mainImageEast.getSubimage(4 * width, height * i, width, height); north_west[i] = mainImageWest.getSubimage(3 * width, height * i, width, height); west[i] = mainImageWest.getSubimage(2 * width, height * i, width, height); south_west[i] = mainImageWest.getSubimage(1 * width, height * i, width, height); } } catch (Exception e) { System.out.println("Spritesheet Not Found!"); } }
private void drawSelection(Graphics2D g2d) { if (srcx != destx || srcy != desty) { int x1 = (srcx < destx) ? srcx : destx; int y1 = (srcy < desty) ? srcy : desty; int x2 = (srcx > destx) ? srcx : destx; int y2 = (srcy > desty) ? srcy : desty; rectSelection.x = x1; rectSelection.y = y1; rectSelection.width = (x2 - x1) + 1; rectSelection.height = (y2 - y1) + 1; if (rectSelection.width > 0 && rectSelection.height > 0) { g2d.drawImage(scr_img.getSubimage(x1, y1, x2 - x1 + 1, y2 - y1 + 1), null, x1, y1); } g2d.setColor(selFrameColor); g2d.setStroke(bs); g2d.draw(rectSelection); int cx = (x1 + x2) / 2; int cy = (y1 + y2) / 2; g2d.setColor(selCrossColor); g2d.setStroke(_StrokeCross); g2d.drawLine(cx, y1, cx, y2); g2d.drawLine(x1, cy, x2, cy); if (Screen.getNumberScreens() > 1) { drawScreenFrame(g2d, srcScreenId); } } }
public void generarFondo(Component componente) { boolean dibujarFondo = false; Rectangle med = this.getBounds(); Rectangle areaDibujo = this.getBounds(); BufferedImage tmp; GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); if (Principal.fondoBlur) { dibujarFondo = true; } if (dibujarFondo) { JRootPane root = SwingUtilities.getRootPane(this); blurBuffer = GraphicsUtilities.createCompatibleImage(Principal.sysAncho, Principal.sysAlto); Graphics2D g2 = blurBuffer.createGraphics(); g2.setClip(med); blurBuffer = blurBuffer.getSubimage(med.x, med.y, med.width, med.height); ((Escritorio) Principal.getEscritorio()).getFrameEscritorio().paint(g2); g2.dispose(); backBuffer = blurBuffer; // blurBuffer = toGrayScale(blurBuffer); blurBuffer = GraphicsUtilities.createThumbnailFast(blurBuffer, getWidth() / 2); blurBuffer = new GaussianBlurFilter(4).filter(blurBuffer, null); g2 = (Graphics2D) blurBuffer.getGraphics(); g2.setColor(new Color(0, 0, 0, 195)); g2.fillRect(0, 0, Principal.sysAncho, Principal.sysAlto); listo = true; } }
public void loadTiles(String s) { try { tileset = ImageIO.read(new File(s)); int numTilesAcross = (tileset.getWidth() + 1) / (tileSize + 1); tiles = new Tile[2][numTilesAcross]; BufferedImage subimage; for (int col = 0; col < numTilesAcross; col++) { subimage = tileset.getSubimage(col * tileSize + col, 0, tileSize, tileSize); tiles[0][col] = new Tile(subimage, false); subimage = tileset.getSubimage(col * tileSize + col, tileSize + 1, tileSize, tileSize); tiles[1][col] = new Tile(subimage, true); } } catch (Exception e) { e.printStackTrace(); } }
// compare the two images in this object. public void compare() { // setup change display image imgc = imageToBufferedImage(img1); Graphics2D gc = imgc.createGraphics(); gc.setColor(Color.RED); // convert to gray images. img1 = imageToBufferedImage(GrayFilter.createDisabledImage(img1)); img2 = imageToBufferedImage(GrayFilter.createDisabledImage(img2)); // how big are each section int blocksx = (int) (img1.getWidth() / comparex); int blocksy = (int) (img1.getHeight() / comparey); // set to a match by default, if a change is found then flag non-match this.match = true; // loop through whole image and compare individual blocks of images for (int y = 0; y < comparey; y++) { if (debugMode > 0) System.out.print("|"); for (int x = 0; x < comparex; x++) { int b1 = getAverageBrightness( img1.getSubimage(x * blocksx, y * blocksy, blocksx - 1, blocksy - 1)); int b2 = getAverageBrightness( img2.getSubimage(x * blocksx, y * blocksy, blocksx - 1, blocksy - 1)); int diff = Math.abs(b1 - b2); if (diff > factorA) { // the difference in a certain region has passed the threshold value of // factorA // draw an indicator on the change image to show where change was detected. gc.drawRect(x * blocksx, y * blocksy, blocksx - 1, blocksy - 1); this.match = false; } if (debugMode == 1) System.out.print((diff > factorA ? "X" : " ")); if (debugMode == 2) System.out.print(diff + (x < comparex - 1 ? "," : "")); } if (debugMode > 0) System.out.println("|"); } }
private BufferedImage cropSelection() { int w = rectSelection.width, h = rectSelection.height; if (w <= 0 || h <= 0) { return null; } BufferedImage crop = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D crop_g2d = crop.createGraphics(); try { crop_g2d.drawImage(scr_img.getSubimage(rectSelection.x, rectSelection.y, w, h), null, 0, 0); } catch (RasterFormatException e) { Debug.error(e.getMessage()); } crop_g2d.dispose(); return crop; }
public ImageFont(String pathToImage, int numCols, int numRows) { try { tilesImg = convertToARGB(ImageIO.read(new File(pathToImage))); this.imgW = tilesImg.getWidth(); this.imgH = tilesImg.getHeight(); this.numCols = numCols; this.numRows = numRows; this.numTiles = numCols * numRows; this.tW = this.imgW / this.numCols; this.tH = this.imgH / this.numRows; this.tiles = new BufferedImage[this.numTiles]; for (int i = 0; i < numCols; i++) { for (int j = 0; j < numRows; j++) { tiles[(numCols * j) + i] = tilesImg.getSubimage(i * tW, j * tH, tW, tH); } } } catch (IOException e) { System.out.println("Couldn't load the image: " + e); } }
public void insertBuilding(int i, int j, int index) { if (!tiles[i][j].getValue().equals("")) { JOptionPane.showMessageDialog(null, "There is already a building here"); return; } String temp = bb.get(index).getText(); String[] parts = temp.split("-"); int newQ = Integer.parseInt(parts[1]) - 1; if (newQ < 0) return; Building tB = bList.get(0); for (int b = 0; b < bList.size(); b++) { if (bList.get(b).getName().equals(parts[0])) { tB = bList.get(b); // System.out.println("here"); break; } } Image image = null; BufferedImage buffered; try { image = ImageIO.read(new File("images/" + tB.getName().replace(" ", "_") + ".png")); System.out.println("Loaded image"); } catch (IOException e) { System.out.println("Failed to load image"); } buffered = (BufferedImage) image; for (int c = 0; c < tB.getQWidth(); c++) { for (int r = 0; r < tB.getQHeight(); r++) { if (i + c == 40 || j + r == 40) { JOptionPane.showMessageDialog(null, "Placing a building here would be out of bounds"); return; } if (!tiles[i + c][j + r].getValue().equals("")) { JOptionPane.showMessageDialog(null, "Placing a building here will result to a overlap"); return; } } } double w = buffered.getWidth() / tB.getQWidth(); double h = buffered.getHeight() / tB.getQHeight(); for (int c = 0; c < tB.getQWidth(); c++) { for (int r = 0; r < tB.getQHeight(); r++) { tiles[i + c][j + r].setBackground(new Color(51, 204, 51)); tiles[i + c][j + r].setIcon( new ImageIcon( resize( buffered.getSubimage((int) w * r, (int) h * c, (int) w, (int) h), tiles[i + c][j + r].getWidth(), tiles[i + c][j + r].getHeight()))); String tValue = (c == 0 && r == 0) ? tB.getName() : i + "-" + j; // System.out.println(tValue); tiles[i + c][j + r].setValue(tValue); } } // tiles[i][j].setBackground(Color.BLUE); bb.get(index).setText(parts[0] + "-" + newQ); }
private BufferedImage getSpecificImage( int x, int y, int w, int h, int imgIndexX, int imgIndexY, BufferedImage buffImg, int zoom, int cachedZoom, GMapListener listener, boolean localFilesOnly) { int xIndex = x / GDataSource.sourceSize.width; int yIndex = y / GDataSource.sourceSize.height; xIndex += imgIndexX; yIndex += imgIndexY; BufferedImage image = null; if (!localFilesOnly || getGDataSource().isCached(xIndex, yIndex, zoom)) image = getIndexedImage(xIndex, yIndex, zoom, cachedZoom, listener); int xCoord = x % GDataSource.sourceSize.width; int yCoord = y % GDataSource.sourceSize.height; // Checks for invalid xCoord and yCoord if (xCoord < 0) { xCoord = 0; } if (yCoord < 0) { yCoord = 0; } // get info about the image Dimension imageSize = new Dimension(getGDataSource().sourceSize.width, getGDataSource().sourceSize.height); // find the width of what we CAN paint int initPaintWidth = imageSize.width - xCoord; int initPaintHeight = imageSize.height - yCoord; int paintWidth = initPaintWidth; int paintHeight = initPaintHeight; int rowImages = numOfRows(x, y, h, zoom, cachedZoom); int colImages = numOfCols(x, y, w, zoom, cachedZoom); if (imgIndexX >= colImages || imgIndexY >= rowImages) { return null; } int xImage = 0; int yImage = 0; int xInitCoord = xCoord; int yInitCoord = yCoord; if (imgIndexX > 0) { xImage = initPaintWidth + (imgIndexX - 1) * imageSize.width; xCoord = 0; if (imgIndexX < (colImages - 1)) { paintWidth = imageSize.width; } else { paintWidth = w - ((colImages - 2) * imageSize.width) - (imageSize.width - xInitCoord); } } if (imgIndexY > 0) { yImage = initPaintHeight + (imgIndexY - 1) * imageSize.height; yCoord = 0; if (imgIndexY < (rowImages - 1)) { paintHeight = imageSize.height; } else { paintHeight = h - ((rowImages - 2) * imageSize.height) - (imageSize.height - yInitCoord); } } if (buffImg != null) { Graphics2D g = (Graphics2D) buffImg.getGraphics(); if (image != null) { // System.out.println(xCoord + ":" + yCoord + ":" + paintWidth + ":" + paintHeight); g.drawImage( image.getSubimage(xCoord, yCoord, paintWidth, paintHeight), xImage, yImage, paintWidth, paintHeight, null); } else { Composite originalComposite = g.getComposite(); g.setComposite(opacity40); g.setColor(Color.BLACK); g.fillRect(xImage, yImage, paintWidth, paintHeight); g.setComposite(originalComposite); } } return buffImg; }
protected void paintToImage(Component c, Graphics gr, int width, int height, Object args) { // Cast Graphics to Graphics2D // Workaround for Java 1.4 and 1.4 on Mac OS X 10.4. We create a new // Graphics object instead of just casting the provided one. This is // because drawing texture paints appears to confuse the Graphics object. Graphics2D g = (Graphics2D) gr.create(); // Convert image to buffered image (and keep the buffered image). image = Images.toBufferedImage(image); BufferedImage img = (BufferedImage) image; // Set some variables for easy access of insets and image size int top = imageInsets.top; int left = imageInsets.left; int bottom = imageInsets.bottom; int right = imageInsets.right; int imgWidth = img.getWidth(); int imgHeight = img.getHeight(); // Optimisation: Draw image directly if it fits into the component if (fillContentArea) { if (width == imgWidth && height == imgHeight) { g.drawImage(img, 0, 0, c); g.dispose(); return; } } // Optimisation: Remove insets, if image width or image height fits if (width == imgWidth) { left = imgWidth; right = 0; } if (height == imgHeight) { top = imgHeight; bottom = 0; } // Adjust insets if component is too small if (width < left + right) { left = Math.min(left, width / 2); // Math.max(0, left + (width - left - right) / 2); right = width - left; } if (height < top + bottom) { top = Math.min(top, height / 2); // Math.max(0, top + (height - top - bottom) / 2); bottom = height - top; } // Draw the Corners if (top > 0 && left > 0) { g.drawImage(img, 0, 0, left, top, 0, 0, left, top, c); } if (top > 0 && right > 0) { // g.fillRect(x+width-right, y, x+width, y+top); g.drawImage(img, width - right, 0, width, top, imgWidth - right, 0, imgWidth, top, c); } if (bottom > 0 && left > 0) { g.drawImage(img, 0, height - bottom, left, height, 0, imgHeight - bottom, left, imgHeight, c); } if (bottom > 0 && right > 0) { g.drawImage( img, width - right, height - bottom, width, height, imgWidth - right, imgHeight - bottom, imgWidth, imgHeight, c); } // Draw the edges BufferedImage subImg = null; TexturePaint paint; // North if (top > 0 && left + right < width) { if (imgWidth > right + left) { subImg = img.getSubimage(left, 0, imgWidth - right - left, top); paint = new TexturePaint(subImg, new Rectangle(left, 0, imgWidth - left - right, top)); g.setPaint(paint); g.fillRect(left, 0, width - left - right, top); } } // South if (bottom > 0 && left + right < width) { if (imgHeight > bottom && imgWidth > right + left) { subImg = img.getSubimage(left, imgHeight - bottom, imgWidth - right - left, bottom); paint = new TexturePaint( subImg, new Rectangle(left, height - bottom, imgWidth - left - right, bottom)); g.setPaint(paint); g.fillRect(left, height - bottom, width - left - right, bottom); } } // West if (left > 0 && top + bottom < height) { if (imgHeight > top + bottom) { subImg = img.getSubimage(0, top, left, imgHeight - top - bottom); paint = new TexturePaint(subImg, new Rectangle(0, top, left, imgHeight - top - bottom)); g.setPaint(paint); g.fillRect(0, top, left, height - top - bottom); } } // East if (right > 0 && top + bottom < height) { if (imgWidth > right && imgHeight > top + bottom) { subImg = img.getSubimage(imgWidth - right, top, right, imgHeight - top - bottom); paint = new TexturePaint( subImg, new Rectangle(width - right, top, right, imgHeight - top - bottom)); g.setPaint(paint); g.fillRect(width - right, top, right, height - top - bottom); } } // Fill the center if (fillContentArea) { if (left + right < imgWidth && top + bottom < imgHeight) { subImg = img.getSubimage(left, top, imgWidth - right - left, imgHeight - top - bottom); paint = new TexturePaint( subImg, new Rectangle(left, top, imgWidth - right - left, imgHeight - top - bottom)); g.setPaint(paint); g.fillRect(left, top, width - right - left, height - top - bottom); } } g.dispose(); }