Java Code Examples for java.awt.Graphics#clearRect()

The following examples show how to use java.awt.Graphics#clearRect() . 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 want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public void update(Graphics g) {
    if (backBuffer == null) {
        g.clearRect(0, 0, getSize().width, getSize().height);
    }
    paint(g);
}
 
Example 2
Source Project: jenetics   File: PolygonPanel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void paintComponent(final Graphics g) {
	super.paintComponent(g);
	if (_chromosome != null) {
		_chromosome.draw((Graphics2D) g, width(), height());
	} else {
		g.setColor(Color.white);
		g.clearRect(0, 0, width(), height());
	}
}
 
Example 3
Source Project: pdfxtk   File: HistogramPanel.java    License: Apache License 2.0 5 votes vote down vote up
/** Paints the histogram component.

      @param g graphics context */

  protected void paintComponent(Graphics g) {
    size = getSize(null);

    g.clearRect(0, 0, size.width, size.height);
    
    int[][] bins = histogram.getBins();
    int numBands = bins.length;

    int highValue = 0;
    for (int band = 0; band < numBands; band++) {
      for (int bin = 0; bin < bins[band].length; bin++) {
	highValue = Math.max(highValue, bins[band][bin]);
      }
    }

    for (int band = 0; band < numBands; band++) {
      g.setColor(bandColor[band]);
      for (int bin = 0; bin < bins[band].length; bin++) {
	int xpos = ((bin*numBands)+band) * size.width / (maxBins*numBands);
	int xpos2 = ((bin*numBands)+(band+1)) * size.width / (maxBins*numBands);
	int ysize = size.height * bins[band][bin] / highValue;

	g.fillRect(xpos, size.height-ysize, xpos2-xpos, ysize);
      }
    }
  }
 
Example 4
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 5
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 6
/**
 * Set contents of the current SurfaceData to default state (i.e. clear
 * the background).
 */
public void initContents() {
    // images with forced acceleration type may have a null sdCurrent
    // because we do not create a backup surface for them
    if (sdCurrent != null) {
        Graphics g = vImg.createGraphics();
        g.clearRect(0, 0, vImg.getWidth(), vImg.getHeight());
        g.dispose();
    }
}
 
Example 7
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 8
Source Project: jdk8u60   File: XYZApp.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void update(Graphics g) {
    if (backBuffer == null) {
        g.clearRect(0, 0, getSize().width, getSize().height);
    }
    paint(g);
}
 
Example 9
/**
 * Set contents of the current SurfaceData to default state (i.e. clear
 * the background).
 */
public void initContents() {
    // images with forced acceleration type may have a null sdCurrent
    // because we do not create a backup surface for them
    if (sdCurrent != null) {
        Graphics g = vImg.createGraphics();
        g.clearRect(0, 0, vImg.getWidth(), vImg.getHeight());
        g.dispose();
    }
}
 
Example 10
Source Project: openjdk-8   File: XYZApp.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void update(Graphics g) {
    if (backBuffer == null) {
        g.clearRect(0, 0, getSize().width, getSize().height);
    }
    paint(g);
}
 
Example 11
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 12
/**
 * Set contents of the current SurfaceData to default state (i.e. clear
 * the background).
 */
public void initContents() {
    // images with forced acceleration type may have a null sdCurrent
    // because we do not create a backup surface for them
    if (sdCurrent != null) {
        Graphics g = vImg.createGraphics();
        g.clearRect(0, 0, vImg.getWidth(), vImg.getHeight());
        g.dispose();
    }
}
 
Example 13
/**
 * Set contents of the current SurfaceData to default state (i.e. clear
 * the background).
 */
public void initContents() {
    // images with forced acceleration type may have a null sdCurrent
    // because we do not create a backup surface for them
    if (sdCurrent != null) {
        Graphics g = vImg.createGraphics();
        g.clearRect(0, 0, vImg.getWidth(), vImg.getHeight());
        g.dispose();
    }
}
 
