Java 2D Warmup

CS3610 Assignment #2

 

Out: Tue, 02/05. Due: Tue, 02/12, 12pm

Estimated workload: 1.5 hours

 

 

 

Goals

  • Gain an understanding of the basic capabilities of Java2D and of its use in graphic presentation.
  • Learn what a Java programmer can accomplish in terms of graphics with relative little effort.
  • Learn to evaluate correctly the limitations of Java2D and graphics programming.

 

 

 

The Assignment: Java 2D Is Your Friend



Java 2D overview:

In computer science terms, Java 2D is an API for drawing two-dimensional graphics using the Java programming language. Basically, if you can program in Java, you can also program 2D graphics (i.e., draw colored rectangles, circles, lines, and animate them). At least one person in your group knows how to program in Java; in this assignment they will learn how to use Java 2D in conjunction with their Java programming. If you are not programming proficient, you will still learn what your programming team-mate will be able to do and what they will not be able to do in terms of 2D graphics by the end of the semester.


At a conceptual level, every Java 2D drawing operation can be thought of as filling a programmer-specified shape using a programmer-specified paint and compositing the result onto the screen.



Information for the team

Step 0: Set up a meeting time with your team. It will most likely be inside the CS department -- check with your team's programmer.


Step 1: The team-programer logs onto a machine with Java capabilities, reads through, compiles and runs a few Java2D example programs (see Information for the Team-Programmer section below for details).


Step 2: The team takes turns in front of the machine and tests the interactive Java2D examples.


Step 3a: The team-programmer experiments with the Java2D source code (try different colors, shapes, images, menus). Make sure you explain to the rest of the team what you're doing and why. Grab (with gimp) a snapshot of your most interesting output and submit it via email to the instructor.


Step 3b: The rest of the team writes one paragraph describing what kind of images they think the team-programmer can produce by using Java2D (e.g., "I think Yinglin can generate color-images of network structures like the ones Jim Faeder showed in class last time -- he can read in a file specification and draw colored circles connected by lines etc. Moreover, I think Yinglin can make it so that a user like me could interact with those colored nodes and links -- move them around with the mouse or make them disappear."). This part of the assignment is non-collaborative -- each team member has to write and submit via email to the instructor their own paragraph.


Information for the team programmer

For this assignment you will need access to a machine that has Java JDK version 1.4 (or later) installed. The CS Windows machines have Java JDK and JRE version 1.6.0 (Jave 6) installed. You will find the java compiler and builder in C:/Program Files/Java/jdk1.5.0_06/bin/javac and C:/Program Files/Java/jdk1.5.0_06/bin/java.

The CS Linux machines also have JDK 1.6.0 installed. Under Linux, use /usr/local/jdk1.6.0/bin/javac and /usr/local/jdk1.6.0/bin/java.


For your laptop and home machine, Java 2D and documentation are available for download as a part of JDK (1.)6.

Java 2D API classes are organised into the following packages in JDK 6:
* java.awt This is the main package for the Java Abstract Window Toolkit.
* java.awt.geom This is the Java standard library of two dimensional geometric shapes such as lines, ellipses, and quadrilaterals.
* java.awt.font This is the library for manipulating glyphs in Java.
* java.awt.color This is the library of tools for dealing with the many different ways that color can be represented.
* java.awt.image This is the library for manipulating graphical images.
* java.awt.image.renderable
* java.awt.print This is the library of tools for writing to paper.

Some useful resources are:
Sun's 2D Graphics Tutorial
Sun's Java 2D Programmer's Guide
Sun's description of all the Java packages and classes.

One assignment is obviously not enough to give one indepth knowledge of Java2D. However, the following examples should give you the basics of 2D graphics programming in Java. You'll find out that most of your drawing code from now on will be copy-and-paste snippets from the code below. Please read through, compile and run the following tutorial-style examples:
* loading an image: LoadImageApp.java, you'll also need this image file: strawberry.jpg (example courtesy of Sun)
* drawing various colored shapes (lines, rectangles, circles, ellipses, text): ex1.java (example courtesy of John Ramirez)
* drawing fancier colored shapes (varying thickness, gradient fill etc): ShapesDemo2D.java (example courtesy of Sun)
* drawing colored shapes interactively -- with menus (also shows how to read mouse events: ex2.java (example courtesy of John Ramirez)
* drawing shapes interactively with the mouse: ex13.java (example courtesy of John Ramirez)
* animated arc (pacman): Arcs.java (example courtesy of Sun)
* fancy animated colored balls: Balls.java (example courtesy of Sun)

The examples are commented in-depth -- and all you need to do at this point is, be able to modify some of the renderings (e.g, change the code so the color or shape of an object changes, change the menu names and actions, change the effect of mouse actions etc.). Note that all the drawing happens through the 'paint' or 'paintComponent' function.

For the curios, additional examples are available here.