UNIT - 4
GUI Programming using Applet
PROF : SHARVILUCK CHAUDHARY (SSC)
Commonly Used Methods of
Graphics Class
• public abstract void drawString(String str, int x, int y): is used to draw the
specified string.
• public void drawRect(int x, int y, int width, int height): draws a rectangle
with the specified width and height.
• public abstract void fillRect(int x, int y, int width, int height): is used to fill
rectangle with the default color and specified width and height.
• public abstract void drawOval(int x, int y, int width, int height): is used to
draw oval with the specified width and height.
• public abstract void fillOval(int x, int y, int width, int height): is used to fill
oval with the default color and specified width and height.
PROF : SHARVILUCK CHAUDHARY (SSC)
• public abstract void drawLine(int x1, int y1, int x2, int y2): is used to
draw line between the points(x1, y1) and (x2, y2).
• public abstract boolean drawImage(Image img, int x, int y,
ImageObserver observer): is used draw the specified image.
• public abstract void drawArc(int x, int y, int width, int height, int
startAngle, int arcAngle): is used draw a circular or elliptical arc.
• public abstract void fillArc(int x, int y, int width, int height, int
startAngle, int arcAngle): is used to fill a circular or elliptical arc.
• public abstract void setColor(Color c): is used to set the graphics current
color to the specified color.
• public abstract void setFont(Font font): is used to set the graphics
current font to the specified font.
PROF : SHARVILUCK CHAUDHARY (SSC)
Features of Applets Over HTML
• Displaying dynamic web pages of a web
application.
• Playing sound files.
• Displaying documents
• Playing animations
PROF : SHARVILUCK CHAUDHARY (SSC)
Restrictions Imposed on Java
Applets
• An applet cannot load libraries or define native methods.
• An applet cannot ordinarily read or write files on the execution
host.
• An applet cannot read certain system properties.
• An applet cannot make network connections except to the host
that it came from.
• An applet cannot start any program on the host that’s executing it.
PROF : SHARVILUCK CHAUDHARY (SSC)
Displaying Message on Applet
PROF : SHARVILUCK CHAUDHARY (SSC)
Create an HTML File That Includes
The Applet
PROF : SHARVILUCK CHAUDHARY (SSC)
Output
PROF : SHARVILUCK CHAUDHARY (SSC)
Graphics class
Drawing lines
• To perform graphics like shapes, lines, textbox, label,
checkbox, frames, and menu bars in java, the Graphics
class is used.
• The Graphics class defines a number of drawing
functions, Each shape can be drawn edge-only or filled.
• To draw shapes on the screen, we may call one of the
methods available in the Graphics class.
• The most commonly used drawing methods included in
the graphics class
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• void drawLine(int startX, int startY, int endX,
int endY)
PROF : SHARVILUCK CHAUDHARY (SSC)
import java.awt.*;
import java.applet.*;
public class Lines extends Applet
{
public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
g.drawLine(40,25,250,180);
g.drawLine(5,290,80,19);
}
}
PROF : SHARVILUCK CHAUDHARY (SSC)
HTML CODE
• <html>
<head>
<applet code=Lines.class
width=300 Height=250>
</applet>
</head>
</html>
PROF : SHARVILUCK CHAUDHARY (SSC)
Output
PROF : SHARVILUCK CHAUDHARY (SSC)
Rectangle
• The drawRect() and fillRect() methods display an
outlined and filled rectangle, respectively. The upper-
left corner of the rectangle is at(top, left).
• The dimensions of the rectangle are specified by width
and height.
• Use drawRoundRect() or fillRoundRect() to draw a
rounded rectangle.
• A rounded rectangle has rounded corners.
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• void drawRect(int top, int left, int width, int height);
• void fillRect(int top, int left, int width, int height);
• void drawRoundRect(int top,int left,int width, int
height int Xdiameter, int YDiameter);
• void fillRoundRect(int top,int left,int width, int height
int Xdiameter, int YDiameter);
PROF : SHARVILUCK CHAUDHARY (SSC)
import java.awt.*;
import java.applet.*;
public class Rectangle extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10,10,60,50);
g.fillRect(100,100,100,0);
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
}
}
PROF : SHARVILUCK CHAUDHARY (SSC)
HTML CODE
<html>
<head>
<applet code=Rectangle.class
width=300 Height=250>
</applet>
</head>
</html>
PROF : SHARVILUCK CHAUDHARY (SSC)
Output
PROF : SHARVILUCK CHAUDHARY (SSC)
Circles and Ellipses
• The Graphics class does not contain any method for
circles or ellipses.
• To draw an ellipse, use drawOval(). To fill an ellipse, use
fillOval().
• To draw a circle, specify a square as the bounding
rectangle i.e get height = width.
• The ellipse is drawn within a bounding rectangle
whose upper-left corner is specified by (top, left) and
whose width and height are specified by width and
height.
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• void drawOval(int top, int left, int width, int height);
• void fillOval(int top, int left, int width, int height);
PROF : SHARVILUCK CHAUDHARY (SSC)
import java.awt.*;
import java.applet.*;
public class Ellipses extends Applet
{
public void paint(Graphics g)
{
g.drawOval(10,10,60,50);
g.fillOval(100,10,75,50);
g.drawOval(190,10,90,30);
g.fillOval(70,90,140,100);
}
}
PROF : SHARVILUCK CHAUDHARY (SSC)
HTML CODE
• <html>
<head>
<applet code=Ellipses.class
width=300 Height=250>
</applet>
</head>
</html>
PROF : SHARVILUCK CHAUDHARY (SSC)
Outout
PROF : SHARVILUCK CHAUDHARY (SSC)
AWT Componen
• Java MouseListener Interface
➢ The Java MouseListener is notified whenever you
change the state of mouse.
➢ It is notified against MouseEvent. The
MouseListener interface is found in java.awt.event
package.
➢It has five methods.
PROF : SHARVILUCK CHAUDHARY (SSC)
Methods of MouseListener interface
• public abstract void mouseClicked(MouseEvent e);
• public abstract void mouseEntered(MouseEvent e);
• public abstract void mouseExited(MouseEvent e);
• public abstract void mousePressed(MouseEvent e);
• public abstract void mouseReleased(MouseEvent e);
PROF : SHARVILUCK CHAUDHARY (SSC)
Mouse Motion Listener
• The Java MouseMotionListener is notified
whenever you move or drag mouse.
• It is notified against MouseEvent. The
MouseMotionListener interface is found in
java.awt.event package.
• It has two methods.
PROF : SHARVILUCK CHAUDHARY (SSC)
2 methods found in
MouseMotionListener interface
• public abstract void mouseDragged(MouseEvent e);
• public abstract void mouseMoved(MouseEvent e);
PROF : SHARVILUCK CHAUDHARY (SSC)
Key Listener
• The Java KeyListener is notified whenever you
change the state of key.
• It is notified against KeyEvent.
• The KeyListener interface is found in java.awt.event
package, and it has three methods
PROF : SHARVILUCK CHAUDHARY (SSC)
Interface declaration
• public interface KeyListener extends EventListener
PROF : SHARVILUCK CHAUDHARY (SSC)
Java ActionListener Interface
• The Java ActionListener is notified whenever you
click on the button or menu item.
• It is notified against ActionEvent. The ActionListener
interface is found in java.awt.event package.
• It has only one method: actionPerformed()
PROF : SHARVILUCK CHAUDHARY (SSC)
Java ActionListener Interface
• public abstract void actionPerformed(ActionEvent e);
PROF : SHARVILUCK CHAUDHARY (SSC)
Introduction to UI Components
Label
• A label is a user for placing text inside the container.
• A label is used only for inputting text.
• The label does not imply that the text can be
altered or it can be used as a button which can be
further worked upon.
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• Label n=new Label("Name:",Label.CENTER);
PROF : SHARVILUCK CHAUDHARY (SSC)
Introduction To UI Components
Button
• This command generates a button in the User
Interface.
• Clicking on the button would move the command to
another page or another web server which is used
to show several other outputs in the user interface
page.
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• a1=new Button("submit");
• a2=new Button("cancel");
PROF : SHARVILUCK CHAUDHARY (SSC)
Introduction to UI Components
Checkbox
• There can be a certain question and the checkbox is
used to determine the true or false nature of the
question being asked.
• If the checkbox is ticked then it means that the said
question is true which if it is unchecked it means that
the said question is false.
• It is basically a true or false state in Java programming
language.
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• Checkbox checkbox1 = new Checkbox("Hello World");
PROF : SHARVILUCK CHAUDHARY (SSC)
Introduction to UI Components
Text Field
• A text field is used for the editing of a particular line
of text which can be used within the programming
concept.
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• na=new TextField(20);
PROF : SHARVILUCK CHAUDHARY (SSC)
Introduction To UI Components
List
• The list gives a scrolling list of items for the user.
• The scrolling list of items is also being set by the
user.
• The user sets the scrolling list of items such as
Fruits, Vegetables, some questionnaire or other
facts.
PROF : SHARVILUCK CHAUDHARY (SSC)
Syntax
• List l1=new List(4);
• l1.setBounds(100,100, 75,75);
PROF : SHARVILUCK CHAUDHARY (SSC)
PROF : SHARVILUCK CHAUDHARY (SSC)