Java Code Examples for java.awt.Graphics2D#getBackground()

The following examples show how to use java.awt.Graphics2D#getBackground() . 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: Outline.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @return An {@link Img} containing the current contents of this component, minus the specified
 *         component and its children.
 */
public Img getImage() {
    Img offscreen = null;
    synchronized (getTreeLock()) {
        Graphics2D gc = null;
        try {
            Rectangle bounds = getVisibleRect();
            offscreen = Img.create(getGraphicsConfiguration(), bounds.width, bounds.height, Transparency.TRANSLUCENT);
            gc = offscreen.getGraphics();
            Color saved = gc.getBackground();
            gc.setBackground(new Color(0, true));
            gc.clearRect(0, 0, bounds.width, bounds.height);
            gc.setBackground(saved);
            Rectangle clip = new Rectangle(0, 0, bounds.width, bounds.height);
            gc.setClip(clip);
            gc.translate(-bounds.x, -bounds.y);
            paint(gc);
        } catch (Exception exception) {
            Log.error(exception);
        } finally {
            if (gc != null) {
                gc.dispose();
            }
        }
    }
    return offscreen;
}
 
Example 2
Source File: DirectScrollPanel.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @return A {@link Img} containing the currently visible header and contents (no scroll bars).
 *         If something goes wrong (unable to allocate an offscreen buffer, for instance), then
 *         {@code null} may be returned.
 */
public Img createImage() {
    int        width     = mHeaderBounds.width;
    int        height    = mHeaderBounds.height + mContentBounds.height;
    Img        offscreen = null;
    Graphics2D g2d       = null;
    synchronized (getTreeLock()) {
        try {
            offscreen = Img.create(getGraphicsConfiguration(), width, height, Transparency.TRANSLUCENT);
            g2d = offscreen.getGraphics();
            Color saved = g2d.getBackground();
            g2d.setBackground(new Color(0, true));
            g2d.clearRect(0, 0, width, height);
            g2d.setBackground(saved);
            Insets insets = getInsets();
            g2d.translate(-insets.left, -insets.top);
            Rectangle clip = new Rectangle(0, 0, width, height);
            g2d.setClip(clip);
            paint(g2d);
        } catch (Exception exception) {
            Log.error(exception);
        } finally {
            if (g2d != null) {
                g2d.dispose();
            }
        }
    }
    return offscreen;
}
 
Example 3
Source File: Graphics2DContext.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Create a Graphics2DContext based on the settings of a Graphics2D.
 * <p>
 * This assumes the incoming Graphics2D is in paint mode. (Because there
 * isn't a getter for that property.)
 */
public Graphics2DContext(Graphics2D g) {
	composite = g.getComposite();
	transform = g.getTransform();
	backgroundColor = g.getBackground();
	font = g.getFont();
	stroke = g.getStroke();
	color = g.getColor();
	paint = g.getPaint();
	renderingHints = (RenderingHints) g.getRenderingHints().clone();
	Shape s = g.getClip();
	if (s != null)
		clip = transform.createTransformedShape(s);
}
 
Example 4
Source File: Graphics2DStore.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public void save(Graphics2D g2d) {
	paint = g2d.getPaint();
	font = g2d.getFont();
	stroke = g2d.getStroke();
	transform = g2d.getTransform();
	composite = g2d.getComposite();
	clip = g2d.getClip();
	renderingHints = g2d.getRenderingHints();
	color = g2d.getColor();
	background = g2d.getBackground();
}
 
Example 5
Source File: HolesEditorPanel.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;
  RenderingHints rh = g2.getRenderingHints();
  g2.setRenderingHints(Constants.DEFAULT_RENDERING_HINTS);

  Color defaultBgColor = g2.getBackground();
  Color defaultColor = g2.getColor();

  g2.setColor(previewBb.backColor);
  g2.fill(previewArea);
  g2.setBackground(previewBb.backColor);
  g2.setColor(previewBb.borderColor);
  Stroke defaultStroke = g2.getStroke();
  g2.setStroke(previewBb.getBorder());

  if (img != null) {
    g2.drawImage(img, previewArea.x, previewArea.y, previewArea.width, previewArea.height, this);
  }

  pdp.drawGrid(g, EditableShapeConstants.gridWidth);

  g2.setColor(Color.black);
  for (int i = 0; i < shapes.size(); i++) {
    if (i != currentShape)
      g2.draw((Shape) shapes.get(i));
  }

  g2.setColor(Color.red);
  pdp.paint(g2);
  g2.setStroke(defaultStroke);
  g2.setColor(defaultColor);
  g2.setBackground(defaultBgColor);

  g2.setRenderingHints(rh);
}
 
Example 6
Source File: JigSawEditorPanel.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;
  RenderingHints rh = g2.getRenderingHints();
  g2.setRenderingHints(Constants.DEFAULT_RENDERING_HINTS);

  Color defaultBgColor = g2.getBackground();
  Color defaultColor = g2.getColor();

  g2.setColor(previewBb.backColor);
  g2.fill(previewArea);
  g2.setBackground(previewBb.backColor);
  g2.setColor(previewBb.borderColor);
  Stroke defaultStroke = g2.getStroke();
  g2.setStroke(previewBb.getBorder());

  if (img != null)
    g2.drawImage(img, previewArea.x, previewArea.y, this);

  for (int i = 0; i < shapes.size(); i++)
    g2.draw((Shape) shapes.get(i));

  g2.setStroke(defaultStroke);
  g2.setColor(defaultColor);
  g2.setBackground(defaultBgColor);

  g2.setRenderingHints(rh);
}
 
Example 7
Source File: FoldView.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void paint(Graphics2D g, Shape alloc, Rectangle clipBounds) {
    Rectangle2D.Double allocBounds = ViewUtils.shape2Bounds(alloc);
    if (allocBounds.intersects(clipBounds)) {
        Font origFont = g.getFont();
        Color origColor = g.getColor();
        Color origBkColor = g.getBackground();
        Shape origClip = g.getClip();
        try {
            // Leave component font
            
            g.setBackground(getBackgroundColor());

            int xInt = (int) allocBounds.getX();
            int yInt = (int) allocBounds.getY();
            int endXInt = (int) (allocBounds.getX() + allocBounds.getWidth() - 1);
            int endYInt = (int) (allocBounds.getY() + allocBounds.getHeight() - 1);
            g.setColor(getBorderColor());
            g.drawRect(xInt, yInt, endXInt - xInt, endYInt - yInt);
            
            g.setColor(getForegroundColor());
            g.clearRect(xInt + 1, yInt + 1, endXInt - xInt - 1, endYInt - yInt - 1);
            g.clip(alloc);
            TextLayout textLayout = getTextLayout();
            if (textLayout != null) {
                EditorView.Parent parent = (EditorView.Parent) getParent();
                float ascent = parent.getViewRenderContext().getDefaultAscent();
                String desc = fold.getDescription(); // For empty desc a single-space text layout is returned
                float x = (float) (allocBounds.getX() + EXTRA_MARGIN_WIDTH);
                float y = (float) allocBounds.getY();
                if (desc.length() > 0) {
                    
                    textLayout.draw(g, x, y + ascent);
                }
            }
        } finally {
            g.setClip(origClip);
            g.setBackground(origBkColor);
            g.setColor(origColor);
            g.setFont(origFont);
        }
    }
}