Assignment 1
Assignment 1
(1 marks)
A Java applet is an applet delivered to users in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Sun's AppletViewer, a stand-alone tool for testing applets. ii) List the uses of applets. (2marks) used to provide interactive features to web applications that cannot be provided by HTML alone. can change the provided graphic content. can be used for computation . also used to create online game collections that allow players to compete against live opponents in real-time.
Applets are created by extending The java applications start the java.applet.Applet class. execution from the main method.
(5 marks)
init() : called (only once) when the applet is first loaded to initialize variables, resize the applet, setting up GUI components, and etc. start() : called by the browser after the init() to start the applet running (similar to main() in Java application). The start() method is run every time the applet becomes active after it has been inactive. stop() : called when the user leaves the page on which the applet is running, reloads the page, or minimizes the browser, to terminate the applet's running threads.
destroy() : called when the applet is about to be purged from memory. Multithreaded applets can use the destroy() to stop any live threads. Normally, the Java garbage collector takes care of memory management. paint() : called when the applet drawing area must be refreshed, e.g., another window partially covers the applet
(5 marks)
One of the most important features of Java is its ability to draw graphics.
We can write Java applets that can draw lines,figures of different shapes,images, and text indifferent fonts, styles, and colors.
Every applet has its own area on the screen known as canvas, where it creates display.
Java coordinate system has the origin (0,0) in the upper-left corner. Positive x values are to the right and +ve y values to the bottom. The values of (x,y) are in pixels.
Graphics Class: Methods include : drawArc() draws a hollow arc drawLine() draws a straight line g.drawLine(x1,y1,x2,y2) drawOval() - draws a hollow oval g.drawLine(x, y, width, height) If width and height are same, it draws a circle. drawPolygon() - draws a hollow polygon drawRect() - draws a hollow rectangle g.drawLine(x,y,width, height) drawRoundRect() - draws a hollow round cornered rectangle
drawString() display a text string fillArc() - draw a filled arc fillOval() fillPolygon() fillRect() fillRoundRect() getColor retrieve the current drawing color getFont setColor sets the drawing color setFont
Output of Arc
Output