Example usage for java.awt Graphics2D setColor

List of usage examples for java.awt Graphics2D setColor

Introduction

In this page you can find the example usage for java.awt Graphics2D setColor.

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawClipTest(Graphics2D g2) {
    g2.translate(10, 20);//from w  ww  .j a v a  2s  .co  m
    g2.setColor(Color.RED);
    g2.fillRect(10, 10, 100, 100);
    g2.clip(new Rectangle(0, 0, 60, 60));
    g2.setPaint(Color.BLUE);
    g2.fillRect(10, 10, 100, 100);
    g2.setClip(null);
    g2.setPaint(Color.GREEN);
    g2.fillRect(60, 60, 50, 50);
}

From source file:script.imglib.analysis.ChartUtils.java

public static final Image<RGBALegacyType> asImage(final JFreeChart chart, int width, int height) {
    ChartPanel panel = new ChartPanel(chart);
    Dimension d = panel.getPreferredSize();
    if (-1 == width && -1 == height) {
        width = d.width;/*www  . java 2s . com*/
        height = d.height;
        panel.setSize(d);
    } else {
        panel.setSize(width, height);
    }
    layoutComponent(panel);
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    if (!panel.isOpaque()) {
        g.setColor(panel.getBackground());
        g.fillRect(0, 0, width, height);
    }
    panel.paint(g);
    int[] pixels = new int[width * height];
    PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }
    g.dispose();

    Array<RGBALegacyType, IntAccess> a = new Array<RGBALegacyType, IntAccess>(new ArrayContainerFactory(),
            new IntArray(pixels), new int[] { width, height }, 1);
    // create a Type that is linked to the container
    final RGBALegacyType linkedType = new RGBALegacyType(a);
    // pass it to the DirectAccessContainer
    a.setLinkedType(linkedType);

    return new Image<RGBALegacyType>(a, new RGBALegacyType());
}

From source file:Main.java

/**
 * Creates a shadow mask/*from   ww w  . ja  v  a 2  s. co m*/
 * @param image
 * @param shadowColor
 * @param shadowOpacity
 * @return
 */
private static BufferedImage createShadowMask(BufferedImage image, Color shadowColor, float shadowOpacity) {
    BufferedImage mask = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = mask.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, shadowOpacity));
    g2d.setColor(shadowColor);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.dispose();

    return mask;
}

From source file:net.imglib2.script.analysis.ChartUtils.java

public static final Img<ARGBType> asImage(final JFreeChart chart, int width, int height) {
    final ChartPanel panel = new ChartPanel(chart);
    final Dimension d = panel.getPreferredSize();
    if (-1 == width && -1 == height) {
        width = d.width;//from w  ww. ja  va2s  .c  o  m
        height = d.height;
        panel.setSize(d);
    } else {
        panel.setSize(width, height);
    }
    layoutComponent(panel);
    final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = bi.createGraphics();
    if (!panel.isOpaque()) {
        g.setColor(panel.getBackground());
        g.fillRect(0, 0, width, height);
    }
    panel.paint(g);
    final int[] pixels = new int[width * height];
    final PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
    try {
        pg.grabPixels();
    } catch (final InterruptedException e) {
    }
    g.dispose();

    final ArrayImg<ARGBType, IntArray> a = new ArrayImg<ARGBType, IntArray>(new IntArray(pixels),
            new long[] { width, height }, 1);

    // create a Type that is linked to the container
    final ARGBType linkedType = new ARGBType(a);
    // pass it to the DirectAccessContainer
    a.setLinkedType(linkedType);

    return a;
}

From source file:JDK6SplashTest.java

static void renderSplashFrame(Graphics2D g, int frame) {
    final String[] comps = { "foo", "bar", "baz" };
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(130, 250, 280, 40);//from   w w  w  .j a v  a  2  s.  co  m
    g.setPaintMode();
    g.setColor(Color.BLACK);
    g.drawString("Loading " + comps[(frame / 5) % 3] + "...", 130, 260);
    g.fillRect(130, 270, (frame * 10) % 280, 20);
}

