Java Code Examples for javax.swing.JComponent#paint()

The following examples show how to use javax.swing.JComponent#paint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UIUtilities.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * @param component The component to generate an image of.
 * @return The newly created image.
 */
public static Img getImage(JComponent component) {
    Img offscreen = null;
    synchronized (component.getTreeLock()) {
        Graphics2D gc = null;
        try {
            Rectangle bounds = component.getVisibleRect();
            offscreen = Img.create(component.getGraphicsConfiguration(), bounds.width, bounds.height, Transparency.TRANSLUCENT);
            gc = offscreen.getGraphics();
            gc.translate(-bounds.x, -bounds.y);
            component.paint(gc);
        } catch (Exception exception) {
            Log.error(exception);
        } finally {
            if (gc != null) {
                gc.dispose();
            }
        }
    }
    return offscreen;
}
 
Example 2
Source File: bug8032667.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}
 
Example 3
Source File: bug8032667.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}
 
Example 4
Source File: bug8032667.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}
 
Example 5
Source File: bug8032667_image_diff.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 6
Source File: MockComponent.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Creates a MockComponent that resembles the argument component.
 * <P>
 * Note this method will traverse c and its subcomponents and may
 * temporarily change properties of inner components: such as the focused
 * state, the visibility, etc.
 * <P>
 * The goal is of this component is not to mirror the exact state of a
 * component, but rather to provide a sample image of this component in its
 * plain, unmodified, unused state.
 * 
 * @param c
 */
public MockComponent(JComponent c) {
	Dimension preferredSize = c.getPreferredSize();
	Dimension currentSize = c.getSize();

	Dimension d = new Dimension(Math.max(preferredSize.width,
			currentSize.width), Math.max(preferredSize.height,
			currentSize.height));

	if (currentSize.width == 0 || currentSize.height == 0) {
		// if the component isn't visible yet
		c.setSize(d);
		c.doLayout();
	}

	storeState(c);

	image = new BufferedImage(d.width, d.height,
			BufferedImage.TYPE_INT_ARGB);

	Graphics2D g = image.createGraphics();
	g.setComposite(AlphaComposite.Clear);
	g.fillRect(0, 0, d.width, d.height);
	g.setComposite(AlphaComposite.SrcOver);

	c.paint(g);
	g.dispose();
	setPreferredSize(d);
	setMinimumSize(d);
	setMaximumSize(d);
	setOpaque(c.isOpaque());
	setName(c.getName());
	setToolTipText(c.getToolTipText());

	restoreState(c);
}
 
Example 7
Source File: bug8032667.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}
 
Example 8
Source File: bug8032667_image_diff.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 9
Source File: bug8032667_image_diff.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 10
Source File: bug8032667_image_diff.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 11
Source File: bug8032667_image_diff.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 12
Source File: bug8032667_image_diff.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 13
Source File: AbstractTreeTransferHandler.java    From binnavi with Apache License 2.0 5 votes vote down vote up
@Override
public final void dragGestureRecognized(final DragGestureEvent dge) {
  // final TreePath path = tree.getSelectionPath();
  final TreePath path = tree.getPathForLocation(dge.getDragOrigin().x, dge.getDragOrigin().y);

  if (path != null) {
    draggedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
    draggedNodeParent = (DefaultMutableTreeNode) draggedNode.getParent();
    if (drawImage) {
      final Rectangle pathBounds = tree.getPathBounds(path); // getpathbounds of selectionpath
      final JComponent lbl =
          (JComponent) tree.getCellRenderer().getTreeCellRendererComponent(tree, draggedNode,
              false, tree.isExpanded(path),
              ((DefaultTreeModel) tree.getModel()).isLeaf(path.getLastPathComponent()), 0, false);// returning
                                                                                                  // the
                                                                                                  // label
      lbl.setBounds(pathBounds);// setting bounds to lbl
      image =
          new BufferedImage(lbl.getWidth(), lbl.getHeight(),
              java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE);// buffered image reference passing
                                                              // the label's ht and width
      final Graphics2D graphics = image.createGraphics();// creating the graphics for buffered
                                                         // image
      graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // Sets
                                                                                        // the
                                                                                        // Composite
                                                                                        // for the
                                                                                        // Graphics2D
                                                                                        // context
      lbl.setOpaque(false);
      lbl.paint(graphics); // painting the graphics to label
      graphics.dispose();
    }
    dragSource.startDrag(dge, DragSource.DefaultMoveNoDrop, image, new Point(0, 0),
        new TransferableNode(draggedNode), this);
  }
}
 
Example 14
Source File: bug8032667.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}
 
Example 15
Source File: bug8032667_image_diff.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 16
Source File: bug8032667.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}
 
Example 17
Source File: bug8032667_image_diff.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 18
Source File: bug8032667.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}
 
Example 19
Source File: bug8032667_image_diff.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 20
Source File: bug8032667.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static Image getImage(JComponent component) {
    final BufferedImage image = new BufferedImage(
            scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();

    return image;
}