I have a sprite sheet which has each image centered in a 32×32 cell. The actual images are not 32×32, but slightly smaller. What I’d like to do is take a cell and crop the transparent pixels so the image is as small as it can be. How would I do that in Java (JDK 6)? Here is an example
Tag: java
Comparing Numbers in Java
In Java, all numeric types extend from java.lang.Number. Would it be a good idea to have a method like the following: I’m concerned about cases where a double 2.00000 does not equal an int 2. Are these handled by the built-in equals? If not, is there any way to write a simple number compare function in …
Unit testing mathematical code
I am writing a small utility for calculating a complicated mathematical formula (using commons-math library for integration and root finding). I was trying to write it in the same way as a normal …
What is the convention for word separator in Java package names?
How should one separate words in package names? Which of the following are correct? com.stackoverflow.my_package (Snake Case using underscore) com.stackoverflow.my-package (Kebab Case using hyphens) com.stackoverflow.myPackage (Camel Case) com.stackoverflow.MyPackage (Pascal Case) What is the general standard…
Java : convert List of Bytes to array of bytes
Trying to solve what should be a simple problem. Got a list of Bytes, want to convert it at the end of a function to an array of bytes. compiler doesn’t like syntax on my toArray. How to fix this? Answer The compiler doesn’t like it, because byte[] isn’t Byte[]. What you can do is use common…
Reverse bits in number
For example, I have the binary number 1011 which is equal to decimal 11. I want the reverse bit’s location such that it become 1101, which is decimal 13. Here is code: But when I enter x 11 then it prints 26. What is the mistake? Answer You are shifting b one time too many. Do the shift first (so
Android VideoView not playing sample video on T-Mobile G2 (just audio)
What I’m trying to do is play the first video retrieved from the external SD card, which on my T-Mobile G2 turns out to be the sample video for the phone. Now I assumed that since it plays in the phones video player, that it’d have no problems playing in the VideoView in my test app. However, all …
How do I iterate through the files in a directory and it’s sub-directories in Java?
I need to get a list of all the files in a directory, including files in all the sub-directories. What is the standard way to accomplish directory iteration with Java? Answer You can use File#isDirectory() to test if the given file (path) is a directory. If this is true, then you just call the same method aga…
Persisting set of Enums in a many-to-many unidirectional mapping
I’m using Hibernate 3.5.2-FINAL with annotations to specify my persistence mappings. I’m struggling with modelling a relationship between an Application and a set of Platforms. Each application is available for a set of platforms. From all the reading and searching I’ve done, I think I need …
Handling nested elements in JAXB
I am wondering if it is possible to have JAXB not to create Java object for XML elements that serve as wrappers. For example, for XML of the following structure I do not want an object for <wrapper> to be created at all. So for a class like the <entity> element should be unmarshalled directly into…