Extending the GUI

In the previous three examples, you can see that you have had very little control over the actual drawing of the components; they just magically appeared on the screen once you performed the correct setup for them, and it's all very high level.

It is possible to extend the GUI and provide your own functionality for certain aspects of them, such as rendering. For example, we could extend a JButton and provide our own painting code, as follows:

public class MyButton extends JButton <>     public void paintComponent(Graphics g)     <>         // custom paint here     <> <>

Note that for Swing components derived from JComponent, not top-level Swing containers, you would override the paintComponent method, whereas with AWT components you would override the paint method.

However, even using this method we still feel that we don't have complete control over the GUI, especially if the GUI is involved in the real-time running of the game, such as in a role-playing game or real-time strategy game where GUI is an important part of the in-game functionality and not just used for the outer-game menu system. For example, in the last example we defined buttons that were actually shaped as circles, whereas the collision detection of the mouse press with the buttons involved bounding rectangle tests (i.e., the bounds of the button). It is quite possible that we may be able to override a method to provide our own mouse detection code somewhere in the depths of Swing, but quite frankly we would highly recommend creating your own GUI system.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net