Example usage for java.awt Graphics create

List of usage examples for java.awt Graphics create

Introduction

In this page you can find the example usage for java.awt Graphics create.

Prototype

public abstract Graphics create();

Source Link

Document

Creates a new Graphics object that is a copy of this Graphics object.

Usage

From source file:Diva.java

@Override
public void paint(Graphics g, JComponent c) {
    Graphics2D g2 = (Graphics2D) g.create();

    // Paint the view.
    super.paint(g2, c);

    if (mActive) {
        // Create a radial gradient, transparent in the middle.
        java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(mX, mY);
        float radius = 72;
        float[] dist = { 0.0f, 1.0f };
        Color[] colors = { new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK };
        RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors);
        g2.setPaint(p);//from   w w w  .  j  a va 2 s .c om
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f));
        g2.fillRect(0, 0, c.getWidth(), c.getHeight());
    }

    g2.dispose();
}

From source file:ca.sqlpower.wabit.swingui.chart.WabitJFreeChartPanel.java

@Override
public void paintComponent(Graphics g) {

    if (getChart() == null) {
        return;//from   w  w  w . j  av  a 2s  .co  m
    }

    Graphics2D g2 = (Graphics2D) g.create();

    float baseline = getXaxisBaseline();

    ChartGradientPainter.paintChartGradient(g2, getBounds(), (int) baseline);

    g2.setColor(getForeground());
    super.paintComponent(g2);

    // this rendering has a different layout than last time;
    // have to paint again to update gradient position
    if (Math.abs(baseline - getXaxisBaseline()) > 1f) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                repaint();
            }
        });
    }
    g2.dispose();
}

From source file:TrackFocusDemo.java

protected void paintComponent(Graphics graphics) {
    Graphics g = graphics.create();

    //Draw in our entire space, even if isOpaque is false.
    g.setColor(Color.WHITE);//w  w  w  .  j a v  a2s .co  m
    g.fillRect(0, 0, image == null ? 125 : image.getWidth(this), image == null ? 125 : image.getHeight(this));

    if (image != null) {
        //Draw image at its natural size of 125x125.
        g.drawImage(image, 0, 0, this);
    }

    //Add a border, red if picture currently has focus
    if (isFocusOwner()) {
        g.setColor(Color.RED);
    } else {
        g.setColor(Color.BLACK);
    }
    g.drawRect(0, 0, image == null ? 125 : image.getWidth(this), image == null ? 125 : image.getHeight(this));
    g.dispose();
}

From source file:components.SizeDisplayer.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create(); //copy g
    Dimension minSize = getMinimumSize();
    Dimension prefSize = getPreferredSize();
    Dimension size = getSize();// www. j  a  v a2 s .c  o m
    int prefX = 0, prefY = 0;

    //Set hints so text looks nice.
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    //Draw the maximum size rectangle if we're opaque.
    if (isOpaque()) {
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, size.width, size.height);
    }

    //Draw the icon.
    if (icon != null) {
        Composite oldComposite = g2d.getComposite();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
        icon.paintIcon(this, g2d, (size.width - icon.getIconWidth()) / 2,
                (size.height - icon.getIconHeight()) / 2);
        g2d.setComposite(oldComposite);
    }

    //Draw the preferred size rectangle.
    prefX = (size.width - prefSize.width) / 2;
    prefY = (size.height - prefSize.height) / 2;
    g2d.setColor(Color.RED);
    g2d.drawRect(prefX, prefY, prefSize.width - 1, prefSize.height - 1);

    //Draw the minimum size rectangle.
    if (minSize.width != prefSize.width || minSize.height != prefSize.height) {
        int minX = (size.width - minSize.width) / 2;
        int minY = (size.height - minSize.height) / 2;
        g2d.setColor(Color.CYAN);
        g2d.drawRect(minX, minY, minSize.width - 1, minSize.height - 1);
    }

    //Draw the text.
    if (text != null) {
        Dimension textSize = getTextSize(g2d);
        g2d.setColor(getForeground());
        g2d.drawString(text, (size.width - textSize.width) / 2,
                (size.height - textSize.height) / 2 + g2d.getFontMetrics().getAscent());
    }
    g2d.dispose();
}

From source file:FieldValidator.java

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);

    JLayer jlayer = (JLayer) c;
    JFormattedTextField ftf = (JFormattedTextField) jlayer.getView();
    if (!ftf.isEditValid()) {
        Graphics2D g2 = (Graphics2D) g.create();

        // Paint the red X.
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int w = c.getWidth();
        int h = c.getHeight();
        int s = 8;
        int pad = 4;
        int x = w - pad - s;
        int y = (h - s) / 2;
        g2.setPaint(Color.red);//  w  ww. j  ava2s. c o  m
        g2.fillRect(x, y, s + 1, s + 1);
        g2.setPaint(Color.white);
        g2.drawLine(x, y, x + s, y + s);
        g2.drawLine(x, y + s, x + s, y);

        g2.dispose();
    }
}

