/* * Scribble * -------- * This program lets the user scribble on a window * with the mouse. It was adapted from the applet * found on pages 10-12 of "Java in a Nutshell", * 2nd edition, by David Flanagan, as a comparison. */ #include /** Scribble application variables: **/ point last_position; // Store the last mouse position. rgb current_colour; // Store the current colour. button clear_button; // Button to clear the window. listbox colour_choices; // The colour dropdown list. char * colour_names[] = { "Black", "Red", "Yellow", "Green" }; rgb colours[] = { Black, Red, Yellow, Green }; /** This function is called when the user clicks the mouse ** to start scribbling. **/ void mouseDown(window w, int buttons, point p) { last_position = p; } /** This function is called when the user drags the mouse. **/ void mouseDrag(window w, int buttons, point p) { setcolour(current_colour); drawline(last_position, p); last_position = p; } /** This function is called when the user clicks on the ** clear button to clear the window. **/ void selectClear(button b) { clear(parentwindow(b)); } /** This function is called when the user selects a ** colour from the dropdown list. **/ void selectColour(listbox d, int i) { current_colour = colours[i]; } /** This function is called to initialise the Scribble ** application, from the main function. **/ void init(void) { // Create the application window. window w = newwindow("Scribble", rect(50,50,480,400), StandardWindow); show(w); setmousedown(w, mouseDown); setmousedrag(w, mouseDrag); // Set the background colour. setbackground(w, White); // Create a button and add it to the window. clear_button = newbutton("Clear", rect(100,10,80,24), selectClear); // Create a dropdown list of colours and add it // to the window, with a label saying what it is. newlabel("Colour: ", rect(200,10,80,24), AlignCenter); colour_choices = newdroplist(colour_names, rect(300,10,80,24), selectColour); // Initialise current colour. setlistitem(colour_choices, 0); } /** The main function is the starting point for the ** application, and it is here that everything is ** initialised. **/ int main(int argc, char *argv[]) { init(); mainloop(); return 0; }