TUSHAR B KUTE, LECTURER, SANDIP INSTITUTE OF TECHNOLOGY AND RESEARCH CENTRE, NASHIK Graphics Programming in Java http://www.tusharkute.com 1
TUSHAR B KUTE,LECTURER,
SANDIP INSTITUTE OF TECHNOLOGY AND RESEARCH CENTRE, NASHIK
Graphics Programming in Java
http://www.tusharkute.com
1
Applets
Applet is a web based application to support the dynamic web pages on internet…
2
3
Applet vs. Applications
Applets can't read from or write to the reader's file system.
Applets can't communicate with any network server other than the one that had originally stored the applet.
Applets can't run any programs on the reader's system.
Applets can't load programs native to the local platform, including shared libraries such as DLLs (Dynamic Link Library).
All these rules are true for Java applets running Netscape Navigator or Microsoft Internet Explorer.
4
Applet Class
Object
Component
Container
Panel
Applet
JApplet
Window
Frame
JFrame
java.lang
java.awt
java.applet
javax.swing
5
The Applet Skeleton
import java.awt.*; import java.applet.*; public class MyApplet extends Applet { public void init( ) { // initializes applet } public void start( ) { // start or resume execution } public void stop( ) { // suspends execution } public void destroy( ) { // perform shutdown activities } public void paint(Graphics g) { // redisplay contents of window } }
6
Applet Life Cycle
Born
Running Idle
Dead
Begin
Initialization
Display
Stopped
Destroyed
End
start()
paint()
stop()
start()
destroy()
7
Compiling the Applet
/*<applet code="MyApplet" width=200 height=100></applet>*/ javac MyApplet.java MyApplet.class appletviewer MyApplet.java
8
The drawString() method
//void drawString(String msg, int x, int y) /* <applet code = "HelloApplet" width = 200
height = 100> </applet> */ public class HelloApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello",100,50); } }
Program
9
Output window10
Colors
void setBackground(Color newCol)void setForeground(Color newCol)
Example:setBackground(Color.lightGray);setForeground(Color.red);
Color getBackground( )Color getForeground( )
Colors Available in Color classColor.black Color.magentaColor.blue Color.orangeColor.cyan Color.pinkColor.darkGray Color.redColor.gray Color.whiteColor.green Color.yellowColor.lightGray
Program
11
Building Applet to the HTML file
<APPLET[CODEBASE = codebaseURL]CODE = appletFile[ALT = alternateText][NAME = appletInstanceName]WIDTH = pixels HEIGHT = pixels[ALIGN = alignment][VSPACE = pixels] [HSPACE = pixels] >[< PARAM NAME = AttributeName VALUE = AttributeValue>][< PARAM NAME = AttributeName2 VALUE = AttributeValue>]. . .. . .</APPLET>
12
ALIGN tags
ALIGN = TEXTTOPALIGN = TOPALIGN = ABSMIDDLEALIGN = MIDDLEALIGN = BASELINEALIGN = BASELINEALIGN = BOTTOM ALIGN = ABSBOTTOM
13
Building Applet on HTML Page
<HTML><HEAD><TITLE>THIS PAGE HAS AN APPLET ON IT</TITLE></HEAD><BODY><P>MY JAVA APPLET SAYS:<BR><APPLET CODE = "FullApplet" WIDTH = 400
HEIGHT = 100 HSPACE = 20 VSPACE = 20 ALIGN = CENTER></APPLET></BODY></HTML>
14
Example:
The Applet classApplet HTML code
15
Passing parameters to the applet
param taggetParameter() method
String getParameter(parameter name)Example:
The program code
16
Using the Status Window
import java.awt.Graphics; import java.applet.Applet; import java.awt.Color; /* <applet code="StatusWindow" width=300 height=50> </applet> */ public class StatusWindow extends Applet { public void init( ) { setBackground(Color.yellow); } public void paint(Graphics g) { g.drawString("This is applet window.", 10, 20); showStatus("This is status window."); } }
Program
17
Drawing lines
void drawLine(int stx, int sty, int edx, int edy)
Example:
g.drawLine(120, 150, 400, 140); g.drawLine(40, 25, 250, 180); g.drawLine(75, 90, 400, 200);
Program
18
Drawing Rectangles
void drawRect(int top, int left, int width, int height)void fillRect(int top, int left, int width, int height)
Example:
g.drawRect(20,20,160,160);g.fillRect(120,20,60,60);
Program
19
Drawing Rounded Rectangles
void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)
void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam)
Examples:g.drawRoundRect(20,30,160,160,30,30);g.fillRoundRect(40,120,100,60,25,50);
Program
20
Drawing Ellipse and Circle
void drawOval(int top, int left, int width, int height)void fillOval(int top, int left, int width, int height)
Examples:g.drawOval(20, 20, 60, 60);g.fillOval(100, 30, 75, 150);
Program
21
Drawing Arcs
void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
Examples:g.drawArc(20, 20, 60, 60, 90, 180);g.fillArc(120, 20, 60, 60, 90, 180);
Program
22
Drawing Polygons
drawPolygon(x array, y array, coordinates);fillPolygon(x array, y array, coordinates);
Examples:
Program
23
Applet Using Control Loops
/* <applet code = "CtrlLoop" width = 200 height = 200> </applet> */ public class CtrlLoop extends Applet { public void paint(Graphics g) { int i = 70, j = 70; while(i > 10) { g.drawOval(i, i, j, j); i –= 10 ; j += 20; } } }
Program
24
Drawing Line Graphs
/*<applet code = "LineGraph" width = 250 height = 250></applet>*/ public class LineGraph extends java.applet.Applet { public void paint(java.awt.Graphics g) { int i, j; int x[] = {20, 60, 100, 140, 180, 220, 220}; int y[] = {220, 96, 46, 70, 82, 124, 220}; g.setColor(java.awt.Color.blue); g.drawLine(20, 20, 20, 240); //y axis g.drawLine(0, 220, 240, 220); //x axis g.setColor(java.awt.Color.red); g.drawPolygon(x, y, x.length); } } Program
25
Drawing Bar Charts
/* <applet code = "BarChart" width = 350 height = 250> </applet> */ public class BarChart extends java.applet.Applet { String sub[ ] = {"SCI","HIN","MAR","SOC","MAT"}; int marks[ ] = {62, 87, 75, 69, 48}; public void paint(java.awt.Graphics g) { for(int i=0;i<5;i++) { g.setColor(java.awt.Color.red); g.drawString(sub[i], 20, i*50+30); g.setColor(java.awt.Color.blue); g.fillRect(50, i*50, marks[i]*3,40); } } } Program
26
Reference Books
Java: The Complete Reference by Herbert Schildt
Programming in Java by E Balagurusamy
Core Java Programming: A Practical Approach by Tushar B Kute
The Java Handbook by Patrick Naughton
27
Thank You
28