From source file:Main.java

public static void drawShadowText(Graphics2D g2, String s, int x, int y, Color shadowColor, int offset) {
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    Color c = g2.getColor();//from ww w. j  a v a  2 s  . c  om
    g2.setColor(shadowColor);
    g2.drawString(s, x, y + offset);
    g2.setColor(c);
    g2.drawString(s, x, y);
}

From source file:nl.ctmm.trait.proteomics.qcviewer.utils.Utilities.java

/**
 * Scale the supplied image to the specified width and height. The scale type is either {@link Utilities#SCALE_FIT}
 * to make the scaled image fit within the width by height rectangle or {@link Utilities#SCALE_FILL} to make the
 * scaled image fill the entire rectangle (and possibly go outside it in one dimension).
 *
 * @param image     the image to be scaled.
 * @param scaleType {@link Utilities#SCALE_FIT} or {@link Utilities#SCALE_FILL}.
 * @param width     the preferred width.
 * @param height    the preferred height.
 * @return the scaled image.//from w w  w.  j  av  a2  s  .co  m
 */
public static BufferedImage scaleImage(final BufferedImage image, final int scaleType, final int width,
        final int height) {
    logger.fine("scaleImage: width: " + width + " height: " + height);

    /* TODO: can we do the scaling once and save the images of the right size? [Freek]
     * This is a good idea. [Pravin]  
     * 
     * TODO: are there classes in the standard Java libraries or third party libraries that do this scaling? [Freek]
     * return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
     * This article describes use of Greaphics2D.drawImage() [Pravin]
     * http://www.mkyong.com/java/how-to-resize-an-image-in-java/
     * imgscalr is Java image scaling library available under Apache 2 License. 
     * http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/
     */
    final BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setColor(Color.white);
    graphics2D.fillRect(0, 0, width, height);
    final double imageWidth = image.getWidth();
    final double imageHeight = image.getHeight();
    final double xScale = width / imageWidth;
    final double yScale = height / imageHeight;
    double scale = 1.0;
    switch (scaleType) {
    case SCALE_FIT:
        scale = Math.min(xScale, yScale);
        break;
    case SCALE_FILL:
        scale = Math.max(xScale, yScale);
        break;
    default:
        logger.warning(String.format("Unexpected scale type: %d.", scaleType));
        break;
    }
    final double x = (width - imageWidth * scale) / 2;
    final double y = (height - imageHeight * scale) / 2;
    final AffineTransform affineTransform = AffineTransform.getTranslateInstance(x, y);
    affineTransform.scale(scale, scale);
    graphics2D.drawRenderedImage(image, affineTransform);
    graphics2D.dispose();
    return scaledImage;
}

From source file:SplashDemo.java

static void renderSplashFrame(Graphics2D g, int frame) {
    final String[] comps = { "foo", "bar", "baz" };
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(120, 140, 200, 40);/*from   w  w w . j a  v a  2 s.com*/
    g.setPaintMode();
    g.setColor(Color.BLACK);
    g.drawString("Loading " + comps[(frame / 5) % 3] + "...", 120, 150);
}

From source file:Main.java

public static void drawRoundBorder(Graphics g, Color c, int x, int y, int w, int h, int r) {
    Graphics2D g2D = (Graphics2D) g;
    Object savedRederingHint = g2D.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2D.setColor(c);
    g2D.drawRoundRect(x, y, w - 1, h - 1, r, r);
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, savedRederingHint);
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static BufferedImage eraseRect(BufferedImage image, int x, int y, int w, int h, double xScale,
        double yScale) {
    x = (int) (x * xScale);
    w = (int) (w * xScale);

    y = (int) (y * yScale);
    h = (int) (h * yScale);

    Graphics2D g = image.createGraphics();
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(x, y, w, h);//from   w w  w  . j  a v  a2s .  c  o m
    g.dispose();
    return image;
}