Assignment No 03
AIM:
A) Design an applet that displays the string “Hello AIT” moving from left to right. When it reaches to right, it scrolls back to left.
B) Design an applet that displays circle which grows outwards (up to certain value) and finally it reduces to 0.
THEORY:
The abstract Windowing toolkit(AWT) is an API that is responsible for building the GUI. It is a part of JFC. Java programs are classified into two groups namely applications and applets. An applet is a dynamic and interactive program that can run inside a web page displayed by Java capable browser. This can be also executed using applet viewer application.
Some of the important tags used inside an applet .html file are
Major Applet activities are :
init() method gets called as soon as applet is started. Initialization of all variables, creation of objects, setting of parameters can be done in this method. start() method is executed after the init() method where the applet is started. stop() method is used to halt the running of an applet. destroy() method is used to free the memory occupied by the variables and objects initialized in the applet. Any cleaning up activity that needs to be performed can be done in this method. paint() method helps in drawing, writing and creating a colored background or an image onto the applet. It takes an argument, which is an instance of Graphics class. repaint() method is used in case an applet is to be repainted. The repaint() calls the update() method to clear the screen of any existing contents. The update() method in turn calls paint() method that then draws the contents of the current frame.
Methods in Graphics class are
g.drawBytes(byte[ ] data, int offset, int length, int x, int y)
|
g.drawChars(char[ ] data, int offset, int length, int x, int y)
|
g.drawString(String str, int x, int y)
|
g.drawLine( int x1, int y1, int x2, int y2)
|
g.drawRect( int x1, int y2, int width, int height)
g.fillRect( int x1, int y2, int width, int height)
|
g.drawRoundRect( int x1, int y2, int width, int height, int width1, int height1)
g.fillRoundRect( int x1, int y2, int width, int height, int width1, int height1)
|
g.drawOval( int x1, int y2, int width, int height)
g.fillOval( int x1, int y2, int width, int height)
|
g.draw3DRect( int x1, int y2, int width, int height, true/false)
|
g.drawPolygon( int xs[ ], int ys [ ] )
g.fillPolygon( int xs[ ], int ys [ ] )
|
g.drawArc(int x1, int y1, int width, int height, angle1, angle2)
g.fillArc(int x1, int y1, int width, int height, angle1, angle2)
|
g.drawImage(image img, int x, int y, int width, int height, imageObserver)
|
Font class represents Fonts. Text can be written inside an applet using different fonts.
Constructor:
Font f = new Font (string name, int style, int size);
Name: The logical name of the font.
Style: The style of the font as Font.BOLD, Font.ITALIC, Font.PLAIN
Size: The size of the font
For setting the font setFont(f) method present in a graphics class can be used.
Color class represents color. Color is represented as a combination of red, blue and green. Each component can have a number between 0 and 255. 0,0,0 is black and 255,255,255 is white.
Constructor:
Color c = new Color(0,0,0);
Method:
g.setColor( c) //can be used present in Graphics class.
Animation is the technique by which an object is moved on the screen. The object to be moved can be simple drawing or a complex image loaded from an image file. This object, in animation region is called a frame of animation.
CONCLUSION
Both applets are successfully implemented.
PROGRAM CODE
import java.awt.*;
import java.applet.Applet;
public classmovingBall extends Applet implements Runnable {
Thread mythread = null;
int position =0;
public void start( )
{
mythread =new mythread (this); // this refers to current object
mythread.start( );
}
public void run( )
{
while(true)
{
for(position=o; position {
repaint();
try{ mythread.sleep(100); }
catch(InterruptedException e) { };
}
}
}
public void stop( )
{
mythread.stop( );
mythread=null;
}
public void paint(graphics g)
{
g.setColor(Color.gray);
g.fillOval(position,50,30,30);
g.setColor(Color.black);
g.fillOval(position+6,58,5,5);
g.drawLine(position,58,10,12);
}
}
Leave a Reply