Java Reference
In-Depth Information
If you want to try to improve this simple painting application (this is a great starter project with the
right amount of challenges), here are some suggestions:
Try using background tasks. Perhaps you don't need to call for a repaint every time the mouse is
moved while dragged; perhaps you can postpone the redraw until the movement has settled down?
Similarly, instead of adding a set of pixels based on every point the mouse touches while dragging,
you can also poll less aggressively and draw line segments between two points the mouse has been.
This solves the problem of gaps while drawing quickly, but makes it harder to draw smooth curves
when moving quickly.
Try exploring the java.awt.Canvas component. This component has been specifically designed
for custom free‐form drawing.
Finally, you can add a panel with some buttons, such as to select colors and so on. In this case,
you might want to drop the set of black pixels and work directly on and with the painting
surface. You can also try using a BufferedImage to store your image data directly (and more
efficiently).
visual gui designers: making life easy?
Earlier in this chapter, you learned that the GroupLayout and SpringLayout layout managers were
originally designed to be used in combination with visual GUI designers. So what are these exactly?
Eclipse, by default, does not come with a visual GUI designer, but you can install one by selecting
Install New Software from the Help menu and searching for the WindowBuilder packages, as shown
in Figure 11-26.
Note To make WindowBuilder understand Swing components, you will also
need to search for the “Swing Designer” packages and install these as well.
Once the new package is installed (and you restart Eclipse), try creating a new class. Right‐click it
in the Package Explorer and select Open With WindowBuilder Editor. From this editor, you can
switch between a Source and Design tab, as shown in Figure 11-27.
When you switch to the Design tab, WindowBuilder will complain about the fact that it cannot fig-
ure out that this is a GUI class. No worries; just modify the class to make it a JFrame :
import javax.swing.JFrame;
public class WindowBuilderExample extends JFrame {
public WindowBuilderExample() {
}
}
Open the Design tab once more (select Reparse when nothing appears). You will be presented with
the screen shown in Figure 11-28.
 
Search WWH ::




Custom Search