CS 480 - Program #
2 - Due: 2/18/02
Assignment:
Write a C++ class definition (and all appropriate member function
definitions for the class) that implements a sliding-bar control widget
that returns a floating-point value. Then use this class to
implement a program that allows the user to change the Red, Green
and Blue components of the color of a polygon that is drawn on the
screen. This will give the user a way to test various RGB
color values interactively.
Sliding-Bar Class Definition:
Use the class definition of the PushButton class (seen in class)
as a guide for creating a Sliding-Bar control widget class in
C++. Here are the data members and member functions that
you should implement for this control:
Data Members:
- leftX, rightX, topY, bottomY: the corners of
the sliding-bar control (alternatively, you could use
leftX, topY, width and height)
- labelText: an array
of characters that will act as a string that identifies
what the sliding-bar will be used for in the user interface.
- red, green, blue: the colors of the
background of the widget (set a default if you like)
- value: the current floating-point
value, corresponding to the position of the "knob" within
the sliding-bar (set a default value if you choose).
This data member must always be a value between 0.0 and 1.0,
inclusive, and represents the ratio of the distance of the
knob from the left edge of the widget
as compared to the total width of the widget.
Member Functions:
-
void drawSlider( ); - this function
draws the sliding bar on the screen,
and draws the labelText of the sliding bar
just above the left-edge of the sliding bar. The
knob of the bar should be drawn at a postion that
corresponds to the value private data member of
the widget.
-
bool isBeingUsed(int x, int y); -
returns true if a movement (mouse DRAG) event
occurs on the "knob" of the sliding-bar
and false if not.
-
float moveKnob(int x, int y); -
resets the value of the sliding-bar's value private
data member to the percent of the distance the knob
is from the left edge of the sliding-bar, and returns
this percent. Note that the knob must never
be moved off the sliding-bar background track.
- Don't forget the constructor and destructor functions
for the
class, also (see the PushButton class definition
for an example of how to write these functions).
All of the
member functions for your class should be public,
and all of the data members should be private.
Program:
Draw three sliding-bars on the screen, one for the Red component
of the color of the Polygon to be drawn by your program, one for
the Green component, and one for the Blue component. Declare
three global floating-point variables that will contain the starting red,
green and blue color values -- be sure to initialize these variables
with the same values passed into the constructor functions for the
sliders of the same color. Also,
include three PushButton objects in your program: a Button
labelled "Triangle", a button labelled "Square" and a button
labelled "Quit".
Initially, the screen should only show
your buttons and sliders. If the user clicks on the Triangle
button, draw a triangle with randomly-determined vertices in the
display window, using the colors that are indicated by the current
values of the sliders. If the user clicks on the Square button,
draw a square with randomly-determined vertices in the display.
The program should exit if the Quit button is clicked.
Your mouse callback function should be able to handle a click on
the buttons properly. You will also need a motion callback
function that will test to see if the motion event (a dragging of
the mouse) happens on the knob of one of your sliding-bars.
If so, call the moveKnob function for the sliding-bar, use the
return value of the moveKnob function to change the appropriate
color component global variable, and redraw the sliding-bar (so that the
knob position is updated on screen).
Notes:
Completing this assignment as specified above is worth 13 points (out of
a possible 15). In order to earn more points, add your own creative
touches to the program. Some suggestions might include: coloring
the screen background, buttons or other program features; more
descriptive text on the screen (in a variety of fonts), other
functionality for button or slider widget behavior, etc.
Be creative, and have fun! This assignment is to be
completed individually. Please submit a title page and printed source listing, and send a copy of your .CPP fil.e to CINDRICBB on or before the due date.