Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Creating Web Applications from Scratch

Archy is a useful tool I came across which can be used to create blank web applications.

This tool becomes extremely handy as it includes all the necessary web.xml configuration and some sample code to start with.

And its very easy to use also. I recommend all starters to use this tool to create their application base.

Maven: See deprecated API usage warnings; commandline

Maven command line option to see the warnings for deprecated code usage.

mvn clean compile -Dmaven.compiler.showDeprecation=true

http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html

ArrayList ClassCastException

When trying to typecast Object array to the Actual array type, a runtime ClassCastException is thrown.

ArrayList lst = new ArrayList();
lst.add("String 1");
lst.add("String 2");
lst.add("String 3");

String[] strArr = (String[])lst.toArray();


Solution for this is to use the toArray method with argument.

String[] strArr = new String[0];
strArr = (String[]) lst.toArray(strArr);

Time Zones in Java

Here is a complete list of Java TimeZone codes and their details.

Java Timezones Complete List

To get java.util.TimeZone instance for EST, following code can be used.


TimeZone estZone = TimeZone.getTimeZone("America/New_York");


To create a Calendar object with EST Timezone, following code can be used.


Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("America/New_York"));

Image Resize

Following is sample Java code for reading an Image dimensions and to Resize image while preserving aspect ratio.


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageTest {
public static void main(String[] args) {
try {
System.out.println("Before reading...");
BufferedImage bi = readImage("D:\\temp\\itsecurity.jpg");
System.out.println("After reading...");

printImageProperties(bi);

BufferedImage scaledBufImg = scaleImageToWidth(bi, 200);

System.out.println("Writing new img...");
File modImg = new File("D:\\temp\\itsecurity_mod.jpg");
ImageIO.write(scaledBufImg, "jpeg", modImg);

} catch (Exception e) {
e.printStackTrace();
}
}

public static BufferedImage scaleImageToWidth(BufferedImage bi, int newWidth) throws Exception {
int iw = bi.getWidth();
int ih = bi.getHeight();
// Getting aspect ratio
double ratio = (double) iw / (double) ih;
// Calculate new height
int newheight = (int) (newWidth / ratio);
System.out.println("Scaling... " + newWidth + " X " + newheight);

// Get Scaled image
Image scaledImg = bi.getScaledInstance(newWidth, newheight, Image.SCALE_SMOOTH);

// Create new writable Image
BufferedImage scaledBufImg = new BufferedImage(newWidth, newheight, BufferedImage.TYPE_INT_RGB);
scaledBufImg.getGraphics().drawImage(scaledImg, 0, 0, null);
return scaledBufImg;
}

public static void printImageProperties(BufferedImage bi) throws Exception {
System.out.println("Dimensions(WxH):" + bi.getWidth() + " X " + bi.getHeight());
System.out.println("MinX:" + bi.getMinX() + ", MinY:" + bi.getMinY());
}

public static BufferedImage readImage(String path) throws Exception {
File imageFile = new File(path);
InputStream is = new FileInputStream(imageFile);
BufferedImage bi = ImageIO.read(is);
return bi;
}

public static BufferedImage readImage(byte[] bytes) throws Exception {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BufferedImage bi = ImageIO.read(bis);
return bi;
}
}