Archive

Posts Tagged ‘java’

Gist, my new scratchpad

March 12th, 2013 No comments

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:

Categories: Source Code Tags: , , ,

Java Helper Library

April 13th, 2012 No comments

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!

Resize Image method in Java

February 10th, 2012 No comments

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.
  */
  public static Image resizeImage(Image image, int width, int height, boolean max) {
    if (width < 0 && height > 0) {
      return resizeImageBy(image, height, false);
    } else if (width > 0 && height < 0) {
      return resizeImageBy(image, width, true);
    } else if (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 minimum
    int size = height;
    if (max && expectedWidth > width) {
      size = width;
    } else if (!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
  */
  public static Image 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);
    }
  }
 
  public static Image resizeImageFromFile(String location, int width, int height, boolean max) throws IOException {
    BufferedImage image = ImageIO.read(new File(location));
    return resizeImage(image, width, height, max);
  }
 
  public static Image resizeImageFromResource(String location, int width, int height, boolean max) throws IOException {
    BufferedImage image = ImageIO.read(SwingHelper.class.getResource(location));
  }

Batch Video Encoder in Java

January 17th, 2012 No comments


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

Individual Files:
broadscope package:

encodequerygenerator package:

encodergui package:

folderfinder package:

metadatagenerator package:

metagui package:

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

January 15th, 2012 No comments

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)
 */
public static String shortenString(String originalString, int maxLength, double percentStart) {
  String newString = originalString;
  double endLength = (maxLength * percentStart) - 3;
  double beginLength = maxLength * (1 - percentStart);
  if (originalString.length() &gt; 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.

Code Syntax Builder in Java

December 30th, 2011 No comments


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

Individual Files:
back package:

folderfind package:

front package:

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.

Protected: Retail Software GUI Demo in Java (MyStuff)

December 24th, 2011 Enter your password to view comments.

This post is password protected. To view it please enter your password below:

Protected: Enterprise Software GUI Demo in Java (Intex – SilverLinings)

December 24th, 2011 Enter your password to view comments.

This post is password protected. To view it please enter your password below:

Protected: DOS Attack Scanner in Java (Final)

December 24th, 2011 Enter your password to view comments.

This post is password protected. To view it please enter your password below:

Protected: Web Parser in Java (Today’s News)

November 26th, 2011 Enter your password to view comments.

This post is password protected. To view it please enter your password below:

buy flagyl no prescription cialis soft