Example 14
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 15
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 16
/**
 * Invokes paint and update on target Component with optimal
 * rectangular clip region.
 * If PAINT bounding rectangle is less than
 * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 * painted separately.  Otherwise the entire bounding rectangle is painted.
 *
 * @param   target Component to <code>paint</code> or <code>update</code>
 * @since   1.4
 */
public void paint(Object target, boolean shouldClearRectBeforePaint) {
    Component comp = (Component)target;

    if (isEmpty()) {
        return;
    }

    if (!comp.isVisible()) {
        return;
    }

    RepaintArea ra = this.cloneAndReset();

    if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
        subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
    }

    if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
        Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
        int square = paintRect.width * paintRect.height;
        int benefit = square - ra.paintRects[HORIZONTAL].width
            * ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
            * ra.paintRects[VERTICAL].height;
        // if benefit is comparable with bounding box
        if (MAX_BENEFIT_RATIO * benefit < square) {
            ra.paintRects[HORIZONTAL] = paintRect;
            ra.paintRects[VERTICAL] = null;
        }
    }
    for (int i = 0; i < paintRects.length; i++) {
        if (ra.paintRects[i] != null
            && !ra.paintRects[i].isEmpty())
        {
            // Should use separate Graphics for each paint() call,
            // since paint() can change Graphics state for next call.
            Graphics g = comp.getGraphics();
            if (g != null) {
                try {
                    g.setClip(ra.paintRects[i]);
                    if (i == UPDATE) {
                        updateComponent(comp, g);
                    } else {
                        if (shouldClearRectBeforePaint) {
                            g.clearRect( ra.paintRects[i].x,
                                         ra.paintRects[i].y,
                                         ra.paintRects[i].width,
                                         ra.paintRects[i].height);
                        }
                        paintComponent(comp, g);
                    }
                } finally {
                    g.dispose();
                }
            }
        }
    }
}
 
Example 17
/**
 * Invokes paint and update on target Component with optimal
 * rectangular clip region.
 * If PAINT bounding rectangle is less than
 * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 * painted separately.  Otherwise the entire bounding rectangle is painted.
 *
 * @param   target Component to <code>paint</code> or <code>update</code>
 * @since   1.4
 */
public void paint(Object target, boolean shouldClearRectBeforePaint) {
    Component comp = (Component)target;

    if (isEmpty()) {
        return;
    }

    if (!comp.isVisible()) {
        return;
    }

    RepaintArea ra = this.cloneAndReset();

    if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
        subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
    }

    if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
        Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
        int square = paintRect.width * paintRect.height;
        int benefit = square - ra.paintRects[HORIZONTAL].width
            * ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
            * ra.paintRects[VERTICAL].height;
        // if benefit is comparable with bounding box
        if (MAX_BENEFIT_RATIO * benefit < square) {
            ra.paintRects[HORIZONTAL] = paintRect;
            ra.paintRects[VERTICAL] = null;
        }
    }
    for (int i = 0; i < paintRects.length; i++) {
        if (ra.paintRects[i] != null
            && !ra.paintRects[i].isEmpty())
        {
            // Should use separate Graphics for each paint() call,
            // since paint() can change Graphics state for next call.
            Graphics g = comp.getGraphics();
            if (g != null) {
                try {
                    g.setClip(ra.paintRects[i]);
                    if (i == UPDATE) {
                        updateComponent(comp, g);
                    } else {
                        if (shouldClearRectBeforePaint) {
                            g.clearRect( ra.paintRects[i].x,
                                         ra.paintRects[i].y,
                                         ra.paintRects[i].width,
                                         ra.paintRects[i].height);
                        }
                        paintComponent(comp, g);
                    }
                } finally {
                    g.dispose();
                }
            }
        }
    }
}
 
Example 18
Source Project: Bytecoder   File: RepaintArea.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Invokes paint and update on target Component with optimal
 * rectangular clip region.
 * If PAINT bounding rectangle is less than
 * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 * painted separately.  Otherwise the entire bounding rectangle is painted.
 *
 * @param   target Component to {@code paint} or {@code update}
 * @since   1.4
 */
