final Graphics getGraphics_NoClientCode() { ComponentPeer peer = this.peer; if (peer instanceof LightweightPeer) { // This is for a lightweight component, need to // translate coordinate spaces and clip relative // to the parent. Container parent = this.parent; if (parent == null) return null; Graphics g = parent.getGraphics_NoClientCode(); if (g == null) return null; if (g instanceof ConstrainableGraphics) { ((ConstrainableGraphics) g).constrain(x, y, width, height); } else { g.translate(x,y); g.setClip(0, 0, width, height); } g.setFont(getFont_NoClientCode()); return g; } else { return (peer != null) ? peer.getGraphics() : null; } }
/** * Run the test: cycle through all the rectangles, use one as clip and * another as the area to render to. * Set the clip in the same way Swing does it when repainting: * first constrain the graphics to the damaged area, and repaint everything */ void runTest(Graphics2D destGraphics) { destGraphics.setColor(Color.black); destGraphics.fillRect(0, 0, IMG_W, IMG_H); destGraphics.setColor(Color.red); for (Rectangle clip : rects) { Graphics2D g2d = (Graphics2D)destGraphics.create(); g2d.setColor(Color.red); // mimic what swing does in BufferStrategyPaintManager if (g2d instanceof ConstrainableGraphics) { ((ConstrainableGraphics)g2d).constrain(clip.x, clip.y, clip.width, clip.height); } g2d.setClip(clip); for (Rectangle renderRegion : rects) { if (renderRegion != clip) { // from CellRendererPane's paintComponent Graphics2D rG = (Graphics2D) g2d.create(renderRegion.x, renderRegion.y, renderRegion.width, renderRegion.height); rG.fillRect(0,0, renderRegion.width, renderRegion.height); } } } }