Thursday, September 29, 2016

CSS - Centering element

Hello,

Here is CSS using for centering element


 <html>  
 <head>  
 <style>  
   /*display table cell*/  
  .container1{  
  width: 200px;  
  height: 200px;  
  border : 1px solid red;  
  display : table-cell;  
  vertical-align: middle;  
  }  
  .content1{  
  border:1px solid blue;  
  }  
  /*CSS3 with transform*/  
  .container2{  
  margin-top:50px;  
  width: 200px;  
  height: 200px;  
  border : 1px solid red;  
  position:relative;  
  }  
  .content2{  
  border:1px solid blue;  
  position: absolute;  
  top: 50%;  
  transform: translate(0, -50%);  
  }  
  /*CSS3 with transform*/  
  .container3{  
  margin-top:50px;  
  width: 200px;  
  height: 200px;  
  border : 1px solid red;  
  display: flex;  
  align-items: center;  
  }  
  .content3{  
  border:1px solid blue;  
  }  
 </style>  
 </head>  
 <body>  
  <h2>  
 Centering vertical with table style:</h2>  
 <div class="container1">  
  <div class="content1">  
   1. This small paragraph is vertically centered.<br />  
  </div>  
 </div>  
 <h2>  
 Centering vertical CSS3 :</h2>  
 <div class="container2">  
  <div class="content2">  
   2. This small paragraph is vertically centered.<br />  
  </div>  
 </div>  
 <h2>  
 Centering vertical using Flex :</h2>  
 <div class="container3">  
  <div class="content3">  
   3. This small paragraph is vertically centered.<br />  
  </div>  
 </div>  
 </body>  
 </html>  
You can find the live demo here Plunker

Wednesday, September 21, 2016

Java - Create image inside a rectangle

Here is an example put image into a rectangle, with calculation to keep full width of image:


package my.example.image;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import javax.imageio.ImageIO;

public class ImageInRectange {

    public static void main(String[] args) throws IOException {
        createImageInRectagle(512, 1024, new File("./src/main/resources/chelsea.png"));
    }

    public static File createImageInRectagle(int width, int height, File image) throws IOException {
        Image originImage = ImageIO.read(image);
        int oImgWidth = originImage.getWidth(null);
        int oImgHeight = originImage.getHeight(null);
        float imageRatio = (float) oImgHeight / oImgWidth;
        float rectangleRatio = (float) width / height;

        BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        BufferedImage bufferedImage = ImageIO.read(image);
        Graphics g2 = combined.getGraphics();
        Point p = new Point(0, 0);
        if (Math.abs(imageRatio - rectangleRatio) < 0.05) {
            bufferedImage = scale(bufferedImage, width, height);
            p.x = 0;
            p.y = 0;
        } else {
            int minEdge = width < height ? width : height;
            if (oImgHeight > oImgWidth) {
                float diff = (float) oImgHeight / minEdge;
                int newWidth = Math.round(oImgWidth / diff);
                bufferedImage = scale(bufferedImage, newWidth, minEdge);
                if (minEdge == height) {
                    p.x = Math.abs((width - newWidth) / 2);
                    p.y = 0;
                } else {
                    p.x = 0;
                    p.y = Math.abs((height - minEdge) / 2);
                }
            } else {
                float diff = (float) oImgWidth / minEdge;
                int newHeight = Math.round(oImgHeight / diff);
                bufferedImage = scale(bufferedImage, minEdge, newHeight);
                if (minEdge == height) {
                    p.x = Math.abs((width - minEdge) / 2);
                    p.y = 0;
                } else {
                    p.x = 0;
                    p.y = Math.abs((height - newHeight) / 2);
                }
            }
        }
        g2.drawImage(bufferedImage, p.x, p.y, null);
        g2.dispose();
        File tempImage = Paths.get("target", "ok.png").toFile();
        ImageIO.write(combined, "png", tempImage);
        return tempImage;
    }

    public static BufferedImage scale(BufferedImage imgb, int newWidth, int newHeight) {
        Image image;
        image = imgb.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
        return toBufferedImage(image);
    }

    public static BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();
        return bimage;
    }
}

You can find source code on Github

Wednesday, September 14, 2016

APACHE PDFBOX EXAMPLE WITH MAVEN

Here is an example of create a Maven project using Netbean.

In this example, I used Apache PDFBox as an example of using maven for managing Java library

1. We create a new project using Netbean:



New Maven project with Netbean

Input the artifactId and package name


2. Add code


Declare maven dependenies


    4.0.0
    com.blogspot.ducnguyen.dev
    pdfbox-example
    1.0-SNAPSHOT
    jar
    
        UTF-8
        1.7
        1.7
    
    
        
        
            org.apache.pdfbox
            pdfbox
            2.0.0
        
    



Add Java Code:
package com.blogspot.ducnguyen.dev.pdfbox.example;

import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

/**
 *
 * @author nvduc
 */
public class Main {
    public static void main(String[] args) throws IOException
    {
        long timestamp = System.currentTimeMillis();
        String filename = "test-"+timestamp+".pdf";
        String message = "Hello World at : "+ timestamp;
        
        PDDocument doc = new PDDocument();
        try
        {
            PDPage page = new PDPage();
            doc.addPage(page);
            
            PDFont font = PDType1Font.HELVETICA_BOLD;

            PDPageContentStream contents = new PDPageContentStream(doc, page);
            contents.beginText();
            contents.setFont(font, 12);
            contents.newLineAtOffset(100, 700);
            contents.showText(message);
            contents.endText();
            contents.close();
            
            doc.save(filename);
        }
        finally
        {
            doc.close();
        }
    }
}



3. Running


Before running, we must build project, When we build, maven will get pdfbox library from maven repository

Build project in NetBean



Right click on Main.java file, Choose Run File

We can check the result in Files view

Result after choose Run File

You can check the source code on Github