Example usage for java.awt.image BufferedImage createGraphics

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

Introduction

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

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;

    TexturePaint tp;/* w w w  .  j a  v  a  2  s .  c  om*/

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    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);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    System.out.println(tp.getTransparency());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;

    TexturePaint tp;/*from  ww w  .  jav a 2s .  c o  m*/

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    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);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    tp.createContext(ColorModel.getRGBdefault(), new Rectangle(10, 10, 20, 20), null, null, null);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:com.krawler.esp.handlers.genericFileUpload.java

private BufferedImage toBufferedCompanyImage(Image image) {
    image = new ImageIcon(image).getImage();
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bufferedImage.createGraphics();
    //       java.awt.Color transparent = new java.awt.Color(255, 255, 255, 1);
    //                g.setColor(transparent);
    //                int rule = java.awt.AlphaComposite.SRC_OVER;
    //                        
    //                java.awt.AlphaComposite ac = java.awt.AlphaComposite.getInstance(rule,1);
    //                g.setComposite(ac);
    //      g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
    g.drawImage(image, 0, 0, null);//from  w w w. j a  v a 2  s. c  o m
    g.dispose();

    return bufferedImage;
}

From source file:edu.csudh.goTorosBank.WithdrawServlet.java

public String writeIntoCheck(String filePath, String username, String theAmount, String amountInWords,
        String dateWrote, String person_payingto, String billType) throws IOException, NullPointerException {

    File blueCheck = new File("blank-blue-check.jpg");
    String pathToOriginal = getServletContext().getRealPath("/" + blueCheck);

    File file = new File(pathToOriginal);
    BufferedImage imageToCopy = null;
    try {//from   w ww  . ja  v a 2 s  .  c  o m
        imageToCopy = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String amount = theAmount;
    String person_gettingPayed = person_payingto;
    String amountinWords = amountInWords;
    String date = dateWrote;
    String bill_type = billType;

    int w = imageToCopy.getWidth();
    int h = imageToCopy.getHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(g2d.getBackground());
    g2d.fillRect(0, 0, w, h);
    g2d.drawImage(imageToCopy, 0, -100, null);
    g2d.setPaint(Color.black);
    g2d.setFont(new java.awt.Font("Serif", Font.BOLD, 36));
    //g2d.setFont(new Font("Serif", Font.BOLD, 36));

    FontMetrics fm = g2d.getFontMetrics();
    int x = img.getWidth() - fm.stringWidth(amount) - 100;
    int y = fm.getHeight();
    g2d.drawString(amount, x - 70, y + 335);
    g2d.drawString(person_gettingPayed, x - 800, y + 329);
    g2d.drawString(amountinWords, x - 940, y + 390);
    g2d.drawString(date, x - 340, y + 245);
    g2d.drawString(bill_type, x - 900, y + 530);

    String signature = "Use The Force";
    g2d.setFont(new java.awt.Font("Monotype Corsiva", Font.BOLD | Font.ITALIC, 36));
    g2d.drawString(signature, x - 340, y + 530);
    g2d.dispose();
    /*write check to file*/
    String filename = fileNameGenerator(username);
    String fullname = filePath + "_" + filename + ".jpg";
    ImageIO.write(img, "jpg", new File(fullname));
    return fullname;
}

From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java

/**
 * ?/*from w  w  w . j  a  v  a2 s  . c o  m*/
 * 
 * @param source
 * @param targetW
 * @param targetH
 * @param ifScaling ?
 * @return
 */
public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) {
    // targetWtargetH
    int type = source.getType();
    BufferedImage target = null;
    double sx = (double) targetW / source.getWidth();
    double sy = (double) targetH / source.getHeight();
    // targetWtargetH??,?if else???
    if (ifScaling) {
        if (sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
    }
    if (type == BufferedImage.TYPE_CUSTOM) { // handmade
        ColorModel cm = source.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else
        target = new BufferedImage(targetW, targetH, type);
    Graphics2D g = target.createGraphics();
    // smoother than exlax:
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
    g.dispose();
    return target;
}

From source file:org.kuali.mobility.people.service.PeopleServiceImpl.java

@Override
public BufferedImage generateObfuscatedImage(String text) {
    int width = 250;
    int height = 25;

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = bufferedImage.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 14);
    g2d.setFont(font);//from ww w  .j a v  a 2  s  .  c o m

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g2d.setRenderingHints(rh);

    Paint bg = new Color(255, 255, 255);
    g2d.setPaint(bg);
    g2d.fillRect(0, 0, width, height);

    int x = 0;
    int y = height - 7;

    Paint textPaint = new Color(0, 0, 0);
    g2d.setPaint(textPaint);
    g2d.drawString(text, x, y);

    g2d.dispose();
    return bufferedImage;
}

From source file:dcstore.web.ImagesWeb.java

private void resizeImage(String inPath, int w, int h, String outPath) {
    try {/*from  w w  w. j av  a 2s .c  o m*/
        BufferedImage originalImage = ImageIO.read(new File(inPath));
        int ow, oh;
        ow = originalImage.getWidth();
        oh = originalImage.getHeight();
        double ratio = (double) ow / (double) oh;
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        int ch = (int) Math.round(w / ratio);

        BufferedImage resizedImage = new BufferedImage(w, h, type);
        Graphics2D g = resizedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
        g.drawImage(originalImage, 0, (int) (((float) h - (float) ch) / 2), w, ch, null);
        g.dispose();
        ImageIO.write(resizedImage, "jpg", new File(outPath));
    } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage("",
                new FacesMessage("Error while resizeing image: " + e.getMessage()));
    }
}

