Java Lecture 4 Applets

Java Lecture 4 Applets

Java Lecture 4 Applets HTML: Hypertext Markup Language lays out the syntax for web pages. The relevant parts for our purposes are shown below: <html> // start tag for html document <head> // Introduces the Head section <title>Hi</title> // Title that will appear on title bar </head> // end of head section <body> // start of body section <hr> // Horizontal line <applet // begin an applet definition code=Hi.class // class to execute name=Hi // name of applet for use on the html page width=320 // initial window width height=240 > // initial window height </applet> // end of applet definition <hr> <a href="Hi.java">The source.</a> // declares a link </body> // end of body section </html> // end of html document Here's the code for the Java image in the syllabus: <APPLET code=An.class id=An width=56 height=30 > // the param parameter passes a value to a variable in the applet <PARAM name=fps value=10> <PARAM name=imageFile value="LJN"> <PARAM name=numImages value=22> </APPLET> Applets: Some additional applet parameters for the applet tag in HTML are: CODEBASE This url specifies the base url of the applet, the default is the html document's url. ALT Text the browser can display IF it can't run the applet - but understands the applet tag. ALIGN Gives the alignment of the applet. Possible values are: BASELINE, CENTER, LEFT, MIDDLE, RIGHT, TEXTBOTTOM, TEXTMIDDLE, TEXTTOP. VSPACE Specifies the number of pixels above and below the applet. HSPACE Specifies the number of pixels on each side of the applet. Here's the class hierarchy that pertains to applets: java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet AWT means the Abstract Windows Toolkit which we will investigate later. Event Oriented: Java applets are event oriented. We'll see mouse and keyboard events later. Key events in an applets life: Loading the Applet: 1. An instance of the applet's controlling class is created (this is a subclass of Applet). 2. The applet initializes itself. 3. The applet starts running. Reloading the Applet: This occurs when the window is minimized (iconified) or goes to another page. The applet can (has the option) of stopping itself. If it stops it must restart, with the steps shown above in loading an applet. Quitting the Applet: The applet has a chance of stopping itself and doing final cleanup. And the Member methods: init Initializes the applet each time it is opened (loaded or reloaded). This is called after the constructor. This method is called by the browser or applet viewer to inform this applet that it has been loaded into the system. It is always called before the first time the start method is called. A subclass of Applet should override this method if it has initialization to perform. For example, an applet with threads would use the init method to create the threads and the destroy method to kill them. start This method is called by the browser or applet viewer to inform this applet that it should start its execution. It is called after the init method and each time the applet is revisited in a Web page. A subclass of Applet should override this method if it has any operation that it wants to perform each time the Web page containing it is visited. For example, an applet with animation might want to use the start method to resume animation, and the stop method to suspend the animation. stop This method is called by the browser or applet viewer to inform this applet that it should stop its execution. It is called when the Web page that contains this applet has been replaced by another page and also just before the applet is to be destroyed. A subclass of Applet should override this method if it has any operation that it wants to perform each time the Web page containing it is no longer visible. For example, an applet with animation might want to use the start method to resume animation, and the stop method to suspend the animation. destroy This method is called by the browser or applet viewer to inform thisapplet that it is being reclaimed and that it should destroy any resources that it has allocated. The stop method will always be called before destroy. A subclass of Applet should override this method if it has any operation that it wants to perform before it is destroyed. For example, an applet with threads would use the init method to create the threads and the destroy method to kill them. paint Paints this component. Most application components, including applets, override this method. The paint method of Component calls the repaint method of this component's peer. repaint schedules a call to component’s update method asap. update is responsible for redrawing the component. The default redraws the background and then calls paint. Here's an example: import java.applet.Applet; import java.awt.Graphics; public class Hi extends Applet { private int m_nInitCnt; private int m_nStartCnt; private int m_nStopCnt; private int m_nDestroyCnt; private int m_nPaintCnt; public Hi() { m_nInitCnt = 0; m_nStartCnt = 0; m_nStopCnt = 0; m_nDestroyCnt = 0; m_nPaintCnt = 0; } public void init() { resize(320, 240); m_nInitCnt++; } public void start() { m_nStartCnt++; } public void stop() { m_nStopCnt++; } public void destroy() { m_nDestroyCnt++; } public void paint(Graphics g) { g.drawString("Init count = " + m_nInitCnt, 0, 20); g.drawString("Start count = " + m_nStartCnt, 0, 30); g.drawString("Stop count = " + m_nStopCnt, 0, 40); g.drawString("Destroy count = " + m_nDestroyCnt, 0, 50); g.drawString("Paint count = " + ++m_nPaintCnt, 0, 60); } } Painting and Graphics: paint is a member of the Component class. It has a signature of public void paint(Graphics g) Paints this component. For example, a string would NOT be painted with println but with drawString. Most application components, including applets, override this method. The paint method of Component calls the repaint method of this component's peer. The (0, 0) coordinate of the graphics context is the top-left corner of this component. This is the MM_TEXT mapping mode of C++ Windows programming. The clipping region of the graphics context is the bounding rectangle of this component. Positive x value are to the right. The y offset has positive values going down. Parameters: g - the graphics context to use for painting The Graphics class is the abstract base class for all graphics contexts which allow an application to draw onto components or onto off-screen images. public abstract class java.awt.Graphics extends java.lang.Object { // Constructors protected Graphics() // Methods public abstract void clearRect(int x, int y, int width, int height); public abstract void clipRect(int x, int y, int width, int height); public abstract void copyArea(int x, int y, int width, int height, int dx, int dy); public abstract Graphics create(); public Graphics create(int x, int y, int width, int height); public abstract void dispose(); public void draw3DRect(int x, int y, int width, int height, boolean raised); public abstract voiddrawArc(int x, int y, int width, int height, int startAngle, int arcAngle); public void drawBytes(byte data[], int offset, int length, int x, int y); public void drawChars(char data[], int offset, int length, int x, int y); public abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer); public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer); public abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer); public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer); public abstract void drawLine(int x1, int y1, int x2, int y2); public abstract void drawOval(int x, int y, int width, int height); public abstract void drawPolygon(int xPoints[], int yPoints[], int nPoints); public void drawPolygon(Polygon p); public void drawRect(int x, int y, int width, int height); public abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight); public abstract void drawString(String str, int x, int y); public void fill3DRect(int x, int y, int width, int height, boolean raised); public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle); public abstract void fillOval(int x, int y, int width, int height); public abstract void fillPolygon(int xPoints[], int yPoints[], int nPoints); public void fillPolygon(Polygon p); public abstract void fillRect(int x, int y, int width, int height); public abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight); public void finalize(); public abstract Rectangle getClipRect(); public abstract Color getColor(); public abstract Font getFont(); public FontMetrics getFontMetrics(); public abstract FontMetrics getFontMetrics(Font f); public abstract void setColor(Color c); public abstract void setFont(Font font); public abstract void setPaintMode(); public abstract void setXORMode(Color c1); public String toString(); public abstract void translate(int x, int y); } An Event Application: An Event object has the following: public Object arg; // optional additional argument public int clickCount; // number of times in a row that mouse was pressed public Event evt; // the next event (for a linked list) public int id; // the event type (see below) public int key; // the key that was pressed public int modifiers; // Modifier state (like shift, control and alt) public Object target; //object on which the event occurred (like a window) public long when; // time when event occurred

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    13 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us