Example code - COM1370 Computer Graphics - Summer 2003
Professor Futrelle, Northeastern University, CCIS
Version of 1 July 2003
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.
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/com1370sm2003/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.
A Sun site
has many Java 2D examples.
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.
- 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.
Here's the directory
that has the single source file and the compiled file.
- 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.
- Controlling graphics with menus is shown in the simple application
ShowRectOval, which can be found here.
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.
- Local copies of a large number of Java2D code examples
can be found here.
This page
gives you access to the source and to applets you can run,
but only if you have something more than Java 1.1 as a browser plugin.
(7/03: MS will be dropping Java from its platform but Dell and HP will now
include full Java in all their systems starting in 2004, as I understand it.)
More simply, you can just compile and run the source of any of the demos
yourself. A zip file of the demos is in the directory linked above.
You can run all the apps, one after the other,
by executing the .sh or .bat file included.
You may well find a demo here that will help you with your course project.
-
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 COM1370 home page.
Return to Prof. Futrelle's home page