JAVA
Java Applet Basics & Event Handling
Fill in
the blanks
1.A reference to an
applet is embedded in a Web page using a special HTML tag
2.Applet can’t
communicate with a server other than the one that had originally stored the
applet.[T/F]. True.
3.Write a package for
applet import java.awt.*;
4.Write a method to
print string in applet drawString()
5. Applets cannot make
network connection exception to the server host from which it originated.[True/False].
True
6.The paint()
method is called each time your applet’s output must be redrawn.
7.Write a method to
draw a line drawLine()
8.To draw rectangle
what method is used drawRect()
9.Write a method for
polygon drawPolygon()
10. Write a method for
filling oval fillOval()
11. Write a method
for To printing a text in red color setColor(Color.red)
12. ActionEvent
is used for when a button is pressed.
13.KeyEvent is
used to generate when input is received
from the keyboard.
14.MouseEvent is
used to generate when the mouse is dragged ,moved,clicked,pressed or released.
15.Adapter class
is to simplify the creation of event
handler in certain situations.
Answer the Following Questions
(4
marks)
1. Write
a program to print Rectangle using Applet
import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangles"
width=300 height=200>
</applet>
*/
public class Rectangles extends Applet{
public void paint(Graphics g){
g.drawRect(10,10,60,50);
g.fillRect(100,10,60,50);
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
}
}
2. Write
a program to print Polygon using Applet
import java.awt.*;
import java.applet.*;
/*
<applet code="HourGlass"
width=230 height=210>
</applet>
*/
public class HourGlass extends Applet{
public void paint(Graphics g){
int xpoints[]={30,200,30,200,30};
int ypoints[]={30,30,200,200,30};
g.drawPolygon(xpoints,ypoints,5);
}
}
3. Write
a program to print Oval using Applet
File name: Ellipses.java
import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses"
width=300 height=200>
</applet>
*/
public class Ellipses extends Applet{
public void paint(Graphics g){
g.drawOval(10,10,50,50);
g.fillOval(100,10,75,50);
g.drawOval(190,10,90,30);
g.fillOval(70,90,140,100);
}
}
4. Write
a program to print Lamp using Applet
File name: Lamp.java
import java.awt.*;
/*
<applet code="Lamp"
width=300 height=200>
</applet>
*/
public class Lamp extends
java.applet.Applet{
public void paint(Graphics g){
g.fillRect(0,250,290,290);
g.drawLine(125,250,125,160);
g.drawLine(175,250,175,160);
g.drawArc(85,157,130,50,-65,312);
g.drawArc(85,87,130,50,62,58);
g.drawLine(85,177,119,89);
g.drawLine(215,177,181,89);
g.fillArc(78,120,40,40,63,-174);
g.fillOval(120,96,40,40);
g.fillArc(173,100,40,40,110,180);
}
}
5. Write
a program to print text using color
Output :
Hello World
Hello World
Hello World
File name: ColorAppletDemo.java
import java.awt.*;
import java.awt.Font;
import java.applet.*;
/*
<applet code=ColorAppletDemo
width=300 height=300>
</applet>
*/
public class ColorAppletDemo extends
Applet{
public void paint(Graphics g){
Font f=new
Font("TimesRoman",Font.BOLD,20);
Font f1=new
Font("Courier",Font.ITALIC,20);
Font f2=new
Font("Helevitica",Font.PLAIN,20);
g.setColor(Color.red);
g.setFont(f);
g.drawString("Hello World",30,30);
g.setColor(Color.blue);
g.setFont(f1);
g.drawString("Hello World ",30,70);
g.setColor(Color.pink);
g.setFont(f2);
g.drawString("Hello World ",30,110);
}
}
6.Write a program for
KeyEvent Handler
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
/*
<applet
code="SimpleKey" width=300 height=100>
</applet>
*/
public
class SimpleKey extends Applet implements KeyListener{
String
msg="";
int
X=10,Y=20;
public
void init(){
addKeyListener(this);
requestFocus();
}
public
void keyPressed(KeyEvent ke){
showStatus("Key
Down");
}
public
void keyReleased(KeyEvent ke){
showStatus("Key
Up");
}
public
void keyTyped(KeyEvent ke){
msg+=ke.getKeyChar();
repaint();
}
public
void paint(Graphics g){
g.drawString(msg,X,Y);
}
}
Answer the Following
Questions
(10
marks)
1.Write a program for Mouse Event
Handling
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet
code="MouseEvents"width=300 height=100>
</applet>
*/
public class MouseEvents extends
Applet
implements
MouseListener,MouseMotionListener
{
String msg="";
int mouseX=0,mouseY=0;//coordinates
of mouse
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
//Handle mouse clicked.
public void mouseClicked(MouseEvent
me){
//save coordinates
mouseX=0;
mouseY=10;
msg="Mouse clicked.";
repaint();
}
//Handle mouse entered.
public void mouseEntered(MouseEvent
me)
{
//save coordinates
mouseX=0;
mouseY=10;
msg="Mouse entered.";
repaint();
}
//Handle mouse exited.
public void mouseExited(MouseEvent
me)
{
//save coordinates
mouseX=0;
mouseY=10;
msg="Mouse exited.";
repaint();
}
//Handle button pressed.
public void mousePressed(MouseEvent
me)
{
//save coordinates
mouseX=me.getX();
mouseY=me.getY();
msg="Down";
repaint();
}
//Handle button released.
public void mouseReleased(MouseEvent
me)
{
//save coordinates
mouseX=me.getX();
mouseY=me.getY();
msg="Up";
repaint();
}
//Handle mouse dragged.
public void mouseDragged(MouseEvent
me)
{
//save coordinates
mouseX=me.getX();
mouseY=me.getY();
msg="*";
showStatus("Dragging mouse
at"+mouseX+","+mouseY);
repaint();
}
//Handle mouse moved.
public void mouseMoved(MouseEvent me)
{
//show status
showStatus("Moving mouse
at"+me.getX()+","+me.getY());
}
//Display msg in applet window at
current X,Y location.
public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
}
2.Write a
program for color changing event using MouseAdapter class
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet
code="MouseEvents"width=300 height=100>
</applet>
*/
public class MouseEvents extends
Applet
implements
MouseListener,MouseMotionListener
{
String msg="";
int mouseX=0,mouseY=0;//coordinates
of mouse
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
//Handle mouse clicked.
public void mouseClicked(MouseEvent
me){
//save coordinates
mouseX=0;
mouseY=10;
msg="Mouse clicked.";
repaint();
}
//Handle mouse entered.
public void mouseEntered(MouseEvent
me)
{
//save coordinates
mouseX=0;
mouseY=10;
msg="Mouse entered.";
repaint();
}
//Handle mouse exited.
public void mouseExited(MouseEvent
me)
{
//save coordinates
mouseX=0;
mouseY=10;
msg="Mouse exited.";
repaint();
}
//Handle button pressed.
public void mousePressed(MouseEvent
me)
{
//save coordinates
mouseX=me.getX();
mouseY=me.getY();
msg="Down";
repaint();
}
//Handle button released.
public void mouseReleased(MouseEvent
me)
{
//save coordinates
mouseX=me.getX();
mouseY=me.getY();
msg="Up";
repaint();
}
//Handle mouse dragged.
public void mouseDragged(MouseEvent
me)
{
//save coordinates
mouseX=me.getX();
mouseY=me.getY();
msg="*";
showStatus("Dragging mouse
at"+mouseX+","+mouseY);
repaint();
}
//Handle mouse moved.
public void mouseMoved(MouseEvent me)
{
//show status
showStatus("Moving mouse
at"+me.getX()+","+me.getY());
}
//Display msg in applet window at
current X,Y location.
public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
}
3.Write a
program to print Bar graph using Applet
File name: BarChart.java
import java.awt.*;
import java.applet.*;
public class BarChart extends Applet
{
int n=0;
String label[];
int value[];
public void init()
{
try
{
n=Integer.parseInt(getParameter("coloumn"));
label=new String[n];
value=new int[n];
label[0]=getParameter("label1");
label[1]=getParameter("label2");
label[2]=getParameter("label3");
label[3]=getParameter("label4");
value[0]=Integer.parseInt(getParameter("c1"));
value[1]=Integer.parseInt(getParameter("c2"));
value[2]=Integer.parseInt(getParameter("c3"));
value[3]=Integer.parseInt(getParameter("c4"));
}
catch(NumberFormatException e){}
}
public void paint(Graphics g)
{
for(int i=0;i<n;i++)
{
g.setColor(Color.red);
g.drawString(label[i],20,i*50+30);
g.fillRect(50,i*50+10,value[i],40);
}
}
}
/*<applet
code="BarChart.class" width=300 Height=250>
<param name="coloumns"
value="4">
<param name="c1"
value="110">
<param name="c2"
value="150">
<param name="c3"
value="100">
<param name="c4"
value="170">
<param name="label1"
value="91">
<param name="label2"
value="92">
<param name="label3"
value="93">
<param name="label4"
value="94">
</applet>
*/
No comments:
Post a Comment