ASSIGNMENT 1
F5105 MDM NURUL ZAKIAH BT KASNUN
NORSYAHANIM BINTI SULAIMAN 17DIP08F2011
NUR ATIQAH ZAHRAH BINTI ADLAN 17DIP08F2014 HASLIZA BINTI SAGAP 17DIP08F2015
1 (i) Define an applet.
(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.
2. Differentiate between Java Applets and Java Application. (2 marks)
Java Applets Java Applets are the small programs. Applets are designed just for handling the client site problems. The applets are designed to typically used. Java Application Java Applications are larger programs Java applications are designed to work with the client as well as server. Applications are designed to exists in a secure area.
Applets are created by extending The java applications start the java.applet.Applet class. execution from the main method.
3. Sketch and explain the life cycle of an applet.
(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
4. Explain the Graphic class and its methods.
(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
Drawing Lines and Rectangles
import java.awt.* ; import java.applet.* ; public class LineRect extends Applet { public void paint(Graphics g) { g.drawLine(10,10,50,50); g.drawRect(10,60,40,30); g.fillRect(60,10,30,80); g.drawRoundRect(10,100,80,50,10,10); g.fillRoundRect(20,110,60,30,5,5); g.drawLine(100,10,230,140); g.drawLine(100,140,230,10); } }
Output of LineRect applet
Drawing a Happy Face Applet
import java.awt.* ; import java.applet.* ; public class Face extends Applet { public void paint(Graphics g) { g.drawOval(40,40,120,150); / /Head g.drawOval(57,75,30,20); / /Left eye g.drawOval(110,75,30,20); / /Right eye g.fillOval(68,81,10,10); / /Pupil (left) g.fillOval(121,81,10,10); / /Pupil (right) g.drawOval(85,100,30,30); / /Nose g.fillArc(60,125,80,40,180,180); / /Mouth g.drawOval(25,92,15,30); / /Left ear g.drawOval(160,92,15,30); / /Right ear } }
Output of Arc
Polygon example code
import java.awt.* ; import java.applet.* ; public class Poly extends Applet { int x1[ ]= { 20,120,220,20} ; int y1[ ]= { 20,120,20,20} ; int n1= 4; int x2[ ]= { 120,220,220,120} ; int y2[ ]= { 120,20,220,120} ; int n2= 4; public void paint(Graphics g) { g.drawPolygon(x1,y1,n1); g.fillPolygon(x2,y2,n2); } }
Output