Java Reference
In-Depth Information
g2 . drawImage(xImage , ( int )getX()+1,( int )getY()+1,( int )
getWidth() 2, ( int )getHeight() 2, null );
if (isO) {
g2 . drawImage(oImage , ( int )getX()+1,( int )getY()+1,( int )
getWidth() 2, ( int )getHeight() 2, null );
}
}
}
Examine once again all the methods. Every method is simple, easy to understand, and
performs a single task. Many of the methods are called multiple times, which shortens the
overall code size. Comments are used sparingly. The method and variable names are chosen
appropriately to describe what the methods do and the purpose of the variables. The design
is elegant and easy to understand, change, and maintain. This is what we should always
strive to achieve when writing computer programs. We said it before, we will say it again.
As Martin Fowler once famously wrote, “Any fool can write code that a computer can
understand. Good programmers write code that humans can understand.” Never forget to
follow this principle, and happy programming!
15.5 Summary
The chapter presented the main principles of creating a Java Applet. Unlike regular
applications, there are security concerns associated with Java Applets. For example, a Java
Applet cannot open an arbitrary file, it cannot execute an arbitrary program, and cannot
access information about the host computer. The chapter showed an example of how the
Tic-Tac-Toe game can be coded as a Java Applet and placed inside a web browser. The AI
of the game is very simple and the computer can lose. However, the purpose of the chapter
is to give an example of a well-designed code that implements a Java Applet. The reader
could learn more about how to design the AI of a computer game by reading an introductory
book on artificial intelligence. The material on alphabeta pruning is particularly relevant
to designing the AI of a computer game.
15.6 Syntax
￿ class MyApplet extends JApplet {
...
}⇒
Creates the main JApplet class.
￿ init
Method of the JApplet class that is called first.
￿ start
Method of the JApplet class that is called every time the applet is started,
including immediately after the init method.
￿ stop
Method of the JApplet class that is called every time the applet is stopped,
including immediately before the applet is destroyed.
Method of the JApplet class that is called before the applet is destroyed.
￿
destroy
 
Search WWH ::




Custom Search