Example usage for java.awt.image BufferedImage BufferedImage

List of usage examples for java.awt.image BufferedImage BufferedImage

Introduction

In this page you can find the example usage for java.awt.image BufferedImage BufferedImage.

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:AntiAlias.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    BufferedImage image = // Create an off-screen image
            new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB);
    Graphics2D ig = image.createGraphics(); // Get its Graphics for drawing

    // Set the background to a gradient fill. The varying color of
    // the background helps to demonstrate the anti-aliasing effect
    ig.setPaint(new GradientPaint(0, 0, Color.black, 65, 35, Color.white));
    ig.fillRect(0, 0, 65, 35);//from   w  ww  . jav a  2  s. c om

    // Set drawing attributes for the foreground.
    // Most importantly, turn on anti-aliasing.
    ig.setStroke(new BasicStroke(2.0f)); // 2-pixel lines
    ig.setFont(new Font("Serif", Font.BOLD, 18)); // 18-point font
    ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
            RenderingHints.VALUE_ANTIALIAS_ON);

    // Now draw pure blue text and a pure red oval
    ig.setColor(Color.blue);
    ig.drawString("Java", 9, 22);
    ig.setColor(Color.red);
    ig.drawOval(1, 1, 62, 32);

    // Finally, scale the image by a factor of 10 and display it
    // in the window. This will allow us to see the anti-aliased pixels
    g.drawImage(image, AffineTransform.getScaleInstance(10, 10), this);

    // Draw the image one more time at its original size, for comparison
    g.drawImage(image, 0, 0, this);
}

From source file:SysTray.java

private Image getImage() throws HeadlessException {
    Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
    Image img = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon.getIconHeight(),
            BufferedImage.TYPE_4BYTE_ABGR);
    defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
    return img;//  www.ja  v a2  s  .c o m
}

From source file:eu.novait.imageresizer.helpers.ImageConverter.java

public void process() throws IOException {
    BufferedImage inImage = ImageIO.read(this.file);
    double ratio = (double) inImage.getWidth() / (double) inImage.getHeight();
    int w = this.width;
    int h = this.height;
    if (inImage.getWidth() >= inImage.getHeight()) {
        w = this.width;
        h = (int) Math.round(w / ratio);
    } else {/*from  w w w .ja va 2s  .  com*/
        h = this.height;
        w = (int) Math.round(ratio * h);
    }
    int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType();
    BufferedImage outImage = new BufferedImage(w, h, type);
    Graphics2D g = outImage.createGraphics();
    g.drawImage(inImage, 0, 0, w, h, null);
    g.dispose();
    String ext = FilenameUtils.getExtension(this.file.getAbsolutePath());
    String t = "jpg";
    switch (ext) {
    case "png":
        t = "png";
        break;
    }
    ImageIO.write(outImage, t, this.outputfile);
}

From source file:imageLines.ImageHelpers.java

/**Scale an image to the specified width and height*/
public static BufferedImage scale(BufferedImage img, int height, int width) {

    BufferedImage bdest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bdest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance((double) width / img.getWidth(),
            (double) height / img.getHeight());
    g.drawRenderedImage(img, at);//from   ww  w .  j a va 2 s.co  m
    return bdest;

}

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;//from w w  w  .j  av a  2s. co m
        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:gd.gui.GeneticDrawingView.java

public GeneticDrawingView(SingleFrameApplication app) {
    super(app);//  w  w w  .j  av  a  2s.  c  o m

    initComponents();

    ResourceMap resourceMap = getResourceMap();
    ImageIcon imageIcon = resourceMap.getImageIcon("targetImageLabel.icon");
    targetImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
    imageIcon.paintIcon(null, targetImage.getGraphics(), 0, 0);
    ProblemInstance.currentImage = targetImage;
    ProblemInstance.view = this;

    fittestDrawingView = new FittestDrawingView();
    fittestDrawingView.setVisible(false);
    fittestDrawingView.setSize(targetImage.getWidth(), targetImage.getHeight());

    try {
        startEvolution();
    } catch (InvalidConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:jadx.core.utils.android.Res9patchStreamDecoder.java

public void decode(InputStream in, OutputStream out) throws JadxException {
    try {/*  w  ww  .j a  v a2s .c o m*/
        byte[] data = IOUtils.toByteArray(in);

        BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
        int w = im.getWidth(), h = im.getHeight();

        BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
        im2.createGraphics().drawImage(im, 1, 1, w, h, null);

        NinePatch np = getNinePatch(data);
        drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
        drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);

        int[] xDivs = np.xDivs;
        for (int i = 0; i < xDivs.length; i += 2) {
            drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
        }

        int[] yDivs = np.yDivs;
        for (int i = 0; i < yDivs.length; i += 2) {
            drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
        }

        ImageIO.write(im2, "png", out);
    } catch (IOException | NullPointerException ex) {
        throw new JadxException(ex.toString());
    }
}

From source file:org.primefaces.showcase.view.multimedia.GraphicImageView.java

@PostConstruct
public void init() {
    try {/*from www  . j  a v a  2 s  .c o  m*/
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

private BufferedImage createImage() {
    BufferedImage bim;/*  w ww. ja  va  2  s .co  m*/

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    return bim;
}

From source file:edu.jhuapl.graphs.jfreechart.utils.HighResChartUtil.java

/**
 * Returns a high resolution BufferedImage of the chart. Uses the default DPI_FILE_RESOLUTION.
 *
 * @param resolution The resolution, in dots per inch, of the image to generate.
 * @return the buffered image.//from  ww w  . ja  v a 2  s .  co  m
 */
public static BufferedImage getHighResChartImage(ChartPanel chartPanel, int resolution) {
    int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
    double scaleRatio = resolution / screenResolution;
    int width = chartPanel.getWidth();
    int height = chartPanel.getHeight();
    int rasterWidth = (int) (width * scaleRatio);
    int rasterHeight = (int) (height * scaleRatio);

    BufferedImage image = new BufferedImage(rasterWidth, rasterHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();

    g2.transform(AffineTransform.getScaleInstance(scaleRatio, scaleRatio));
    chartPanel.getChart().draw(g2, new Rectangle2D.Double(0, 0, width, height), null);
    g2.dispose();

    return image;
}