JaVa
   

AWT Notice 1

Some developers prefer to use the AWT components instead of the newer Swing components. One particular reason is that earlier releases of the JRE (Java Runtime Environment) used the earlier AWT components before Swing was introduced. This is especially the case for applet games accessed from the Internet, whereby typically you as a developer (and show-off) want to reach as many people across the web as possible, all running on different machines with different versions of Java. Using Java 1.1 obviously gives you a much wider audience than using 1.4 at the moment, but that's no good. The way forward is to use the most up-to-date version of Java, especially 1.4 with full screen mode, which is cool. It may sound like a cop-out to dismiss AWT and just use Swing, but it is not. We will try to highlight differences when it comes to using AWT and Swing, beginning with a briefing on the subject and a look at how we would implement our current template graphics app and applet using only AWT classes. It is quite possible that you want to make an applet game in an earlier version of Java so that your friends can play it. For example, say they only have the Java 1.2 JRE installed on their machine and cannot update Java for whatever reason; we will pay particular attention to methods used in earlier versions of Java (for example, image loading, which has changed so much from earlier versions to the one-line command it is today supplied by the new-to-1.4 javax.imageio package's ImageIO class, as we shall see later in the chapter). As we noted from the previous chapter, AWT components are based in the java.awt package, whereas Swing components are based in the javax.swing package. The most obvious coding difference between AWT and Swing components is that Swing components are named the same as their AWT counterparts but with the letter J in front of them. For example, the Button class represents a button in AWT, whereas JButton represents a Swing button. (For an introduction to AWT and Swing, you may wish to return to the previous chapter.) If we want to alter the TemplateGraphicsapp class to AWT instead of Swing, we would need to make the following distinct changes to the code. First you need to change the class definition so that you extend a Frame and not a JFrame. Then we call setLayout(null) on the AWT Frame object itself, as the content pane is a feature new to Swing and is not part of the way AWT works. We will see more about the content pane in a moment. The changes we need to make so far are as follows:

import javax.swing.*;
import java.awt.*;
public class TemplateGraphicsapp extends Frame
{
 public TemplateGraphicsapp()
 {
 super("Template Graphics App");
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 setResizable(false);
 setLayout(null);
 // The rest of the code as before


The only thing remaining is to remove the Swing JFrame method setDefaultCloseOperation(EXIT_ON_CLOSE).

JaVa
   
Comments