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

The following examples show how to use javax.swing.JComponent#paint() . These examples are extracted from open source projects. 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 Project: gcs   File: UIUtilities.java    License: 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
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 3
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
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 5
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 6
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 7
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
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
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 10
@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 11
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
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 13
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 14
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 15
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
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 Project: pumpernickel   File: MockComponent.java    License: 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 18
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 19
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 20
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;
}