/** * Draws as much as possible of a given string into a 2d square region in a graphical space. * * @param g component onto which to draw * @param f font to use in drawing the string * @param s string to be drawn * @param xPos * @param yPos * @param width * @param height */ public static void drawStringMultiline( Graphics2D g, Font f, String s, double xPos, double yPos, double width, double height) { FontMetrics fm = g.getFontMetrics(f); int w = fm.stringWidth(s); int h = fm.getAscent(); // g.setColor(Color.LIGHT_GRAY); g.setColor(Color.BLACK); g.setFont(f); Scanner lineSplitter = new Scanner(s); // draw as much as can fit in each item // read all content from scanner, storing in string lists (where each string == 1 line), each // string should be as long as possible without overflowing the space int maxRows = (int) height / h; List<String> textRows = new ArrayList<>(); while (lineSplitter.hasNextLine() && textRows.size() < maxRows) { String line = lineSplitter.nextLine(); // if line is blank, insert to maintain paragraph seps if (line.trim().equals("")) { textRows.add(""); } // else, pass to inner loop StringBuilder currentBuilder = new StringBuilder(); int currentStrWidth = 0; Scanner splitter = new Scanner(line); while (splitter.hasNext() && textRows.size() < maxRows) { String token = splitter.next() + " "; // TODO incorporate weight detection, formatting for token? currentStrWidth += fm.stringWidth(token); if (currentStrWidth >= width) { // if string length >= glyph width, build row textRows.add(currentBuilder.toString()); currentBuilder = new StringBuilder(); currentBuilder.append(token); currentStrWidth = fm.stringWidth(token); } else { // if not yet at end of row, append to builder currentBuilder.append(token); } } // if we've still space and still have things to write, add them here if (textRows.size() < maxRows) { textRows.add(currentBuilder.toString()); currentBuilder = new StringBuilder(); currentStrWidth = 0; } } // write each line to object for (int t = 0; t < textRows.size(); t++) { String line = textRows.get(t); if (fm.stringWidth(line) <= width) { // ensure that string doesn't overflow the box // g.drawString(line, (float) (xPos-(width/2.)), (float) (yPos-(height/2.) + // h * (t+1))); g.drawString(line, (float) xPos, (float) (yPos + h * (t + 1))); } } }
/** [Internal] */ private void paintBackground(Graphics2D g2, Color theBackground) { Color color1 = g2.getColor(); if (theBackground == null) theBackground = Color.white; g2.setColor(theBackground); g2.fillRect(0, 0, 30000, 30000); g2.setColor(color1); }
/** paint the canvas into a image file of given width and height */ public void writeToImage(String s, int w, int h) { String ext; File f; try { ext = s.substring(s.lastIndexOf(".") + 1); f = new File(s); } catch (Exception e) { System.out.println(e); return; } if (!ext.equals("jpg") && !ext.equals("png")) { System.out.println("Cannot write to file: Illegal extension " + ext); return; } boolean opq = true; if (theOpaque != null) opq = theOpaque; BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setBackground(Color.white); g2.setPaint(Color.black); g2.setStroke(new BasicStroke(1)); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); doBuffer(g2, true, new Rectangle(0, 0, w, h)); try { ImageIO.write(image, ext, f); } catch (Exception e) { System.out.println(e); } }
/** Erase the whole canvas. (Does not repaint.) */ private void erase() { Color original = graphic.getColor(); graphic.setColor(backgroundColor); Dimension size = canvas.getSize(); graphic.fill(new Rectangle(0, 0, size.width, size.height)); graphic.setColor(original); }
public void paint(Graphics g) { super.paint(g); Graphics2D g2D = (Graphics2D) g; int x = this.getWidth(); int y = this.getHeight(); int z = 4; if (orientation.equals(DesktopBar.WEST)) { int[] X = {x - z, x - z, 2 * x / 5, x / 2, z, x / 2, 2 * x / 5}; int[] Y = {2 * y / 5, 3 * y / 5, 3 * y / 5, y - z, y / 2, z, 2 * y / 5}; g2D.draw(new Polygon(X, Y, 7)); } if (orientation.equals(DesktopBar.EAST)) { int[] X = {z, z, 2 * x / 3, x / 2, x - z, x / 2, 2 * x / 3}; int[] Y = {2 * y / 5, 3 * y / 5, 3 * y / 5, y - z, y / 2, z, 2 * y / 5}; g2D.draw(new Polygon(X, Y, 7)); } if (orientation.equals(DesktopBar.NORTH)) { int[] X = {2 * x / 5, 3 * x / 5, 3 * x / 5, x - z, x / 2, z, 2 * x / 5}; int[] Y = {y - z, y - z, 2 * y / 5, y / 2, z, y / 2, 2 * y / 5}; g2D.draw(new Polygon(X, Y, 7)); } if (orientation.equals(DesktopBar.SOUTH)) { int[] X = {2 * x / 5, 3 * x / 5, 3 * x / 5, x - z, x / 2, z, 2 * x / 5}; int[] Y = {z, z, 2 * y / 3, y / 2, y - z, y / 2, 2 * y / 3}; g2D.draw(new Polygon(X, Y, 7)); } }
void drawLine(Graphics g, DrawObject L) { if (L == null) { return; } if ((sequencingOn) && (L.sequenceNum != currentSequenceNumDisplay)) { return; } Graphics2D g2 = (Graphics2D) g; g2.setStroke(L.drawStroke); int x1 = (int) ((L.x - minX) / (maxX - minX) * (D.width - 2 * inset)); int y1 = (int) ((L.y - minY) / (maxY - minY) * (D.height - 2.0 * inset)); int x2 = (int) ((L.x2 - minX) / (maxX - minX) * (D.width - 2 * inset)); int y2 = (int) ((L.y2 - minY) / (maxY - minY) * (D.height - 2.0 * inset)); if (L.isArrow) { drawArrow( (Graphics2D) g, inset + x1, D.height - y1 - inset, inset + x2, D.height - y2 - inset, 1.0f, L.drawStroke); } else { g.drawLine(inset + x1, D.height - y1 - inset, inset + x2, D.height - y2 - inset); } }
private BufferedImage createThumbNailImage(Dimension imgSize, ProgressMonitor pm) { Assert.notNull(pm, "pm"); String thumbNailBandName = getThumbnailBandName(); Band thumbNailBand = product.getBand(thumbNailBandName); Debug.trace( "ProductSubsetDialog: Reading thumbnail data for band '" + thumbNailBandName + "'..."); pm.beginTask("Creating thumbnail image", 5); BufferedImage image = null; try { MultiLevelSource multiLevelSource = BandImageMultiLevelSource.create(thumbNailBand, SubProgressMonitor.create(pm, 1)); final ImageLayer imageLayer = new ImageLayer(multiLevelSource); final int imageWidth = imgSize.width; final int imageHeight = imgSize.height; final int imageType = BufferedImage.TYPE_3BYTE_BGR; image = new BufferedImage(imageWidth, imageHeight, imageType); Viewport snapshotVp = new DefaultViewport(isModelYAxisDown(imageLayer)); final BufferedImageRendering imageRendering = new BufferedImageRendering(image, snapshotVp); final Graphics2D graphics = imageRendering.getGraphics(); graphics.setColor(getBackground()); graphics.fillRect(0, 0, imageWidth, imageHeight); snapshotVp.zoom(imageLayer.getModelBounds()); snapshotVp.moveViewDelta(snapshotVp.getViewBounds().x, snapshotVp.getViewBounds().y); imageLayer.render(imageRendering); pm.worked(4); } finally { pm.done(); } return image; }
public void draw(node leaf, Graphics2D g, int px, int py) { int lvl = leaf.getLevel(); double l = lvl; counts[lvl]++; double xfraq = counts[lvl] / (spacing[lvl] + 1); double yfraq = l / depth; int x = new Double(1600 * xfraq).intValue(); int y = new Double(1200 * yfraq).intValue() + 10; if (leaf.getAttr() != null) { g.drawString(leaf.getAttr(), x - 20, y); } if (leaf.getCrit() != null) { g.drawString(leaf.getCrit(), x - 20, y + 10); } if (leaf.getResult() != null) { g.drawString(leaf.getResult(), x - 20, y + 10); } g.drawLine(x, y, px, py); // g.fillRect(x,y,20,20); ArrayList children = leaf.getChildren(); while (!children.isEmpty()) { draw((node) children.remove(0), g, x, y); } }
@Override public void paint(Graphics2D g2, State s, float cWidth, float cHeight) { float domainXScale = this.dwidth; float domainYScale = this.dheight; // determine then normalized width float width = (1.0f / domainXScale) * cWidth; float height = (1.0f / domainYScale) * cHeight; // pass through each cell of the map and if it is a wall, draw it for (int i = 0; i < this.dwidth; i++) { for (int j = 0; j < this.dheight; j++) { if (this.map[i][j] > 0) { float rx = i * width; float ry = cHeight - height - j * height; // draw the walls; make them black g2.setColor(this.cols[this.map[i][j] - 1]); g2.fill(new Rectangle2D.Float(rx, ry, width, height)); } } } }
public static void saveJPG(Image img, String s) { BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, null, null); FileOutputStream out = null; try { out = new FileOutputStream(s); } catch (java.io.FileNotFoundException io) { System.out.println("File Not Found"); } JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); param.setQuality(0.5f, false); encoder.setJPEGEncodeParam(param); try { encoder.encode(bi); out.close(); } catch (java.io.IOException io) { System.out.println("IOException"); } }
private static Image getImage(ResizableIcon icon, int size) { icon.setDimension(new Dimension(size, size)); if (icon instanceof AsynchronousLoading) { AsynchronousLoading async = (AsynchronousLoading) icon; if (async.isLoading()) { final CountDownLatch latch = new CountDownLatch(1); final boolean[] status = new boolean[1]; AsynchronousLoadListener all = new AsynchronousLoadListener() { @Override public void completed(boolean success) { status[0] = success; latch.countDown(); } }; async.addAsynchronousLoadListener(all); try { latch.await(); } catch (InterruptedException ignored) { } async.removeAsynchronousLoadListener(all); if (!status[0]) { return null; } if (async.isLoading()) { return null; } } } Image result = FlamingoUtilities.getBlankImage(size, size); Graphics2D g2d = (Graphics2D) result.getGraphics().create(); icon.paintIcon(null, g2d, 0, 0); g2d.dispose(); return result; }
/** Draws the edge structure of the tree */ public void drawEdges(Graphics2D gg) { gg.setPaint(Color.black); Enumeration nodeList = argument.getBreadthFirstTraversal().elements(); // For each vertex... while (nodeList.hasMoreElements()) { // Get its edge list... TreeVertex vertex = (TreeVertex) nodeList.nextElement(); Enumeration edges = vertex.getEdgeList().elements(); // For each edge in the list... while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); // If we have several vertices on layer 0, only draw // edges for layers below that if (!(argument.isMultiRoots() && vertex.getLayer() == 0)) { // If the edge has been selected with the mouse, // use a thick line if (edge.isSelected()) { gg.setStroke(selectStroke); } gg.draw(edge.getShape(this)); // If we used a thick line, reset the stroke to normal // line for next edge. if (edge.isSelected()) { gg.setStroke(solidStroke); } TreeVertex edgeSource = edge.getDestVertex(); } } } }
/** * Returns the specified image as icon. * * @param name name of icon * @return icon */ public static ImageIcon icon(final String name) { ImageIcon ii = ICONS.get(name); if (ii != null) return ii; Image img; if (GUIConstants.scale > 1) { // choose large image or none final URL url = GUIConstants.large() ? BaseXImages.class.getResource("/img/" + name + "_32.png") : null; if (url == null) { // resize low-res image if no hi-res image exists img = get(url(name)); final int w = (int) (img.getWidth(null) * GUIConstants.scale); final int h = (int) (img.getHeight(null) * GUIConstants.scale); final BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); final Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(img, 0, 0, w, h, null); g2.dispose(); img = tmp; } else { img = get(url); } } else { img = get(name); } ii = new ImageIcon(img); ICONS.put(name, ii); return ii; }
/** * Paint to an offscreen graphic, e.g. a graphic for an image or svg file. * * @param g * @param rect */ public void paintOffscreen(Graphics2D g, Rectangle rect) { // Get the components of the sort by X position. Component[] components = getComponents(); Arrays.sort( components, new Comparator<Component>() { public int compare(Component component, Component component1) { return component.getX() - component1.getX(); } }); for (Component c : this.getComponents()) { if (c instanceof DataPanel) { Graphics2D g2d = (Graphics2D) g.create(); Rectangle clipRect = new Rectangle(c.getBounds()); clipRect.height = rect.height; g2d.setClip(clipRect); g2d.translate(c.getX(), 0); ((DataPanel) c).paintOffscreen(g2d, rect); } } // super.paintBorder(g); }
// buffered images are just better. protected static BufferedImage imageToBufferedImage(Image img) { BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, null, null); return bi; }
// From: http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135 void drawArrow( Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke, BasicStroke drawStroke) { double aDir = Math.atan2(xCenter - x, yCenter - y); // Line can be dashed. g2d.setStroke(drawStroke); g2d.drawLine(x, y, xCenter, yCenter); // make the arrow head solid even if dash pattern has been specified g2d.setStroke(lineStroke); Polygon tmpPoly = new Polygon(); int i1 = 12 + (int) (stroke * 2); // make the arrow head the same size regardless of the length length int i2 = 6 + (int) stroke; tmpPoly.addPoint(x, y); tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5)); tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir)); tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5)); tmpPoly.addPoint(x, y); // arrow tip g2d.drawPolygon(tmpPoly); // Remove this line to leave arrow head unpainted: g2d.fillPolygon(tmpPoly); }
private void shadeExt(Graphics2D g2, int r, int g, int b, int a) { g2.setPaint(new Color(r, g, b, a)); g2.fillRect(0, 0, iw, rect.y); /* _N_ */ g2.fillRect( rect.x + rect.width + 1, rect.y, iw - rect.x - rect.width - 1, rect.height + 1); /* E */ g2.fillRect(0, rect.y, rect.x, rect.height + 1); /* W */ g2.fillRect(0, rect.y + rect.height + 1, iw, ih - rect.y - rect.height - 1); /* _S_ */ }
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle2D rect = new Rectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1); g2.setPaint(Color.YELLOW); g2.fill(rect); g2.setPaint(Color.BLUE); g2.draw(rect); }
// initialize default image private BufferedImage getDefaultImage(int w, int h) { BufferedImage defaultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = defaultImage.createGraphics(); graphics2D.setColor(new Color(200, 200, 200)); graphics2D.fillRect(0, 0, w, h); graphics2D.setColor(new Color(130, 130, 130)); graphics2D.drawRect(0, 0, w - 1, h - 1); return defaultImage; }
public void paint(Graphics g) { super.paint(g); if (_focus && g instanceof Graphics2D) { Stroke old = ((Graphics2D) g).getStroke(); ((Graphics2D) g).setStroke(_dashed); g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); ((Graphics2D) g).setStroke(old); } }
private Graphics2D createCanvas(BufferedImage image) { Graphics2D canvas = (Graphics2D) image.getGraphics(); canvas.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); canvas.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); canvas.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); canvas.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); return canvas; }
public Icon makeIcon(Color color, int width, int height) { BufferedImage temp = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // create buffered icon image Graphics2D temp2 = temp.createGraphics(); temp2.setColor(color); temp2.fillRect(0, 0, width, height); // draw in buffer Icon icon = new ImageIcon(temp); return icon; }
public void plot(Color c, int x0, int y0, int z0) { Graphics2D g = bi.createGraphics(); g.setColor(c); if (x0 >= XRES || y0 >= YRES || x0 < 0 || y0 < 0) return; if (z0 > zbuffer[x0][y0]) { g.drawLine(x0, y0, x0, y0); zbuffer[x0][y0] = z0; } }
public static BufferedImage resize(BufferedImage image, int width, int height) { BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.addRenderingHints( new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(image, 0, 0, width, height, null); g2d.dispose(); return bi; }
/** Draws this handle. */ public void draw(Graphics2D g) { Graphics2D gg = (Graphics2D) g.create(); gg.transform(view.getDrawingToViewTransform()); for (Connector c : connectors) { c.draw(gg); } gg.dispose(); drawCircle(g, (getTarget() == null) ? Color.red : Color.green, Color.black); }
private void drawBlock(Graphics2D g, double x, GraphBlock gb) { double y0 = getTimeY(gb.getStartTime()); double y1 = getTimeY(gb.getEndTime()); double x0 = x - active_width / 2; Color c = getThreadBlockColor(gb.getThread()); Rectangle2D r = new Rectangle2D.Double(x0, y0, active_width, y1 - y0); g.setColor(c); g.fill(r); }
/** * Render the View to the given graphic context. This implementation render the interior first, * then the outline. */ public void paint(Graphics2D g, Rectangle2D a) { if (!a.intersects(getBounds())) return; if (image != null) { // paint bitmap g.drawImage(image, text2ModelTr, null); // debug: g.setPaint(Color.red); g.draw(this.bounds); super.paint(g, a); // possibly paint framebox if non-null } else { // paint textlayout super.paint(g, a); // possibly paint framebox if non-null AffineTransform oldAT = g.getTransform(); // paint text in black g.setPaint(Color.black); // from now on, we work in Y-direct (<0) coordinates to avoid inextricable problems with font // being mirrored... g.transform(text2ModelTr); // also include rotation textLayout.draw(g, 0.0f, 0.0f); // [pending] ajouter un cadre si areDimensionsComputed (wysiwyg du pauvre) // get back to previous transform g.setTransform(oldAT); if (DEBUG) { g.setPaint(Color.red); g.draw(bounds); } } }
public void paint(Graphics g) { gRef = (Graphics2D) g; // change size of font gRef.setFont(gRef.getFont().deriveFont(9.0f)); fmRef = g.getFontMetrics(); // Clear background if (Preferences.monochrome) { gRef.setColor(Preferences.whiteColor); } else { gRef.setColor(Preferences.backgroundColor); } gRef.fillRect(0, 0, getWidth(), getHeight()); // set colour to correct drawing colour if (Preferences.monochrome) { gRef.setColor(Preferences.blackColor); } else { gRef.setColor(Preferences.penColor); } gRef.translate(0, margin); // Call c code to draw tree gRef.scale(scale, scale); nativeDrawTree(); }
public void draw(Graphics2D graphic) { setForegroundColor(colorString); graphic.fill(shape); if (text != null) { setForegroundColor("black"); java.awt.Rectangle bounds = shape.getBounds(); int x = bounds.x + bounds.width / 2; int y = bounds.y + bounds.height / 2; graphic.drawString(text, x, y); } }
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; int i, j, k; System.out.println("Starting painting"); // draw sets if (firstTimeAround) { drawSet(g2); render(); viewer.save("final_product_full.png"); firstTimeAround = false; } // render(); /* for(i=0;i<viewer.getWidth();i++){ for(j=0;j<viewer.getHeight();j++){ viewer.setPixel(i,j,Color3.BLACK); } } */ g2.setBackground(Color.BLACK); // if(newRender){ for (i = minX; i < maxX; i++) { // System.out.println("line "+i); for (j = minY; j < maxY; j++) { g2.setColor( new Color( (float) viewer.getPixel(i, j).getR(), (float) viewer.getPixel(i, j).getG(), (float) viewer.getPixel(i, j).getB())); // g2.drawLine(i,j,1,1); Ellipse2D.Double node_circ = new Ellipse2D.Double(i, j, 1, 1); g2.fill(node_circ); g2.setColor(Color.BLACK); // if(j*scale<viewer.getHeight()){ // j=0;//viewer.getHeight(); // } } // if(i*scale<viewer.getWidth()){ // i=0;//viewer.getWidth(); // } } // g2.drawImage(viewer.1) newRender = false; // } System.out.println("end painting"); }