From source file:IconDemoApp.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.WHITE);/*from  w  w w .  ja  v  a 2  s.com*/
    g2d.fillRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.BLACK);
    g2d.drawRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.RED);

    g2d.setStroke(stroke);
    g2d.drawLine(x + 10, y + 10, x + width - 10, y + height - 10);
    g2d.drawLine(x + 10, y + height - 10, x + width - 10, y + 10);

    g2d.dispose();
}

From source file:TapTapTap.java

@Override
public void paint(Graphics g, JComponent c) {
    int w = c.getWidth();
    int h = c.getHeight();

    // Paint the view.
    super.paint(g, c);

    if (!mIsRunning) {
        return;//w  w  w.ja v a 2  s .c  o m
    }

    Graphics2D g2 = (Graphics2D) g.create();

    float fade = (float) mFadeCount / (float) mFadeLimit;
    // Gray it out.
    Composite urComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f * fade));
    g2.fillRect(0, 0, w, h);
    g2.setComposite(urComposite);

    // Paint the wait indicator.
    int s = Math.min(w, h) / 5;
    int cx = w / 2;
    int cy = h / 2;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    g2.setPaint(Color.white);
    g2.rotate(Math.PI * mAngle / 180, cx, cy);
    for (int i = 0; i < 12; i++) {
        float scale = (11.0f - (float) i) / 11.0f;
        g2.drawLine(cx + s, cy, cx + s * 2, cy);
        g2.rotate(-Math.PI / 6, cx, cy);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, scale * fade));
    }

    g2.dispose();
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.ProfilesComparisonReportChartCanvas.java

protected void paintComponent(Graphics g) {

    // prepare graphic object
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // set clipping area
    if (is_printing) {
        g2d.translate(-draw_area.x, -draw_area.y);
        g2d.setClip(draw_area);/*from www. ja v  a  2 s. co  m*/
    }

    //paint canvas background
    if (!is_printing) {
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());
    }

    // paint white background on drawing area    
    g2d.setColor(Color.white);
    g2d.fillRect(draw_area.x, draw_area.y, draw_area.width, draw_area.height);
    if (!is_printing) {
        g2d.setColor(Color.black);
        g2d.draw(draw_area);
    }

    // paint
    paintChart(g2d);

    // dispose graphic object
    g2d.dispose();

    revalidate();
}

From source file:ScalingMethods.java

/**
 * Draws the picture five times, using the five different scaling
 * approaches described in the book. All five look the same, since
 * all are using default (nearest neighbor) filtering during the
 * scale operation./*from w  w w  .j  a  v a  2  s.c o  m*/
 */
public void paintComponent(Graphics g) {
    int x = PADDING;
    int y = PADDING;

    // Simplest approach
    g.drawImage(picture, x, y, scaleW, scaleH, null);

    // Subregion approach
    x += scaleW + PADDING;
    g.drawImage(picture, x, y, x + scaleW, y + scaleH, 0, 0, picture.getWidth(), picture.getHeight(), null);

    // Graphics2D.scale approach
    x += scaleW + PADDING;
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.translate(x, y);
    g2d.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, 0, 0, null);
    g2d.dispose();

    // AffineTransform.scale approach
    x += scaleW + PADDING;
    g2d = (Graphics2D) g.create();
    AffineTransform at = new AffineTransform();
    at.translate(x, y);
    at.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, at, null);
    g2d.dispose();

    // getScaledInstance() approach
    x += scaleW + PADDING;
    Image scaledImg = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_DEFAULT);
    g.drawImage(scaledImg, x, y, null);
}

From source file:com.nbt.TileCanvas.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.BLACK);/*from w  w  w .  j a  va  2s .  co  m*/
    int x = 0, y = 0;
    Dimension size = getSize();
    g2d.fillRect(x, y, size.width, size.height);

    final int altitude = getAltitude();
    for (int w = 0; w < getTileWidth(); w++) {
        x = w * SPRITE_SIZE;
        for (int h = 0; h < getTileHeight(); h++) {
            y = h * SPRITE_SIZE;

            int xOffset = w + getTileX();
            int zOffset = h + getTileZ();
            BufferedImage tile = getBackgroundTile(xOffset, altitude, zOffset);
            if (tile != null)
                g2d.drawImage(tile, x, y, null);
        }
    }

    g2d.dispose();
}