Applets• Java code is compiled into byte code instead of machine
language– Languages like C, C++, Pascal and others are compiled into
machine language so that the programs can be directly executed
– In Java, byte code cannot be directly executed so instead, it iis interpreted by a program called the Java Virtual Machine
• The difference is that the JVM is part of every web browser– This is important because a C++ program compiled for one
machine (say Windows) will not run on another (Mac or Linux) – you have to compile the program for each machine and environment
– Since the JVM is built into every web browser, we could compile a Java program into byte code which then can run on any computer
• To run a Java program in a web browser, you have to write an applet instead of a normal program
JApplet Class• In order to write an applet you extend the JApplet class
(similar to how we extended JPanel)– a program that extends a JApplet does not use a main method,
instead the JApplet class uses an init method• you move all of the code from main into the class’ constructor and/or
init and add init( ); into the constructor
• Some commands will not work now, for instance, addActionListener(this); or new Timer(time, this); – because “this” refers to this program but a JApplet is not
capable of being a listener
– to get around this problem, we create extra JPanels for the things that need listeners
– this will cause us to change where and how we specify our GUI components
Simple Appletimport java.awt.*;import javax.swing.*;
public class SimpleApplet extends JApplet {private GraphicsPanel gp;public SimpleApplet( ) {
gp = new GraphicsPanel();gp.setPreferredSize(new Dimension(400,200));add(gp);init( );
}
public void init( ) {gp.repaint( );
}// continued on next slide
}
Continued
public class GraphicsPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);g.setColor(Color.black);g.fillRect(0,0,400,200);g.setFont(new Font("Ariel", Font.BOLD, 24));g.setColor(Color.red);g.drawString("Here's a simple applet", 50, 75);
}}
Applet with Timer• To add a Timer to control action, such as having
the text on the previous JApplet slide across the screen– you need to add:
• private Timer t; // to the JApplet class
• private int x; // to the GraphicsPanel class
• t = new Timer(value,gp); // makes gp the ActionListener
• and start the timer in init
• implements ActionListener // to the GraphicsPanel header
• and an actionPerformed method to GraphicsPanel– For this example, we would use x to control where the text should
be displayed, we increment x in the actionPerformed method and we change g.drawString(“text”, 50, 75); to be (“text”, x, 75); so that we control where the String is to start
To Display an Applet• The Applet will not run in JCreator
– write and compile your JApplet in Jcreator– create an html file called app.html that consists of the
following:• <html><body><applet code=“name.class” width=value
height=value”></applet></body></html>• where name is the name of the JApplet’s class such as SimpleApplet,
and width and height are numeric values equal to the size of your JPanel
– in your web browers, type in the URL of your html file, such as www.nku.edu/~username/app.html
• The applet will load and run in your browers– NOTE: if you need to fix your JApplet, things get kind of
complicated – first , fix your code and recompile the JApplet, next click on reload in your browser – however, this may not actually load the new version of the class since the old class has been cached – instead, you may have to change the name of the JApplet and/or change the name of the html file!