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.

Download 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.

Post on 19-Jan-2016

212 views

Category:

Documents

0 download

TRANSCRIPT

<ul><li><p>AppletsJava code is compiled into byte code instead of machine languageLanguages like C, C++, Pascal and others are compiled into machine language so that the programs can be directly executedIn Java, byte code cannot be directly executed so instead, it iis interpreted by a program called the Java Virtual MachineThe difference is that the JVM is part of every web browserThis 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 environmentSince the JVM is built into every web browser, we could compile a Java program into byte code which then can run on any computerTo run a Java program in a web browser, you have to write an applet instead of a normal program</p></li><li><p>JApplet ClassIn 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 methodyou move all of the code from main into the class constructor and/or init and add init( ); into the constructorSome 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 listenerto get around this problem, we create extra JPanels for the things that need listenersthis will cause us to change where and how we specify our GUI components</p></li><li><p>Simple Appletimport java.awt.*;import javax.swing.*;</p><p>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}</p></li><li><p>Continuedpublic 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);}}</p></li><li><p>Applet with TimerTo add a Timer to control action, such as having the text on the previous JApplet slide across the screenyou need to add:private Timer t; // to the JApplet classprivate int x; // to the GraphicsPanel classt = new Timer(value,gp); // makes gp the ActionListenerand start the timer in initimplements ActionListener // to the GraphicsPanel headerand an actionPerformed method to GraphicsPanelFor 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</p></li><li><p>To Display an AppletThe Applet will not run in JCreatorwrite and compile your JApplet in Jcreatorcreate an html file called app.html that consists of the following:</p><p>where name is the name of the JApplets class such as SimpleApplet, and width and height are numeric values equal to the size of your JPanelin your web browers, type in the URL of your html file, such as www.nku.edu/~username/app.htmlThe applet will load and run in your browersNOTE: 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! </p></li></ul>

Recommended

View more >