下面列出了javax.swing.JComponent#paint ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
@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);
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
/**
* 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);
}
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;
}
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;
}
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;
}