There are some pieces of code that I really want to remember, but I’m not sure how to classify them. Maybe a cool method I worked on or a one-file script I found useful. Whatever the case may be, my code has found it’s way into Gist by GitHub. So whenever I need to keep a snippet of code, it’s going up to Gist. Here’s my first Gist:
Java Helper Library is a project I’ve been working on for a little bit. I found while I was coding up at work, school, and on personal projects that there are tons of methods I use consistently, so I made a class called Helper which contained a bunch of static versions of those methods. This class got really big and it was hard to maintain the right version in different projects so eventually I created a new project called “JavaHelper.”
In an effort to spread this out and maybe get some help on the project, I finally figured out git (with the help of Stack Overflow and some awesome people) and added my git repository on github.com. You can follow the project and download the latest version of the project here: https://github.com/kentcdodds/Java-Helper
Let me know what you think and I’m always open to suggestions!
This takes an image file (whether it be a resource or direct filepath) and resizes it with the given parametersThis method is pretty helpful. It returns an Image Icon with the size you specify so you can use it in a GUI. I’ll add more later, but I’m trying to answer a question on Stack Overflow right now with it, so this’ll do. UPDATE: This is now in my Java Helper Library.
/**
* This method resizes the given image using Image.SCALE_SMOOTH.
*
* @param image the image to be resized
* @param width the desired width of the new image. Negative values force the only constraint to be height.
* @param height the desired height of the new image. Negative values force the only constraint to be width.
* @param max if true, sets the width and height as maximum heights and widths, if false, they are minimums.
* @return the resized image.
*/publicstaticImage resizeImage(Image image, int width, int height, boolean max){if(width <0&& height >0){return resizeImageBy(image, height, false);}elseif(width >0&& height <0){return resizeImageBy(image, width, true);}elseif(width <0&& height <0){
PrinterHelper.printErr("Setting the image size to (width, height) of: ("+ width +", "+ height +") effectively means \"do nothing\"... Returning original image");return image;//alternatively you can use System.err.println("");//or you could just ignore this case}int currentHeight = image.getHeight(null);int currentWidth = image.getWidth(null);int expectedWidth =(height * currentWidth)/ currentHeight;//Size will be set to the height//unless the expectedWidth is greater than the width and the constraint is maximum//or the expectedWidth is less than the width and the constraint is minimumint size = height;if(max && expectedWidth > width){
size = width;}elseif(!max && expectedWidth < width){
size = width;}return resizeImageBy(image, size, (size == width));}/**
* Resizes the given image using Image.SCALE_SMOOTH.
*
* @param image the image to be resized
* @param size the size to resize the width/height by (see setWidth)
* @param setWidth whether the size applies to the height or to the width
* @return the resized image
*/publicstaticImage resizeImageBy(Image image, int size, boolean setWidth){if(setWidth){return image.getScaledInstance(size, -1, Image.SCALE_SMOOTH);}else{return image.getScaledInstance(-1, size, Image.SCALE_SMOOTH);}}publicstaticImage resizeImageFromFile(String location, int width, int height, boolean max)throwsIOException{BufferedImage image = ImageIO.read(newFile(location));return resizeImage(image, width, height, max);}publicstaticImage resizeImageFromResource(String location, int width, int height, boolean max)throwsIOException{BufferedImage image = ImageIO.read(SwingHelper.class.getResource(location));}
This project is currently in development… Stay tuned for updates
I will describe this more fully in the future. I wanted to get it up and running. The video demo shows only one side of the project and the project has changed considerably since I made that video as well. You may have a hard time running it without some explanation of how it works. I’ll upload documentation when I get the time. But take a look if you’re curious. It’s pretty powerful. It’s by far the biggest project I’ve ever had.
I have uploaded browser-reader friendly versions of my files using my CodeSyntaxBuilder as well as the whole project in a zip you can download and a runnable jar (you must have a recent version of JRE to run the jar file). The files are available below:
Jar: MGFEncode.jar
Project Zip: MGFEncode.zip
For more projects, please see my All Projects post. And please take time to see my post on Helper Methods which links to some methods I have found to be very useful.
Shorten a String in Java. You see it all the time where a long string (like a file’s location) has something like this:
C:/Users/Your_Name/Documents/Desktop/Bl…h/Blah/Blah/Blah/Blah.txt
I’ve come up with a pretty good method to do this for me in Java. It’s proven to be very helpful when trying to display a filepath or trying to name a file based on a few parameters which in some cases make the filename really long. So here’s the code for the string shortener:
/**
* This method will take a string and shorten it to the given length
* Method written by Kent Dodds (www.kentcdodds.com)
* @param originalString the text to be shortened
* @param maxLength maximum length of the string
* @param percentStart the fraction of the string length
* to give before inserting the dots (for 50% give .5)
* @return the shortened string, (or original string if not too long)
*/publicstaticString shortenString(String originalString, int maxLength, double percentStart){String newString = originalString;double endLength =(maxLength * percentStart)-3;double beginLength = maxLength *(1- percentStart);if(originalString.length()> maxLength +3){System.out.println("Old String: "+ originalString);//For debugging
newString = originalString.substring(0, (int) beginLength)+"..."+ originalString.substring(originalString.length()-(int) endLength);System.out.println("New String: "+ newString);//For debugging}else{
newString = originalString;}return newString;}
For more useful methods, please see my post on Helper Methods which links to other useful methods. And please take time to see my All Projects post to see if there’s any project I’ve done which may be useful for you or interesting in any way.
I have uploaded browser-reader friendly versions of my files using my CodeSyntaxBuilder as well as the whole project in a zip you can download and a runnable jar (you must have a recent version of JRE to run the jar file). The files are available below:
Jar: CodeSyntaxBuilder.jar
Project Zip: CodeSyntaxBuilder.zip
For more projects, please see my All Projects post. And please take time to see my post on Helper Methods which links to some methods I have found to be very useful.