Skip to content

Graphics Lab 02 Java Assignment

Presentation on theme: "Laboratory Study II October, 24 2003. Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of."— Presentation transcript:

1 Laboratory Study II October, 24 2003

2 Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of gas.  The user should interactively input figures for the capacity of the tank and average rate of gas consumption per mile by the car.  Write a program to convert any amount of Turkish Liras input by the user into any chosen world currency.  Write the conversion factor for the currency of your choice as a constant.  Write a program to convert a temperature that is interactively entered by the user in degrees Fahrenheit to degrees Celsius.  The formula for conversion is: Celsius = (Fahrenheit - 32) * (5/9).

3 import java.awt.*; import java.applet.Applet; public class Applet1 extends Applet { public void paint(Graphics g){ //draw triangle g.drawLine(20,20,20,100); g.drawLine(20,20,100,60); g.drawLine(100,60,20,100); } //end paint } //end triangle

4 One of the classes in the java.awt package is class Graphics. Graphics objects contain many subroutines Three of them are as follows: g.setColor(c): is called to set the color that is used for drawing.  The parameter, c is an object belonging to a class named Color, another one of the classes in the java.awt package.  About a dozen standard colors are available as static member variables in the Color class.  These standard colors include, Color.white,,, and  For example, if we want to draw in red, you would say "g.setColor(;".  The specified color is used for all drawing operations up until the next time setColor is called.

5  g.drawRect(x,y,w,h) draws the outline of a rectangle.  The parameters x, y, w, and h must be integers.  This draws the outline of the rectangle whose top-left corner is x pixels from the left edge of the applet and y pixels down from the top of the applet. The width of the rectangle is w pixels, and the height is h pixels.  g.fillRect(x,y,w,h) is similar to drawRect except that it fills in the inside of the rectangle instead of just drawing an outline.  drawString(String str, int x, int y) draws the text given by the string str.  The string is drawn using the current color and font of the graphics context. x specifies the position of the left end of the string. y is the y-coordinate of the baseline of the string. The baseline is a horizontal line on which the characters rest.


7 import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics g) { g.drawString(“Zeynep", 25, 60); g.drawString (“Ali", 25, 85); g.drawString (“Fatma", 25, 105); g.setColor(; g.fillRect(30, 40, 100, 15); g.setColor(; g.fillRect(30, 65, 135, 15); g.setColor(Color.yellow); g.fillRect(30, 90, 170, 15); } Exercise on this applet changing the borders of methods

8 import java.awt.*; import java.applet.Applet ; public class Applet3 extends Applet { public void paint(Graphics g ){ //write name labels g.drawString(“Zeynep",0,50); g.drawString(“Ali",0,75); g.drawString(“Okul",0,100); g.setColor(; //draw and label name Zeynep g.fillRect(15,25,150,20); g.drawString(“first winner",184,50); g.setColor(; //draw and label Ali g.fillRect(15,65,175,20); g.drawString(“secod winner",194,75); g.setColor(; //draw and label name Fatma g.fillRect(15,105,200,20); g.drawString(“third winner",204,100); } }

9 Compare applets Applet2 and Applet3 Exercise on applets Applet3 changing the borders of methods

10 Another applet example defining and using objects import java.awt.*; import java.awt.event.*; import java.applet.*; public class Rectangle extends Applet { public Rectangle (int x1, int y1, int x2, int y2){ this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public Rectangle( ) { System.out.println (“Drawing Rectangle”); x1=10; y1=10; x2=10; y2=10; } public int x1, y1, x2, y2; }

Programming assignment #1 - Paint program

CS 248 - Introduction to Computer Graphics
Autumn Quarter, 2001
Marc Levoy
Handout #3

Demos on Monday, October 15

Writeups due on Tuesday, October 16 by 2:00pm

Your assignment is to write a 24-bit paint program that runs on the Sweet Hall basement lab PCs, using the provided user interface package.

Required functionality

  1. Overpainting brush. Implement a simple rectangular overpainting brush as described in handout #4. Allow slider control over the color and size of your brush. For this brush and the tinting brush below, provide two sets of three sliders, one set that controls R, G, and B, and a second set that controls H, S, and V (using the single-hexcone colorspace described in section 13.3.4 of the textbook). Allow either set to be used to select the brush color, but keep the other set consistent on the display.
  2. Tinting brush. Implement a weighted mask-driven tinting brush as described in handout #4. Allow slider control over the color and size of your brush, as described above. Regardless of which set of three sliders (RGB or HSV) you use to select the color of your brush, you should perform your tinting calculations in HSV, as defined by option 2 of the section entitled "Simple tinting" in handout #4, and using the single-hexcone space described in section 13.3.4 of the textbook. Using check boxes, allow the user to select which coordinate(s) are affected: H alone, S alone, V alone, H & S, H & V, S & V, or all three. Non-affected coordinates should remain unchanged. Think carefully about interpolating hue (H), given the circularity of that axis. Choose any mask function you like that satisfies the conditions listed at the bottom of the section entitled "Weighted mask-driven tinting" in handout #4. Allow slider control over one or more parameters of your function.
  3. Brush visualizations. Draw 1x (actual size) and 4x (enlarged by 4x in both X and Y) visualizations of the current brush in a second canvas. These visualizations should depict the size, shape, and falloff of the brush, With some creativity, you can also depict the currently selected brush color in the visualizations. If you do this, then the brush visualizations should be clearly visible regardless of the brush color. Update the visualizations whenever a related slider is moved. The purpose of these visualizations is to understand the brush shape in its WYSIWYG, spatially discretized glory. Therefore, in the enlarged visualization, one pixel of the actual brush should be displayed as a 4x4 pixel constant-color block; don't interpolate for the sake of the visualization.

Support software

To eliminate X or OpenGL hacking, we provide a software package, xsupport, that displays any number of windows, called canvases, a programmable number of sliders, and several kinds of buttons. You will need at least two canvases, one for your painting and one for your brush shape visualizations. The size of these canvases is programmable. The support package runs on the Sweet Hall graphics lab PCs and most other X-compatible workstations. If you are working on a 24-bit workstation with hardware gamma correction (such as our PCs), the package allows you to display your 24-bit painting directly. If you are working on an 8-bit color workstation with no hardware gamma correction, the package automatically dithers and gamma corrects your painting prior to display.

The package is described in the file /usr/class/cs248/support/src/xsupport/README.xsupport. Everything you need to know is contained in this file. We recommend you begin the assignment by copying the directory /usr/class/cs248/assignments/assignment1 to somewhere in your home directory structure and modifying the skeleton paint program provided. To use the package, you must program in C or C++. You can develop your paint program on any platform you like, but all demos must be given on a Sweet Hall graphics lab PC. To help us judge the performance of your program, it should be displaying on the same machine it is executing on during your demo.

Some hints and additional requirements

  • The support package handles all mouse events outside the canvas; you handle mouse events inside the canvas. Painting should start when the left mouse button is pushed and the mouse is in the canvas. It should stop when the button is released. While the button is pushed, you could either paint at full throttle even if the mouse is held motionless, or you could apply a new brush instance only if the mouse moves some minimum distance. Experiment with different effects. (You only need to implement one.) Note that the support package allows you to modify the rate at which you receive mouse events if the mouse is held motionless while the button is pushed.
  • During painting, you should clip the brush action to the boundaries of the canvas. (This is required.) Your goal should be to allow unimpeded painting near the boundary even if the brush straddles the boundary. You want your painting surface to seem infinitely large, seen through a window equal to your canvas. If your clipping is working correctly, you should be able to paint the lower-leftmost pixel in your canvas using the upper-rightmost pixel in your brush even though most of the brush is beyond the canvas boundaries. Note that in keeping with the usual X convention, you retain control of the mouse when it wanders beyond the canvas boundaries as long as the button stays pressed.
  • To facilitate debugging of this and future assignments, we suggest making liberal use of a standalone pixel magnification utility, such as "xmag" in /usr/pubsw/X/bin/ (works on any X workstation) or "dynamag" in /usr/class/cs248/support/bin/linux/ (works on the Sweet Hall graphics lab PCs). Your program must include the ability to load images from .ppm files. (This ability is already built into paint.c.) If you want a test background image, large and small versions of an alpine pasture and other images are available at /usr/class/cs248/images/*.ppm. To get an intuition for how RGB and HSV color mixing work, try one of the many interactive color pickers on the web (for example at

Submission requirements

There are two parts to submitting your paint program: giving a demo on Monday, October 15, and submitting an online writeup by 2:00pm on Tuesday, October 16.

For the demos, signup sheets will be posted on the door of the Sweet Hall lab a few days before October 15. Sign up for a one-hour slot sometime during which you will be called upon to give a 10-15 minute demo. Some slots will be reserved for SITN students, who therefore don't need to come to campus just to sign up. Other students can sign up for these slots only if all other slots are taken. All demos must be given in the lab. To keep the demos fair, you must freeze your executable at 10:00am on demo day and give your demo from that executable. To freeze your executable, change the current directory to your assignment1 directory and run the script /usr/class/cs248/bin/freeze.

Writeups consist of a commented copy of your source code and a README describing the functionality implemented. Be brief but complete; one screenful is enough. If you did something especially clever, tell us about it. To submit your code and README, change the current directory to your assignment1 directory and run the script /usr/class/cs248/bin/submit.

The assignment will be graded on correctness (40 points), efficiency (20 points), programming style, including your writeup (20 points), and the elegance of your user interface (20 points). Note: the functionality, efficiency, and UI of your program will be graded based only on what you show us during the demo. Functionality you don't show us or that doesn't work will not be considered. Only your writeup and your code will be graded from your submission.

Extra credit

If you have completed the assignment you can earn extra credit by adding bells and whistles. Here are some suggestions. Feel free to make up your own.

  1. Allow interactive manipulation of brush size, shape, and fall-off directly on the visualizations displayed in the second canvas (as an alternative to sliders). Make your interface as intuitive as possible.
  2. Implement a software cursor, used only when the mouse is inside the canvas, that is more suitable for painting than the default arrow cursor. A good cursor would be visible over any image, regardless of color, and it would avoid obscuring too much of the area being painted. Segmentation faults are frowned upon, so remember to clip your cursor to the boundaries of the canvas. You can disable the hardware arrow cursor in xsupport by setting the "DisableHardwareCursor" member of the canvas structure to a non-zero value prior to calling LiftOff().
  3. Implement some fancy brushes, such as a filter brush, smear brush, color cycle brush, or rubber-stamp brush with transparency. We'll describe these and others in the Friday help session for this assignment.
  4. As a button-selectable alternative to interpolation in HSV space (option 2 in handout #4), implement interpolation in RGB space with per-pixel replacement of HSV components, as defined by option 3 in that handout. Hint: if you implement this option correctly, it will produce a behavior that is similar but not identical to the airbrush in Photoshop. In particular, if you fill a portion of the background with pure red (255,0,0), and perform hue-only painting over it with pure green (0,255,0), the first brush "stamp" will mix together with the background as you would expect, but as you drag the mouse around (with the button still pressed), subsequent brush stamps won't mix seamlessly with the first one; you'll faintly see the boundary between them. This is not a bug in your program; it's a limitation of the algorithm we have given you. Photoshop does not exhibit this anomolous behavior. For even more extra credit, diagnose this problem and implement a solution to it. This will require some thought.
  5. Implement painting on textured paper. Provide some kind of user control over the texture. For inspiration, look at MetaCreations's Painter program.
  6. Implement cloning such as found in MetaCreations's Painter. A good paper on this technique is Paul Haeberli's `Paint By Numbers: Abstract Image Representations,' Computer Graphics (Proc. Siggraph), Vol. 24, No. 4, August, 1990, pp. 207-214.
  7. Implement a fill algorithm from section 19.5 of the textbook. For more fun, implement a soft-filling algorithm.
  8. Implement unlimited undo / redo, allowing the user to "cancel" / "restore" the effect of any number of brush strokes (from button down to button up). Why unlimited? An artist working rapidly may lay down many brush strokes before realizing that a mistake has been made. Single-stroke undo is seldom sufficient. Some cleverness will be required to avoid excessive memory use.
  9. Record a painting session (multiple brushstrokes), then allow the user to change one or more brush parameters (color, size, shape, etc.), and "replay" the session, yielding a new painting. For extra fun, allow the user to control which brushstrokes are affected by a change. One way to select brushstrokes is to provide slider-controlled playback of the painting session (using your unlimited undo machinery). Another way would be geometrically, using a bounding rectangle or freeform lasso (like Photoshop).
  10. Display in a third canvas the constant H, constant S, and constant V surfaces from a hexcone or other cylindrical colorspace. Allow selection of H, S, and V for your brush from this display. When a value is selected from one of the three surface, update the other two surfaces to display the appropriate slices from the hexcone. For extra credit, add an axonometric or perspective projection of the 3D colorspace showing in color the currently selected constant H, S, and V surfaces. Copyright © 2001 Marc Levoy
Last update: October 6, 2001 02:39:27 AM