/** constructor */ public DanceQueuePanel() { // flow layout setLayout(new FlowLayout()); // uses buffer to draw arrows based on queues in an array myImage = new BufferedImage(600, 600, BufferedImage.TYPE_INT_RGB); myBuffer = myImage.getGraphics(); // uses timer to queue buffer changes time = 0; timer = new Timer(5, new Listener()); timer.start(); setFocusable(true); // picks instructions based on song & level if (Danceoff.getSong() == -1 && Danceoff.getDifficulty() == 0) { arrows = new Arrow[] {new UpArrow(1000), new DownArrow(2000), new LeftArrow(3000)}; } // setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK, 3), // "DanceQueuePanel")); // load images for arrows rightArrowImg = null; leftArrowImg = null; upArrowImg = null; downArrowImg = null; try { rightArrowImg = ImageIO.read(new File("arrowB right.png")); leftArrowImg = ImageIO.read(new File("arrowB left.png")); upArrowImg = ImageIO.read(new File("arrowB up copy.png")); downArrowImg = ImageIO.read(new File("arrowB down.png")); } catch (IOException e) { warn("YOU FAIL", e); System.exit(2); } }
private static ImageElement fetchImageProperties( final BufferedImage source, final ImageType imageType, final String id, final Date modifiedDate, final String mimeType) throws NullPointerException, IOException { if (verifyNotNull(source)) { ImageElement image = new ImageElement(); image.setBitDepth( verifyNotNull(source.getColorModel().getPixelSize()) ? source.getColorModel().getPixelSize() : 0); image.setHeight(verifyNotNull(source.getHeight()) ? source.getHeight() : 0); image.setWidth(verifyNotNull(source.getWidth()) ? source.getWidth() : 0); image.setSizeInBytes(determineSizeOfBufferedImage(source, imageType.toString())); image.setCreated(new Date(System.currentTimeMillis())); image.setTransparent( determineTransparencyType(source.getGraphics().getColor().getTransparency())); if (verifyNotNull(modifiedDate)) image.setModified(modifiedDate); image.setId((verifyNotNull(id) ? id : "")); image.setFontType( verifyNotNull(source.getGraphics().getFont()) ? source.getGraphics().getFont() : new Font("Default", 0, 0)); image.setMediaType(MediaType.Missing); return image; } throw new NullPointerException(E_OBJECT_WAS_NULL); }
private void scaleImage() { Image img = back.getScaledInstance(scx(back.getWidth()), scy(back.getHeight()), Image.SCALE_SMOOTH); back = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics g = back.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); }
/** * Turn a (possibly) translucent or indexed image into a display-compatible bitmask image using * the given alpha threshold and render-to-background colour, or display-compatible translucent * image. The alpha values in the image are set to either 0 (below threshold) or 255 (above * threshold). The render-to-background colour bg_col is used to determine how the pixels * overlapping transparent pixels should be rendered. The fast algorithm just sets the colour * behind the transparent pixels in the image (for bitmask source images); the slow algorithm * actually renders the image to a background of bg_col (for translucent sources). * * @param thresh alpha threshold between 0 and 255 * @param fast use fast algorithm (only set bg_col behind transp. pixels) * @param bitmask true=use bitmask, false=use translucent */ public JGImage toDisplayCompatible(int thresh, JGColor bg_col, boolean fast, boolean bitmask) { Color bgcol = new Color(bg_col.r, bg_col.g, bg_col.b); int bgcol_rgb = (bgcol.getRed() << 16) | (bgcol.getGreen() << 8) | bgcol.getBlue(); JGPoint size = getSize(); int[] buffer = getPixels(); // render image to bg depending on bgcol BufferedImage img_bg; if (bitmask) { img_bg = createCompatibleImage(size.x, size.y, Transparency.BITMASK); } else { img_bg = createCompatibleImage(size.x, size.y, Transparency.TRANSLUCENT); } int[] bg_buf; if (!fast) { Graphics g = img_bg.getGraphics(); g.setColor(bgcol); // the docs say I could use bgcol in the drawImage as an // equivalent to the following two lines, but this // doesn't handle translucency properly and is _slower_ g.fillRect(0, 0, size.x, size.y); g.drawImage(img, 0, 0, null); bg_buf = new JREImage(img_bg).getPixels(); } else { bg_buf = buffer; } // g.dispose(); // ColorModel rgb_bitmask = ColorModel.getRGBdefault(); // rgb_bitmask = new PackedColorModel( // rgb_bitmask.getColorSpace(),25,0xff0000,0x00ff00,0x0000ff, // 0x1000000, false, Transparency.BITMASK, DataBuffer.TYPE_INT); // ColorSpace space, int bits, int rmask, int gmask, int bmask, int amask, boolean // isAlphaPremultiplied, int trans, int transferType) int[] thrsbuf = new int[size.x * size.y]; for (int y = 0; y < size.y; y++) { for (int x = 0; x < size.x; x++) { if (((buffer[y * size.x + x] >> 24) & 0xff) >= thresh) { thrsbuf[y * size.x + x] = bg_buf[y * size.x + x] | (0xff << 24); } else { // explicitly set the colour of the transparent pixel. // This makes a difference when scaling! // thrsbuf[y*size.x+x]=bg_buf[y*size.x+x]&~(0xff<<24); thrsbuf[y * size.x + x] = bgcol_rgb; } } } return new JREImage( output_comp.createImage( new MemoryImageSource( size.x, size.y, // rgb_bitmask, img_bg.getColorModel(), // display compatible bitmask bitmask ? thrsbuf : bg_buf, 0, size.x))); }
public static BufferedImage tileImage(BufferedImage im, int width, int height) { GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); int transparency = Transparency.OPAQUE; // Transparency.BITMASK; BufferedImage compatible = gc.createCompatibleImage(width, height, transparency); Graphics2D g = (Graphics2D) compatible.getGraphics(); g.setPaint(new TexturePaint(im, new Rectangle(0, 0, im.getWidth(), im.getHeight()))); g.fillRect(0, 0, width, height); return compatible; }
// Method that draws all elements in each set starting with the root and then // calling traverse to get the rest of the tree void drawSets(LinkedList llist, draw d) { int temp; double stringLength = 0.0; String s = ""; StringTokenizer getWeight; GTN gtn = new GTN(); GTN TempChild; x = .0; y = TitleEndy - .24; nodelist.reset(); while (nodelist.hasMoreElements()) { if (nodelist.hasMoreElements()) gtn = (GTN) (nodelist.nextElement()); else gtn = (GTN) (nodelist.currentElement()); // traverse down current tree s = traverse(gtn, s, llist, d, true); s += "}"; BufferedImage buffer = new BufferedImage( GaigsAV.preferred_width, GaigsAV.preferred_height, BufferedImage.TYPE_BYTE_GRAY); temp = buffer.getGraphics().getFontMetrics(defaultFont).stringWidth(s); // temp = d.getGraphics().getFontMetrics(defaultFont).stringWidth(s); stringLength = ((double) temp / (double) d.getSize().width); // gets the weight of the current set getWeight = new StringTokenizer(s, ","); LGKS.set_text_align(0, 2, llist, d); LGKS.text(gtn.Gx, (gtn.Gy + Lenx + .02), ("" + getWeight.countTokens()), llist, d); LGKS.set_text_align(1, 2, llist, d); // these checks insure that the set info will be drawn within // the bounds of the window if ((x + stringLength) >= 1.0) { x = 0; y -= .05; LGKS.text(x, y, s, llist, d); x = stringLength + .05; } else { LGKS.text(x, y, s, llist, d); x += stringLength + .05; } s = ""; } }
public String paintDendrogram(File f, Cluster clustering) { // ArrayList clusters = null; lowThreshold = 0; threshold = (int) maxMergeValue + 1; do { threshold = threshold - 1; clusters = getClusters(clustering, threshold); } while (clusters.size() > maxVertLines); int size = clusters.size(); factor = 1000 / size; if (factor < 4) factor = 4; int xSize = factor * size + 50; int ySize = 500 + 50; BufferedImage image = new BufferedImage(xSize + 1, ySize + 1, BufferedImage.TYPE_BYTE_INDEXED); g = (Graphics2D) image.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, 30 + xSize, 30 + ySize); g.setPaintMode(); paintCoords(xSize, ySize); mapString = "<map name=\"Dendrogram\">\n"; minX = 50; minY = 10; maxY = ySize - 40; drawCluster(clustering); g.setColor(Color.GRAY); g.drawLine(clustering.x, clustering.y, clustering.x, minY); try { FileOutputStream fo = new FileOutputStream(f); GIFEncoder encode = new GIFEncoder(image); encode.write(fo); fo.close(); } catch (Exception e) { e.printStackTrace(); } return mapString + "</map>"; }
public JGImage rotateAny(double angle) { JGPoint size = getSize(); int sw = size.x; int sh = size.y; // destination size is upper bound size. Upper bound is the max // of the longest dimension and the figure's dimension at 45 degrees // = sw*sin(45)+sh*cos(45) ~= 1.5*(sw+sh) int dw = (int) Math.max(Math.max(sw, sh), 0.75 * (sw + sh)); int dh = dw; int xtrans = (dw - sw) / 2; int ytrans = (dh - sh) / 2; BufferedImage dst = createCompatibleImage(dw, dh, Transparency.BITMASK); Graphics2D g = (Graphics2D) dst.getGraphics(); AffineTransform tt = AffineTransform.getTranslateInstance(xtrans, ytrans); AffineTransform tr = AffineTransform.getRotateInstance(angle, sw / 2, sh / 2); tt.concatenate(tr); g.drawImage(img, tt, null); return new JREImage(dst); }
public static java.awt.image.BufferedImage loadBufferedImageFromResources( Component c, String filename) { try { Misc m = new Misc(); java.awt.Image img = loadImageFromResources(filename); MediaTracker mt = new MediaTracker(c); mt.addImage(img, 0); mt.waitForID(0); int width = img.getWidth(null); int height = img.getHeight(null); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics gg = bi.getGraphics(); gg.drawImage(img, 0, 0, null); gg.dispose(); return bi; } catch (Exception ex) { System.out.println(ex.toString()); } return null; }
protected void filePNG() { JFileChooser chooser = null; File pngFile = null; if (currentFile == null) { chooser = new JFileChooser(startDirectory); } else { pngFile = new File(currentFile.getName() + ".png"); chooser = new JFileChooser(pngFile); if (!currentFile.isDirectory()) { chooser.setSelectedFile(currentFile); } } if (pngFile == null) return; try { BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); paint(image.getGraphics()); ImageIO.write(image, "png", pngFile); } catch (Exception e) { } return; /* JFileChooser chooser = null; File pngFile = null; if (currentFile == null) { chooser = new JFileChooser(startDirectory); } else { pngFile = new File(currentFile.getName()+".png"); chooser = new JFileChooser(pngFile); if (!currentFile.isDirectory()) { chooser.setSelectedFile(currentFile); } } int returnVal = chooser.showSaveDialog(gw); if (returnVal == JFileChooser.APPROVE_OPTION) { File pngFile = chooser.getSelectedFile(); int maxX = Integer.MIN_VALUE; int minX = Integer.MAX_VALUE; int maxY = Integer.MIN_VALUE; int minY = Integer.MAX_VALUE; while(Node node : getNodes()) { if(node.getX() > maxX) { maxX = node.getX(); } if(node.getX() < minX) { minX = node.getX(); } if(node.getY() > maxY) { maxY = node.getY(); } if(node.getY() < minY) { minY = node.getY(); } } BufferedImage image = new BufferedImage(maxX-minX+50,maxY-minY+50,BufferedImage.TYPE_INT_RGB); ImageIO.write(image,"png",pngFile); } */ }
// Although it presently returns a boolean, that was only needed // during my aborted attempted at animated graphics primitives. // Until those become a reality the boolean value returned by this // routine is unnecessary public boolean animate(int sAt, boolean forward) { int x; LinkedList lt = null; animation_done = true; // May be re-set in paintComponent via indirect paintImmediately call at end // Was used in aborted attempted to // introduce animated primitives. Now // it's probably excess baggage that // remains because I still have hopes // of eventually having animated // primitives if (getSize().width != 0 && getSize().height != 0) { my_width = getSize().width; // set dimensions my_height = getSize().height; } else { my_width = GaigsAV.preferred_width; // set dimensions my_height = GaigsAV.preferred_height; } // First capture the new image in a buffer called image2 SnapAt = sAt; BufferedImage image2 = new BufferedImage(my_width, my_height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) image2.getGraphics(); // need a separate object each time? g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fillRect(0, 0, my_width, my_height); // Set horizoff and vertoff to properly center the visualization in the // viewing window. This is not quite perfect because visualizations // that are not properly centered within their [0,1] localized // coordinates will not be perfectly centered, but it is much better // than it was previously. if (no_mouse_drag) { horizoff = (my_width - GaigsAV.preferred_width) / 2; vertoff = (my_height - GaigsAV.preferred_height) / 2; } list_of_snapshots.reset(); x = 0; lt = new LinkedList(); while (x < SnapAt && list_of_snapshots.hasMoreElements()) { lt = (LinkedList) list_of_snapshots.nextElement(); x++; } lt.reset(); animation_done = true; // System.out.println("before loop " + horizoff); while (lt.hasMoreElements()) { obj tempObj = (obj) lt.nextElement(); animation_done = animation_done && (tempObj.execute(g2 /*offscreen*/, zoom, vertoff, horizoff)); // System.out.println("in loop"); } // Next capture the image we are coming from in a buffer called image1 SnapAt = (forward ? sAt - 1 : sAt + 1); BufferedImage image1 = new BufferedImage(my_width, my_height, BufferedImage.TYPE_INT_RGB); Graphics2D g1 = (Graphics2D) image1.getGraphics(); // need a separate object each time? g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g1.setColor(Color.WHITE); g1.fillRect(0, 0, my_width, my_height); // Set horizoff and vertoff to properly center the visualization in the // viewing window. This is not quite perfect because visualizations // that are not properly centered within their [0,1] localized // coordinates will not be perfectly centered, but it is much better // than it was previously. if (no_mouse_drag) { horizoff = (my_width - GaigsAV.preferred_width) / 2; vertoff = (my_height - GaigsAV.preferred_height) / 2; } list_of_snapshots.reset(); x = 0; lt = new LinkedList(); while (x < SnapAt && list_of_snapshots.hasMoreElements()) { lt = (LinkedList) list_of_snapshots.nextElement(); x++; } lt.reset(); animation_done = true; // System.out.println("before loop " + horizoff); while (lt.hasMoreElements()) { obj tempObj = (obj) lt.nextElement(); animation_done = animation_done && (tempObj.execute(g1 /*offscreen*/, zoom, vertoff, horizoff)); // System.out.println("in loop"); } // Now slide from image1 to image2 // From the gaff Visualizer by Chris Gaffney // double step = 4; // Adjust this for more/less granularity between images double step = 40; // Adjust this for more/less granularity between images Image buffer = getGraphicsConfiguration().createCompatibleVolatileImage(my_width, my_height); Graphics2D g2d = (Graphics2D) buffer.getGraphics(); AffineTransform trans = AffineTransform.getTranslateInstance(step * (forward ? -1 : 1), 0); // AffineTransform orig = g2d.getTransform(); Shape mask = createMask(my_width, my_height); for (double i = 0; i < my_width; i += step) { if (i + step > my_width) // last time through loop, so adjust transform trans = AffineTransform.getTranslateInstance(((double) (my_width - i)) * (forward ? -1 : 1), 0); g2d.transform(trans); g2d.drawImage(image1, 0, 0, this); g2d.setColor(Color.BLACK); g2d.fill(mask); AffineTransform last = g2d.getTransform(); g2d.transform(AffineTransform.getTranslateInstance(my_width * (-1 * (forward ? -1 : 1)), 0)); g2d.drawImage(image2, 0, 0, this); g2d.setColor(Color.BLACK); g2d.fill(mask); g2d.setTransform(last); this.my_image = buffer; repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { } } Image b = getGraphicsConfiguration().createCompatibleImage(my_width, my_height); b.getGraphics().drawImage(buffer, 0, 0, null); this.my_image = b; return animation_done; }
// Although it presently returns a boolean, that was only needed // during my aborted attempted at animated graphics primitives. // Until those become a reality the boolean value returned by this // routine is unnecessary public /*synchronized*/ boolean execute(int sAt) { // The commented-out variables below are remnants from legacy // code -- they appear to be no longer needed. // String urlTemp=""; // int z=0; // int idx; // int urlid; // boolean showURL=false; animation_done = true; // May be re-set in paintComponent via indirect paintImmediately call at end // Was used in abored attempted to // introduce animated primitives. Now // it's probably excess baggage that // remains because I still have hopes // of eventually having animated // primitives SnapAt = sAt; if (getSize().width != 0 && getSize().height != 0) { my_width = getSize().width; // set dimensions my_height = getSize().height; } else { my_width = GaigsAV.preferred_width; // set dimensions my_height = GaigsAV.preferred_height; } BufferedImage buff = new BufferedImage(my_width, my_height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) buff.getGraphics(); // need a separate object each time? g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fillRect(0, 0, my_width, my_height); // Set horizoff and vertoff to properly center the visualization in the // viewing window. This is not quite perfect because visualizations // that are not properly centered within their [0,1] localized // coordinates will not be perfectly centered, but it is much better // than it was previously. // if(no_mouse_drag){ // if(first_paint_call){ // // horizoff = (my_width - (int)(0.25 * my_width) - // // GaigsAV.preferred_width) / 2; // horizoff = (my_width - GaigsAV.preferred_width) / 2; // first_paint_call = false; // }else{ // horizoff = (my_width - GaigsAV.preferred_width) / 2; // no_mouse_drag = false; // } // } if (no_mouse_drag) { horizoff = (my_width - GaigsAV.preferred_width) / 2; vertoff = (my_height - GaigsAV.preferred_height) / 2; } int x; list_of_snapshots.reset(); x = 0; LinkedList lt = new LinkedList(); while (x < SnapAt && list_of_snapshots.hasMoreElements()) { lt = (LinkedList) list_of_snapshots.nextElement(); x++; } lt.reset(); animation_done = true; // System.out.println("before loop " + horizoff); while (lt.hasMoreElements()) { obj tempObj = (obj) lt.nextElement(); animation_done = animation_done && (tempObj.execute(g2 /*offscreen*/, zoom, vertoff, horizoff)); // System.out.println("in loop"); } Shape mask = createMask(buff.getWidth(), buff.getHeight()); g2.setColor(Color.BLACK); g2.fill(mask); my_image = buff; repaint(); return animation_done; }
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; }