Question 5

(a) Write an applet that draws four horizontal bars of equal size & of different colors such that they cover up the whole applet area. The applet should operate correctly even if it is resized.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="appletComponentResizedH2E" width=400 height=400>
</applet>
*/
public class appletComponentResizedH2E extends Applet implements ComponentListener
{
int fullAppletHeight = 400, fullAppletWidth = 400;
int oneRectHeight = (fullAppletHeight/4);
public void init()
{
addComponentListener(this);
}
public void componentHidden(ComponentEvent e)
{
repaint();

}
public void componentMoved(ComponentEvent e)
{
repaint();
}
public void componentResized(ComponentEvent e)
{
fullAppletWidth = e.getComponent().getWidth();
fullAppletHeight = e.getComponent().getHeight();
oneRectHeight = (fullAppletHeight/4);
repaint();
}
public void componentShown(ComponentEvent e)
{
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.orange);
g.fillRect(0,0,fullAppletWidth,oneRectHeight);
g.setColor(Color.blue);
g.fillRect(0,oneRectHeight,fullAppletWidth,oneRectHeight);
g.setColor(Color.yellow);
g.fillRect(0,(oneRectHeight*2),fullAppletWidth,oneRectHeight);
g.setColor(Color.black);
g.fillRect(0,(oneRectHeight*3),fullAppletWidth,oneRectHeight);
}
}
(a) Write an applet that tracks the position of the mouse when it is dragged or moved. At the current mouse position, it displays message (x, y) showing current position of the mouse. The message should disappear as soon as the user releases the mouse.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="mouseMoveDraggedH2E" width=300 height=300>
</applet>
*/
public class mouseMoveDraggedH2E extends Applet implements MouseListener, MouseMotionListener {
String msg="";
int mouseX=0,mouseY=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
repaint();
}
public void mouseEntered(MouseEvent me)
{
repaint();
}
public void mouseExited(MouseEvent me)
{
repaint();
}
public void mousePressed(MouseEvent me)
{
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "Help2Engg at ("+mouseX+","+mouseY+")";
repaint();
}
public void mouseMoved(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "Help2Engg at ("+mouseX+","+mouseY+")";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
}
(b) Write a program that counts the no. of words in a text file. The file name is passed as a command line argument. The program should check whether the file exists or not. The words in the file are separated by white space characters.
import java.io.*;
class FileWordCountH2E
{
public static void main(String args[]) throws IOException
{
int ch;
boolean prev=false;
boolean last=false;
int word_count = 0;
int line_count = 0;
FileInputStream h2ein = new FileInputStream(args[0]);
while((ch=h2ein.read())!=-1)
{
last = false;
if(!prev && ch == ' ')
{
++word_count;
last=true;
}
if(ch==' ')
{
prev=true;
last=true;
}
else
prev=false;
if(ch=='\n')
{
++line_count;
last=true;
}
}
word_count += line_count;
if(!last)
++word_count;
System.out.println("No. of Words = "+word_count);
h2ein.close();
}
}

Make Comments..!!


Oops!! No posts from user.

Download Android App