private static void drawSelection(JTree tree, Graphics g, final int width) { int y = 0; final int[] rows = tree.getSelectionRows(); final int height = tree.getRowHeight(); for (int row : rows) { final TreeCellRenderer renderer = tree.getCellRenderer(); final Object value = tree.getPathForRow(row).getLastPathComponent(); if (value == null) continue; final Component component = renderer.getTreeCellRendererComponent(tree, value, false, false, false, row, false); if (component.getFont() == null) { component.setFont(tree.getFont()); } g.translate(0, y); component.setBounds(0, 0, width, height); boolean wasOpaque = false; if (component instanceof JComponent) { final JComponent j = (JComponent) component; if (j.isOpaque()) wasOpaque = true; j.setOpaque(false); } component.paint(g); if (wasOpaque) { ((JComponent) component).setOpaque(true); } y += height; g.translate(0, -y); } }
private static Pair<Image, Point> createDragImage( final Tree tree, final Component c, Point dragOrigin, boolean adjustToPathUnderDragOrigin) { if (c instanceof JComponent) { ((JComponent) c).setOpaque(true); } c.setForeground(tree.getForeground()); c.setBackground(tree.getBackground()); c.setFont(tree.getFont()); c.setSize(c.getPreferredSize()); final BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D) image.getGraphics(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); c.paint(g2); g2.dispose(); Point point = new Point(-image.getWidth(null) / 2, -image.getHeight(null) / 2); if (adjustToPathUnderDragOrigin) { TreePath path = tree.getPathForLocation(dragOrigin.x, dragOrigin.y); if (path != null) { Rectangle bounds = tree.getPathBounds(path); point = new Point(bounds.x - dragOrigin.x, bounds.y - dragOrigin.y); } } return new Pair<Image, Point>(image, point); }
private static void drawSelection(JTable table, int column, Graphics g, final int width) { int y = 0; final int[] rows = table.getSelectedRows(); final int height = table.getRowHeight(); for (int row : rows) { final TableCellRenderer renderer = table.getCellRenderer(row, column); final Component component = renderer.getTableCellRendererComponent( table, table.getValueAt(row, column), false, false, row, column); g.translate(0, y); component.setBounds(0, 0, width, height); boolean wasOpaque = false; if (component instanceof JComponent) { final JComponent j = (JComponent) component; if (j.isOpaque()) wasOpaque = true; j.setOpaque(false); } component.paint(g); if (wasOpaque) { ((JComponent) component).setOpaque(true); } y += height; g.translate(0, -y); } }
/** * Extracts the buffered image of some component * * @param myComponent the component for which the BufferedImage is required * @param region region to be captured * @return the BufferedImage which has the current condition of component * @throws IOException */ public static BufferedImage Get_Component_Image(Component myComponent, Rectangle region) throws IOException { BufferedImage img = new BufferedImage( myComponent.getWidth(), myComponent.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); myComponent.paint(g); g.dispose(); return img; }
public void paint(Graphics g1) { Graphics2D g = (Graphics2D) g1; Component c; Point p; paintComponent(g); for (int i = 0; i < getComponentCount(); i++) { AffineTransform save = g.getTransform(); c = getComponent(i); p = c.getLocation(); g.translate((int) p.getX(), (int) p.getY()); c.paint(g); g.setTransform(save); } }
public void paint(Graphics g) { // repaint the whole transformer in case the view component was repainted Rectangle clipBounds = g.getClipBounds(); if (clipBounds != null && !clipBounds.equals(visibleRect)) { repaint(); } // clear the background g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); if (view != null && at.getDeterminant() != 0) { Graphics2D g2 = (Graphics2D) g.create(); Insets insets = getInsets(); Rectangle bounds = getBounds(); // don't forget about insets bounds.x += insets.left; bounds.y += insets.top; bounds.width -= insets.left + insets.right; bounds.height -= insets.top + insets.bottom; double centerX1 = bounds.getCenterX(); double centerY1 = bounds.getCenterY(); Rectangle tb = getTransformedSize(); double centerX2 = tb.getCenterX(); double centerY2 = tb.getCenterY(); // set antialiasing by default g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (renderingHints != null) { g2.addRenderingHints(renderingHints); } // translate it to the center of the view component again double tx = centerX1 - centerX2 - getX(); double ty = centerY1 - centerY2 - getY(); g2.translate((int) tx, (int) ty); g2.transform(at); view.paint(g2); g2.dispose(); } // paint the border paintBorder(g); }
public int print(Graphics g, PageFormat pf, int pageIndex) { int response = NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D) g; disableDoubleBuffering(componentToBePrinted); Dimension d = componentToBePrinted.getSize(); double panelWidth = d.width; double panelHeight = d.height; double pageHeight = pf.getImageableHeight(); double pageWidth = pf.getImageableWidth(); double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); if (pageIndex >= totalNumPages) { response = NO_SUCH_PAGE; } else { g2.translate(pf.getImageableX(), pf.getImageableY()); g2.translate(0f, -pageIndex * pageHeight); g2.scale(scale, scale); componentToBePrinted.paint(g2); enableDoubleBuffering(componentToBePrinted); response = Printable.PAGE_EXISTS; } return response; }
BufferedImage createImageFromComponent(Component comp) { BufferedImage retImage = null; if (comp == null) return retImage; try { GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = genv.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); java.awt.image.ColorModel cm = gc.getColorModel(); boolean hasAlpha = cm.hasAlpha(); int cw = comp.getSize().width; int ch = comp.getSize().height; if (hasAlpha) { retImage = gc.createCompatibleImage(cw, ch); } else { retImage = new BufferedImage(cw, ch, BufferedImage.TYPE_INT_ARGB); } if (retImage == null) return retImage; Graphics og = retImage.getGraphics(); comp.paint(og); og.dispose(); } catch (Throwable t) { } return retImage; }
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { // Component printMe = getPrintComponent(); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); // set default foreground color to black // for faster printing, turn off double buffering // RepaintManager.currentManager(this).setDoubleBufferingEnabled(false); Dimension d = printComponent.getSize(); // get size of document double panelWidth = d.width; // width in pixels double panelHeight = d.height; // height in pixels double pageHeight = pf.getImageableHeight(); // height of printer page double pageWidth = pf.getImageableWidth(); // width of printer page double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); // make sure we don't print empty pages if (pageIndex >= totalNumPages) { return Printable.NO_SUCH_PAGE; } // shift Graphic to line up with beginning of print-imageable region g2.translate(pf.getImageableX(), pf.getImageableY()); // shift Graphic to line up with beginning of next page to print g2.translate(0f, -pageIndex * pageHeight); // scale the page so the width fits... g2.scale(scale, scale); // PRINT IT! printComponent.paint(g2); return Printable.PAGE_EXISTS; }