Example usage for java.awt.image BufferedImage TYPE_INT_RGB

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

Introduction

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

Prototype

int TYPE_INT_RGB

To view the source code for java.awt.image BufferedImage TYPE_INT_RGB.

Click Source Link

Document

Represents an image with 8-bit RGB color components packed into integer pixels.

Usage

From source file:Main.java

public static BufferedImage int2image(int[][] scene) {
    int maxValue = -1;
    int minValue = Integer.MAX_VALUE;
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            maxValue = Math.max(maxValue, scene[y][x]);
            minValue = Math.min(minValue, scene[y][x]);
        }/*from  w ww.  j ava 2  s .  c om*/
    }

    if (maxValue == minValue)
        maxValue = minValue + 1;
    final double scale = 255.0 / (maxValue - minValue);

    BufferedImage image = new BufferedImage(scene[0].length, scene.length, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < scene.length; y++) {
        for (int x = 0; x < scene[y].length; x++) {
            final int c = (int) (scale * (scene[y][x] - minValue));
            image.setRGB(x, y, c << 16 | c << 8 | c);
        }
    }

    return image;
}

From source file:Main.java

private Image createImage() {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.getGraphics();
    g.drawString("www.java2s.com", 20, 20);

    return bufferedImage;
}

From source file:Main.java

public static Image createImage(int size, Color color) {
    BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setColor(color);// ww w .ja v a  2  s.c  o  m
    g.fillRect(0, 0, size, size);
    g.dispose();
    return image;
}

From source file:bayesGame.ui.painter.OrNodePainter.java

public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node,
        Fraction parentNode1Probability, Fraction parentNode2Probability) {

    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();

    NodePainter.graphicBackgroundPainter(g, 0, 0, size, size);

    // get non-zero truth table entries from the node
    List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities();

    // get the identities of its parents by taking the first map and querying it 
    // for KeySet, subtracting the object representing the node itself
    Set<Object> nodeParents = nonZeroEntries.get(0).keySet();
    nodeParents.remove(node.type);//from w  w  w . jav a 2  s .c  o  m

    if (nodeParents.size() > 2) {
        throw new IllegalArgumentException("OR node with more than 2 parents not yet implemented");
    }

    Object[] nodeParentsArray = nodeParents.toArray();
    Object parent1 = nodeParentsArray[0];
    Object parent2 = nodeParentsArray[1];

    // for each map, check the truth table entry it corresponds to and color
    // those appropriately
    boolean p1true_p2true = false;
    boolean p1true_p2false = false;
    boolean p1false_p2true = false;
    boolean p1false_p2false = false;

    for (Map<Object, Boolean> map : nonZeroEntries) {
        Boolean parent1truth = map.get(parent1);
        Boolean parent2truth = map.get(parent2);

        if (parent1truth && parent2truth) {
            p1true_p2true = true;
        } else if (parent1truth && !parent2truth) {
            p1true_p2false = true;
        } else if (!parent1truth && parent2truth) {
            p1false_p2true = true;
        } else if (!parent1truth && !parent2truth) {
            p1false_p2false = true;
        }
    }

    int XSize = parentNode1Probability.multiply(size).intValue();
    int X_Size = size - XSize;
    int YSize = parentNode2Probability.multiply(size).intValue();
    int Y_Size = size - YSize;

    if (p1true_p2true) {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, gridColor, Color.BLACK);
    NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, gridColor, Color.BLACK);

    if (p1false_p2false) {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    return img;
}

From source file:bayesGame.ui.painter.AndNodePainter.java

