Example code - CSU540 Computer Graphics - Fall 2005
Professor Futrelle, Northeastern University, CCIS
Version of 4 September 2005
This page includes pointers to source code, to directories containing
sources and executables and to sites that have sample graphics code.
In particular, here is a link to a number of directories and files.
A separate, recent example, Fall 2005, is
this one on ray tracing a triangle.
If you want to ftp to the directory where the files are in order to transfer
them that way, you'll want to move to this directory:
/home/futrelle/.www/teaching/csu540f2005/examps/examps/
You are welcome to adapt any of the code you find here for your projects.
You may also want to adapt code you find elsewhere.
But it's very important that you write most of the code yourself.
It's also important that you carefully describe the origin of any
code that you adapt. I'm quick to notice code that is not code
you wrote yourself, believe me.
Using others' code and pretending it's yours is plagiarism and a violation of
the university's code of conduct.
An important limitation on adapting code is that you will have to develop data structures
and code for certain capabilities that you could bypass by using builtins,
e.g., the built-in linear transformations of Java 2D. Instead, you will be
required to build them yourself, from scratch.
A Sun site
has many Java 2D examples.
Some simple Swing examples
can be found here.
Geometry classes for ISU540 class added 24 and 25 January 2005
The first items added were
the API for the four classes.
These should be studied. Futrelle will go over the source
code in class before posting it along with the first related
assignment.
The source code for the four "geometry" classes have been added in addition.
You can access them here
Notes on selected examples
When all the examples from the Sun site I've included are added up,
there are too many to comment on individually. But below are comments
on selected ones of mine and a few from the Sun site.
See also my comments on timers,
which can be useful for animation.
- ShadedRectangle
is the beginning of our work on realism. This smoothly shades
a 2D rectangle by linear interpolation.
- TinyDraw is about as simple a Swing application as there is.
It creates a window, draws a bit of text and then creates and fills
a three-sided path, a triangle.
The single source file and the compiled file are all you need.
- Here's a bit of code
that creates and prints out some 2D transform
matrices (3x3). It doesn't do any transforms -- I leave that up to you!
That is, you'll need to write a few loops that multiply a matrix times
a vector and multiply two matrices together.
These are best defined as static methods in a single matrix class
your create,
just as are functions in the java.lang.Math class, e.g., the
Math.sin() and Math.cos() methods.
Hint: For each [i][j] in your matrix multiplication loop, you'll
want to initialize the resulting product element to 0.0 and then add (+=) the
products of the appropriate elements of the two matrices.
Just remember your linear algebra:
Mij = Σk Mik*Mkj.
- ShowRectOval Controlling graphics with menus
is shown in this simple application.
This demo uses simple AWT graphics drawing, not Java2D.
But the emphasis is on the menu control, not the graphics.
- This simple scrolling example
scrolls only text,
but it does show the use of JScrollPane.
The strategy, as shown in Scroller.java, was
to create a TextPanel, size it, create a JScrollPane holding the TextPanel
and then add the JScrollPane to the ContentPane of a JFrame.
Of course it's possible to put any of a variety of Containers in a JScrollPane
and they just work.
- SimplestDraw
draws a triangle filled with red and bordered with green.
It also draws a rectangle and a translated rectangle (not translated by
a translation matrix, but more simply).
- SwingApplication
is another extremely simple Swing example that has one
class for the GUI and another for the component that is drawn,
a single line. It uses simple graphics, not Java 2D.
- DragKing
is a nice little application that I found on the Web.
It demonstrates the use of the mouse to move objects around.
In addition, it demonstrates Java2D's ability to draw quadratic
and cubic curves (Bézier curves), curves we will discuss in
this course and which are described in our textbook.
The mouse hit is checked to see if the mouse click is within a small square region,
using Java's contains() method. The contains() method is defined
for the java.awt.Shape interface and specifically for the
java.awt.geom.Rectangle2D.Double class which implements Shape
and is used in this demo.
- Moving Line.
This is a trivial animation, a moving line.
Click in the window to pause or continue.
The line will eventually jump back to the left side
and repeat its movement.
-
Winding
is a small demo that illustrates how complex shapes are filled.
For example, the shape of the letter O is composed of an outer and
an inner contour and only the region inbetween should be filled.
This is what the "winding rule" handles.
In doing this sort of thing you have to realize that the two contours
can be drawn with the same sense, both clockwise or counterclockwise,
or with opposite senses.
Go to CSU540 homepage.
or RPF's Teaching Gateway or
homepage