More information may be found on the Java documentation web pages hosted by Sun Microsystems, developers of Java. Here is a link to the full set of commands (called methods) available. Mostly, if you want to explore these you will be on your own, but a few are described in more detail below.
- To use a color not on the named color list: replace Color.red with new Color(255,0,0) where the numbers 255, 0, and 0 can be any number from 0 to 255, and represent (respectively) the relative proportions of red, green, and blue in the mixture.
- Making a polygon that is not a rectangle is a little bit trickier. Here is an example:
int[] x = {120,160,200}; int[] y = {20,90,20}; g.fillPolygon(x,y,3);
Note while this example uses three points for a triangle, you may use any number of points. Just be sure that the number of entries in x and y is the same, and the same number appears in the last position in fillPolygon. If you want to include more than one polygon, you'll need to use new names in place of x and y each time. - To change font details, you will need to know the exact name of the font. Here is an example:
g.setFont(new Font("Comic Sans MS",Font.ITALIC,24));
This gives 24-point italic type in the Comic Sans MS typeface (assuming you have a font of that name on your computer). Options in place of ITALIC are PLAIN and BOLD. - Customizing your card based upon input from the user involves several steps. You need to make sure there is a field available to store what the user types. You need to tell the program to ask for the input. (Fortunately, there is a prewritten Java command for this.) Finally, you need to use their input somewhere when drawing the card. Here is an example that shows two custom inputs: one changes the greeting text, and the other changes the window height. Note that in doing this we have had to make changes to several parts of the program outside paintComponent(). These places are noted with comments in all-caps.
- This is pretty complicated, but somebody asked if she could make a card that changed when somebody clicked on it. If you want to do this, take a look at this two-stage greeting card. It changes appearance when someone clicks on it. By following the example, you can make more than two stages. Here's a brief explanation of what is going on: The main function sets up a "listener" which waits for a click and respond to it. Meanwhile, there is a new field that keeps track of the stage currently being displayed. (In this example there is only stage 1 and stage 2, but you could add more.) When the listener detects a mouse click it advances to the next display stage and then tells the window to redraw itself. The paintComponent fucntion has to look at what the current display stage is, and draw the contents accordingly. This involves a conditional structure that we didn't study in class, but the mechanism may be apparent from looking at the example.