public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node,
        Fraction parentNode1Probability, Fraction parentNode2Probability) {

    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();

    NodePainter.graphicBackgroundPainter(g, 0, 0, size, size);

    // get non-zero truth table entries from the node
    List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities();

    // get the identities of its parents by taking the first map and querying it 
    // for KeySet, subtracting the object representing the node itself
    Set<Object> nodeParents = nonZeroEntries.get(0).keySet();
    nodeParents.remove(node.type);//  w  ww.  j av a  2 s . co m

    if (nodeParents.size() > 2) {
        throw new IllegalArgumentException("AND node with more than 2 parents not yet implemented");
    }

    Object[] nodeParentsArray = nodeParents.toArray();
    Object parent1 = nodeParentsArray[0];
    Object parent2 = nodeParentsArray[1];

    // for each map, check the truth table entry it corresponds to and color
    // those appropriately
    boolean p1true_p2true = false;
    boolean p1true_p2false = false;
    boolean p1false_p2true = false;
    boolean p1false_p2false = false;

    for (Map<Object, Boolean> map : nonZeroEntries) {
        Boolean parent1truth = map.get(parent1);
        Boolean parent2truth = map.get(parent2);

        if (parent1truth && parent2truth) {
            p1true_p2true = true;
        } else if (parent1truth && !parent2truth) {
            p1true_p2false = true;
        } else if (!parent1truth && parent2truth) {
            p1false_p2true = true;
        } else if (!parent1truth && !parent2truth) {
            p1false_p2false = true;
        }
    }

    Color whiteColor = Color.WHITE;

    int XSize = parentNode1Probability.multiply(size).intValue();
    int X_Size = size - XSize;
    int YSize = parentNode2Probability.multiply(size).intValue();
    int Y_Size = size - YSize;

    if (p1true_p2true) {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, falseColor, Color.BLACK);
    NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, falseColor, Color.BLACK);

    if (p1false_p2false) {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    return img;
}

From source file:com.pureinfo.srm.common.ImageHelper.java

public static void drawImage(String _sString, OutputStream _os) throws PureException {
    int nWidth = 200;
    int nHeight = 50;
    String sText = _sString;//from  ww  w. j  a v  a 2s.  c om

    BufferedImage image = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();

    for (int i = 0; i < sText.length(); i++) {
        draw(String.valueOf(sText.charAt(i)), g2, i * nWidth / sText.length(), 0,
                (i + 1) * nWidth / sText.length(), nHeight);
    }

    g2.dispose();
    try {
        EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, _os);
    } catch (Exception ex) {
        throw new PureException(PureException.UNKNOWN, "", ex);
    }
}

From source file:ImageFlip.java

public void paint(Graphics g) {
    Image myImage = new ImageIcon("yourImage.jpg").getImage();
    BufferedImage bufferedImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) g;

    Graphics gb = bufferedImage.getGraphics();
    gb.drawImage(myImage, 0, 0, null);/*from  w w  w .  ja v a 2  s.c om*/
    gb.dispose();

    AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-myImage.getWidth(null), 0);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);

    g2d.drawImage(myImage, 10, 10, null);
    g2d.drawImage(bufferedImage, null, 300, 10);
}

From source file:TexturePaintDemo.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.blue);/*from w  w  w. ja v  a  2s  .c  o m*/
    big.fillRect(0, 0, 5, 5);
    big.setColor(Color.lightGray);
    big.fillOval(0, 0, 5, 5);
    Rectangle r = new Rectangle(0, 0, 5, 5);
    g2.setPaint(new TexturePaint(bi, r));

    Rectangle rect = new Rectangle(5, 5, 200, 200);

    g2.fill(rect);
}

From source file:MainClass.java

public BufferedImage emboss(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dst;//  ww  w  . j  av  a 2s  .  c om
    dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            int upperLeft = 0;
            int lowerRight = 0;

            if (i > 0 && j > 0)
                upperLeft = src.getRGB(j - 1, i - 1);

            if (i < height - 1 && j < width - 1)
                lowerRight = src.getRGB(j + 1, i + 1);

            int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255);

            int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255);

            int blueDiff = (lowerRight & 255) - (upperLeft & 255);

            int diff = redDiff;
            if (Math.abs(greenDiff) > Math.abs(diff))
                diff = greenDiff;
            if (Math.abs(blueDiff) > Math.abs(diff))
                diff = blueDiff;

            int grayColor = 128 + diff;

            if (grayColor > 255)
                grayColor = 255;
            else if (grayColor < 0)
                grayColor = 0;

            int newColor = (grayColor << 16) + (grayColor << 8) + grayColor;

            dst.setRGB(j, i, newColor);
        }

    return dst;
}

From source file:ColorPan.java

public void paint(Graphics g) {
    int width = getSize().width;
    int height = getSize().height;
    int[] data = new int[width * height];
    int i = 0;/*w  w  w .jav a 2 s  .  c  om*/
    for (int y = 0; y < height; y++) {
        int red = (y * 255) / (height - 1);
        for (int x = 0; x < width; x++) {
            int green = (x * 255) / (width - 1);
            int blue = 128;
            data[i++] = (red << 16) | (green << 8) | blue;
        }
    }
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, width, height, data, 0, width);
    g.drawImage(image, 0, 0, this);
}