Filling ArrayList with one line, sort of

Posted by & filed under Code Tips.

As a follow up to an earlier post on double braces in Java, came up with a short, but ugly way of filling an ArrayList via an array. Of course, at that point you might as well just do this.

Read a file into a String with one line

Posted by & filed under Code Tips.

This is one of those that I never remember, but use a lot whenever I want to pull an entire file into a String quickly. \\A is the start of the file, so Scanner is going to try and split on the beginning of the file. Since there’s only one beginning, there’s only one next.

Double brace initialization in Java

Posted by & filed under Code Tips.

Came across this the other day on Stack Overflow. Turns out there is a way to one line create an ArrayList and fill it all at the same time. It looks odd, mostly because that’s the first time I’d come across it. But it’s the same as doing this. One slight technical difference. The double… Read more »

Distance Formulas

Posted by & filed under Coding.

It’s a pretty common task to get the distance between 2 points. Maybe you’re wanting to see if two points are close enough to have collided. Maybe you’re making a golf game and the closer the ball is to the hole the higher the score. Either way, you need to know the distance. The Math… Read more »

Converting between degrees and radians

Posted by & filed under Coding.

There are two ways to measure angles. You probably learned to measure angles in degrees early in school. A circle has 360 degrees, a perfect corner is a 90 degree angle, and 180 degrees forms a straight line. But then you took trigonometry or pre-calculus and were introduced to a radian. A radian does the… Read more »

Create a Scrolling Background in Java

Posted by & filed under Coding.

Creating the next great side scroller? Well, you’ll need a scrolling background. And if you’re programming in Java, here’s an easy way to do it. Quick Demo Here’s a little demo of what we’re going to create. What’s happening is that there are two copies of the same image scrolling from the right to left…. Read more »

Get image dimensions in Java

Posted by & filed under Coding.

Earlier this week I Googled for how to find the dimensions of an image using Java, and most of the solutions were way more complicated than they needed to be. Most of what I came across was code that opened the image and parsed through the binary code to get the dimensions. Yes, there’s an… Read more »

How to create a random color in Java

Posted by & filed under Coding.

Want to create a random color in Java? With a bit of math, plus Math.random(), it’s pretty easy. A bit about colors Colors on the computer are made up of a red, green, blue triplet; typically called RGB. And each of the 3 pieces can be in the range from 0 to 255. Java also… Read more »

Copyright overkill

Posted by & filed under Computer Science.

There really are some things that don’t need to be copyrighted. Take the following example for, well, example. Oracle wanted to protect their IP. Sure, it’s only a Hello World program, but they created this specific one. If you want or need a Hello World program in Java, and don’t feel like typing in all… Read more »