Fun Stuff with Java Part 2

In part 1, all we did was set up a window. In part 2, I want to show how Graphics2D can be used to draw cool stuff.

Note: I won’t show the code for creating the window or looping through the frames. I’ll assume you either have that code or some other piece of code with a game loop and a Graphics2D object.

First I want to show how to use setColor() and drawLine(). There are many other drawing commands, check the Graphics2D Javadoc for the full list. Here is the code:

And here is what it draws: The code is pretty straightforward here. I set the draw color to black, then fill a background. I then move the origin to the center of the screen, and set mag, the radius of the circle, and incAmt, the angle size of each “slice”.

The for block loops from 0 to 2PI. It first chooses a color, using the Color.getHSBColor() method. It then creates a Polygon object, and adds three points, one at the center, one at the edge of the circle, and one at the next edge. Finally it fills the polygon on the screen.

The next example is much simpler. Here is the code:

And here is what it looks like:

Note:  I’m drawing this continually each frame, so I don’t fill a rectangle at the beginning to clear each frame.  Also, I move the origin into the negative by the circles’ radius. This is because drawOval() draws from the top left, not from the center.

Each frame I choose a random color, and then draw it randomly on the screen. That’s It!


Posted in Programming | Tagged , , , | Leave a comment

Fun Stuff with Java Part 1

In this series of articles I want to show a few fun things that can be done easily in java. Most of these have to do with graphics. Programming became much more fun for me when I could visualize what I was doing on screen. Java makes it very easy to do this. In this first article, I will show how to create a window that can be drawn into using a game loop.

The first thing we are going to need is a window to draw in. Here is the complete code, I will explain it more below.

Here are the fields we use:

The JFrame is the outer window that contains all the components.

The Canvas is the component we will be drawing onto.

The BufferStrategy allows what we draw to be shown smoothly on the screen with no flickering. Go to the BufferStrategy Javadocs to learn more.

The Graphics2D class gives us nice methods for drawing geometry, text, and images.

The program will run while running == true;

In the constructor we set everything up.

The constructor takes two parameters. These are the width and height of the canvas.

We initialize the JFrame, and then set its close operation to stop the program when the window is closed.

We then initialize the canvas, and set it’s preferred size. We add the canvas to the center of the JFrame, and then pack the JFrame. Packing causes the frame to resize itself to allow the interior elements to be at their preferred size.

Then, we tell the canvas to create a BufferStrategy, and then we get a reference to it. Finally, we set the window to visible, so it will show up on the screen.

Our program uses a “game loop”, which means a function is called every frame which lets us draw to the screen. void start() is the function.

Most of the function is within a while loop.

First we get the graphics context, and then call our update function. After we have finished drawing to the screen, we dispose of the Graphics2d object and tell the BufferStrategy to draw to the screen. We then wait for 10 milliseconds.

Note: Waiting for a constant amount of time will not give constant frame rates. A better strategy is to ask how long it’s been since the last frame, and wait until a preset amount of time has passed. I’m doing this just for simplicity.

The update function is where the logic and drawing commands will go.

In this example I draw a rectangle in the center of the screen. Then I set the color to blue and draw some text in the rectangle.

The main method is pretty simple. I new up a BasicWindow, and then call start().

Here is what the finished program looks like when run.

Pretty boring, I know. But now we have a framework upon which we can do a whole lot more.

Source Code

Posted in Programming | Tagged , , , | Leave a comment

Trying out HTML5 Canvas

HTML5 canvas animation I wrote last spring.

http://www.phyloa.com/wp-content/uploads/2010/05/c1.html

Posted in Programming | Tagged , , | Leave a comment

HistoryWrite – A text editor with a timeline

Inspired by Etherpad, I decided to write my own “text editor with history” program for fun.  I implemented HistoryWrite as a desktop app written in java.

HistoryWrite contains a text area and a slider bar, and a button to change between modes. Edit mode lets you edit the latest ‘revision’, and View mode lets you view everything ever written in the file.

Currently HistoryWrite saves each ‘revision’ of the history as an array of Strings, with each string holding all the text for a certain revision.  This is not optimal by any means, because there is a large amount of redundancy. For example, if I were to type out “Hi, my name is dan.”, The history file would contain

Hi,
Hi, my
Hi, my name
Hi, my name is
Hi, my name is dan.

Obviously it would be much better to save the changes in each revision, and not just the entire text.

One of the main challenges I encountered was trying to decide when the current edit should be saved. At the moment it saves every time space or enter is pressed. This works okay, but this ends up causing there to be a large amount of revisions for a relatively short file (ex. a 500 word document would be at least 500 revisions. Yikes).

Download HistoryWrite

Posted in Programming | Leave a comment

Ray Tracing

I recently decided to delve into ray tracing again, because it’s so easy and so rewarding. The last ray tracer I wrote was slow, and there was no way to rotate or transform the scene or the camera position in any way. My new ray tracer aimed to implement these features, and to be faster. I wrote it as a renderer in my collection of java libraries I’m currently writing.

What is Ray Tracing

Ray tracing is a wonderfully simple way to render 3D scenes. For each pixel on the screen, a ray of “light” is computed shooting out into the scene. A collision test is then performed to see which objects in the scene the ray collided with. The object with the shortest distance is the one the eye “sees”. The pixel is then colored the color of that object.

A second test can be performed to see if the point is in shadow. A second ray is shot from the point of collision to the light source. If it hits another object, its in shadow, otherwise, its lit. I suggest checking out the Wikipedia page, and SuperJer’s PixelMachine.

How mine works

The core of most ray tracers contain a single recursive function used to cast a ray and return the color. My function, trace(), first finds the first object of collision, and then shoots out a ray towards the light source for shadow, and if the object is reflective, another ray is shot off at the angle of reflection to find the color that’s reflected.

The pictures

This is one of the first things I rendered after the initial test box. Its a 4×4 Lego block using the blocks library I wrote. (~20 sec)

More legos. (~20 sec)

Some pretty boring blocks. (~5 min)

Some more interesting blocks (~40 mins ). This program needs optimization badly.

This took forever to render, and didn’t even turn out that great. (~3 hours)

I’ll add some more pictures as a generate them. Generating large scenes is pretty slow still.

Posted in Programming | Tagged , , | Leave a comment