Java Reference
In-Depth Information
How It Works
Rotating elements just involves adding an extra transform before each element is drawn. Because we
draw each element at the origin, rotating an element becomes relatively simple.
Choosing Custom Colors
We made provision in the status bar for showing a custom color. It would be a shame not to make use of this,
so let's add a dialog to enable any color to be chosen. This is going to be a lot easier than you imagine.
To keep it simple, we will implement this as a facility on the general pop-up menu, although in practice
you would probably want it accessible from the main menu and the toolbar. We will add a member to
the SketchFrame class for the menu item:
private JMenuItem customColorItem;
We just need to add this to the pop-up and to add an action listener for it. This requires two statements
in the SketchFrame constructor:
customColorItem = popup.add("Custom Color..."); // Add the item
customColorItem.addActionListener(this); // and add its listener
You can add these statements following the others that set up the pop-up menu. Selecting this menu
item will now cause the actionPerformed() method in the SketchFrame class to be called so we
will implement the custom color choice in there. Let's try it out.
Try It Out - Choosing a Custom Color
We will use the facilities provided by the JColorChooser class that does precisely what we want.
Here's how we will use it in the actionPerformed() method:
Search WWH ::




Custom Search