public void paint(Object target, boolean shouldClearRectBeforePaint) {
    Component comp = (Component)target;

    if (isEmpty()) {
        return;
    }

    if (!comp.isVisible()) {
        return;
    }

    RepaintArea ra = this.cloneAndReset();

    if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
        subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
    }

    if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
        Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
        int square = paintRect.width * paintRect.height;
        int benefit = square - ra.paintRects[HORIZONTAL].width
            * ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
            * ra.paintRects[VERTICAL].height;
        // if benefit is comparable with bounding box
        if (MAX_BENEFIT_RATIO * benefit < square) {
            ra.paintRects[HORIZONTAL] = paintRect;
            ra.paintRects[VERTICAL] = null;
        }
    }
    for (int i = 0; i < paintRects.length; i++) {
        if (ra.paintRects[i] != null
            && !ra.paintRects[i].isEmpty())
        {
            // Should use separate Graphics for each paint() call,
            // since paint() can change Graphics state for next call.
            Graphics g = comp.getGraphics();
            if (g != null) {
                try {
                    g.setClip(ra.paintRects[i]);
                    if (i == UPDATE) {
                        updateComponent(comp, g);
                    } else {
                        if (shouldClearRectBeforePaint) {
                            g.clearRect( ra.paintRects[i].x,
                                         ra.paintRects[i].y,
                                         ra.paintRects[i].width,
                                         ra.paintRects[i].height);
                        }
                        paintComponent(comp, g);
                    }
                } finally {
                    g.dispose();
                }
            }
        }
    }
}
 
Example 19
/**
 * Invokes paint and update on target Component with optimal
 * rectangular clip region.
 * If PAINT bounding rectangle is less than
 * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 * painted separately.  Otherwise the entire bounding rectangle is painted.
 *
 * @param   target Component to <code>paint</code> or <code>update</code>
 * @since   1.4
 */
public void paint(Object target, boolean shouldClearRectBeforePaint) {
    Component comp = (Component)target;

    if (isEmpty()) {
        return;
    }

    if (!comp.isVisible()) {
        return;
    }

    RepaintArea ra = this.cloneAndReset();

    if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
        subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
    }

    if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
        Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
        int square = paintRect.width * paintRect.height;
        int benefit = square - ra.paintRects[HORIZONTAL].width
            * ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
            * ra.paintRects[VERTICAL].height;
        // if benefit is comparable with bounding box
        if (MAX_BENEFIT_RATIO * benefit < square) {
            ra.paintRects[HORIZONTAL] = paintRect;
            ra.paintRects[VERTICAL] = null;
        }
    }
    for (int i = 0; i < paintRects.length; i++) {
        if (ra.paintRects[i] != null
            && !ra.paintRects[i].isEmpty())
        {
            // Should use separate Graphics for each paint() call,
            // since paint() can change Graphics state for next call.
            Graphics g = comp.getGraphics();
            if (g != null) {
                try {
                    g.setClip(ra.paintRects[i]);
                    if (i == UPDATE) {
                        updateComponent(comp, g);
                    } else {
                        if (shouldClearRectBeforePaint) {
                            g.clearRect( ra.paintRects[i].x,
                                         ra.paintRects[i].y,
                                         ra.paintRects[i].width,
                                         ra.paintRects[i].height);
                        }
                        paintComponent(comp, g);
                    }
                } finally {
                    g.dispose();
                }
            }
        }
    }
}
 
Example 20
/**
 * overload paint routine to show provide against
 * repaint errors if no office view is realy plugged
 * into this canvas.
 * If handle is present - we shouldn't paint anything further.
 * May the remote window is already plugged. In such case we
 * shouldn't paint it over.
 */
public void paint(Graphics aGraphic) {
  if (maHandle == null) {
    Dimension aSize = getSize();
    aGraphic.clearRect(0, 0, aSize.width, aSize.height);
  }
}