From source file:edu.csun.ecs.cs.multitouchj.application.whiteboard.ui.InteractiveCanvas.java

protected void drawPoints() {
    synchronized (previousPoints) {
        synchronized (resizableBufferedImage) {
            // clear points for pen if it's been long since last changed
            long currentTime = new Date().getTime();
            for (int id : pens.keySet()) {
                if (previousTimes.containsKey(id)) {
                    long time = previousTimes.get(id);
                    if ((currentTime - time) > POINT_CLEAR_DURATION) {
                        previousPoints.remove(id);
                        previousTimes.remove(id);
                    }//  ww w  . j a  v a2s .c om
                }
            }

            DisplayMode displayMode = getWindowManager().getDisplayManager().getCurrentDisplayMode();
            BufferedImage bufferedImage = resizableBufferedImage.getBufferedImage();

            Graphics2D graphics = bufferedImage.createGraphics();
            for (int id : pens.keySet()) {
                Pen pen = pens.get(id);
                LinkedList<Point> points = previousPoints.get(id);
                if ((pen != null) && (points != null) && (points.size() > 1)) {
                    log.debug("id: " + id + ", points: " + points.size());

                    Point previousPoint = points.removeFirst();
                    Iterator<Point> iterator = points.iterator();
                    while (iterator.hasNext()) {
                        Point point = iterator.next();
                        if (iterator.hasNext()) {
                            iterator.remove();
                        }

                        graphics.setStroke(pen.getStroke());
                        graphics.setPaint(pen.getPaint());
                        graphics.drawLine((int) Math.floor(previousPoint.getX()),
                                (displayMode.getHeight() - (int) Math.floor(previousPoint.getY())),
                                (int) Math.floor(point.getX()),
                                (displayMode.getHeight() - (int) Math.floor(point.getY())));

                        previousPoint = point;
                    }

                    setDirty(true);
                }
            }
            graphics.dispose();
        }
    }
}

From source file:IconDemoApp.java

/**
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 * //from  ww  w. j a v a 2s.c  o m
 * @param srcImg -
 *            source image to scale
 * @param w -
 *            desired width
 * @param h -
 *            desired height
 * @return - the new resized image
 */
private Image getScaledImage(Image srcImg, int w, int h) {
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = resizedImg.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(srcImg, 0, 0, w, h, null);
    g2.dispose();
    return resizedImg;
}

From source file:GraphEditorDemo.java

/**
 * copy the visible part of the graph to a file as a jpeg image
 * @param file//from  www .  j a  v a2  s  .  com
 */
public void writeJPEGImage(File file) {
    int width = vv.getWidth();
    int height = vv.getHeight();

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bi.createGraphics();
    vv.paint(graphics);
    graphics.dispose();

    try {
        ImageIO.write(bi, "jpeg", file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}