0% found this document useful (0 votes)
179 views

Java Programming

This document provides information about Java applets and graphics. It discusses what an applet is, how applets are executed, their life cycle and limitations. It also discusses some important applet concepts like applet tags for embedding in HTML, the appletviewer tool, and methods in the Graphics class for drawing arcs and strings in an applet window. The document contains code snippets for a simple "Hello World" applet and describes how to compile and run applets using browsers or the appletviewer tool.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

Java Programming

This document provides information about Java applets and graphics. It discusses what an applet is, how applets are executed, their life cycle and limitations. It also discusses some important applet concepts like applet tags for embedding in HTML, the appletviewer tool, and methods in the Graphics class for drawing arcs and strings in an applet window. The document contains code snippets for a simple "Hello World" applet and describes how to compile and run applets using browsers or the appletviewer tool.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

CS-309 Java Tools

PURSHOTTAM
Lecturer – CSE Department
Govt Polytechnic College, Jaisalmer
Academic Year – 2019-20
E-mail – purush.aries@gmail.com
Java programming - परिचय
• Java is case-sensitive
• import java.lang.* ; // instruction
• Java is object-oriented programming. So we have to develop objects.
• Objects are developed by defining classes.
• Java is platform independent
• There is no concept of pointers in Java.
• Distributed programming, multi-threaded programming
• Internet applications
• Exception Handling
• Database connectivity
• We need an editor to type the program –
vi, emacs, gedit, gvim, notepad, notepad++, word ….
• Save the program to some directory as .java
• javac – java compiler used to compile a java program
e.g. $javac HelloWorld.java
• After compilation is will create a .class file HelloWorld.class
• to run / execute a java program type the following command in
command prompt –
$java HelloWorld
Java program execution
• Interfaces and packages are supported in java
Java Tools
• Java Software Development Kit (Java SDK)
• Tools and library to create, test and document and execute java
programs.
• JDK – Java Development Kit Version 8
• Javac – java compiler
• Java – java interpreter
• Javadoc – generates documentation in HTML
• Appletviewer – java interpreter to execute java Applets
• Jdb – java debugger
• Javap – java disaasembler
• Javah – to create interface between java and C routines
Java Applet
• Applet is a program that is run inside a browser. It is included into
HTML code.
• Appletviewer can be used to run an applet program.
• Applet are useful for creating GUI program with windows.
Resources Online
• https://www.javatpoint.com/java-tutorial
• https://www.w3schools.com/java/
• https://www.tutorialspoint.com/java/
• API – Application Programming Interface
• Library – packages
• Java API documentation
Java API – Application Programming Interface
• Java includes many packages and those packages are called API.
• Package = library
• API documentation
• java.applet – for applet programming
• java.awt – for designing GUI
• java.io – file input/output handling
• java.lang – Object, Threads, Exception, String, System, Math, Float,
Integer etc. This package is always required by any program. It will
automatically included if you do not include.
• There are nine packages in the Java API
• java.net- network programming
• Java.util – miscellaneous classes – Vector, Stack, List, Date, Hash,
Dictionary etc.
• Javax.swing – for designing GUI - Java swing basically another
extension of AWT abstract windowing toolkit for supporting GUI
development.
• Java.sql – for database connectivity (JDBC)
Java IDE (s)
• jEdit
• Eclipse
• Netbeans
• Notepad++
• BlueJ
• Jdeveloper
• IntelliJ
Features of Java
APPLET क्या है ?
• Applet एक विशेष प्रकार का प्रोग्राम है जो की िेब-पेज (HTML)

में अन्तःस्थावपत वकया जा सकता है | इसकी सहायता से dynamic content


(सचल विषय-िस्तु ) उत्पन्न वकया जा सकता है | यह ब्राउज़र के अन्दर ही
वियान्वित होता है तथा क्लाइं ट साइड पर कायय करता है | Applet को वकसी
HTML page में APPLET or OBJECT tag की मदद से embed (अन्तःस्थावपत)
कर web server पर host वकया जा सकता है |
Applet के फायदे (advantages) / कवमयां (drawbacks) -
• यह client side पर कायय करता है अतः कम प्रत्युत्तर समय (response time)
लेता है |

• यह सुरवित (secure) है |

• यह ब्राउज़र के अन्दर सम्पावदत (execute) होता है अतः इसे वकसी भी


प्लेटफामय जैसे – Windows, Linux or MacOS पर रन वकया जा सकता है |

• इसकी एक कमी है की इसे रन करने के वलए ब्राउज़र में Java-plugin की


आिश्यकता पड़ती है |

• यह वकसी website को dynamic बनाने में सहायक है |


Applet के बारे में कुछ महत्वपूर्य तथ्य (facts) -

• सभी applet प्रत्यि या अप्रत्यि रूप से java.applet.Applet class को inherit


करते है | अथाय त िे इस class के sub-class होते है |

• Applet stand alone प्रोग्राम नहीं है अथाय त ये अपने आप चलने में सिम नहीं
होते है इन्हें execute करने के वलए browser सॉफ्टिेर अथिा appletviewer
की आिश्यकता पड़ती है |

• Applet में main() function का प्रयोग नहीं होता है |

• Applet window में आउटपुट प्रदवशयत करने हे तु System.out.println( )


function की जगह AWT function drawString() प्रयुक्त होता है |
Applet Life Cycle

जब एक applet का वियाियन प्रारम्भ होता है तो वनम्न


function वदए गए िम में call होते है –

1. init( )
2. start( )
3. paint( )

जब एक applet का समापन होता है तो वनम्न function वदए


गए िम में call होते है –

1. stop()
2. destroy()
Applet Life Cycle ....
• init( ) – जब भी वकसी applet का वियाियन (execution) प्रारं भ होता है तो
सियप्रथम यह init() function call होता है | इसमें variables का प्ररान्वम्भक मान
वनर्ाय ररत (initialization) वकया जाता है | यह applet के run-time के दौरान
वसफय एक ही बार call होता है |

• start( ) - यह function init() के बाद call होता है | यह वकसी applet को बंद


करने के बाद पुनः start करने पर भी call होता है | यह हर बार call होता है जब
applet के HTML document को स्क्रीन पर display वकया जाता है | यवद कोई
यूजर applet को छोड़कर पुनः इस पर आता है तब भी यह function call होता है
|
Applet Life Cycle ....

• paint() – जब भी AWT आर्ाररत applet के output को पुनः वचवित (draw)


करना होता है तब इस function को call वकया जाता है |

• जब एक applet का वियाियन (execution) प्रारं भ होता है तब भी paint()


function call होता है | अतः जब भी applet को output re-draw करना होता
है तो paint() function call होता है |

• paint() function input में एक Graphics Type का object parameter लेता है


यह Graphics Context (enevironment) को, वजसमें applet run होता है ,
save करता है |
Applet Life Cycle ....

• stop() – जब browser से applet युक्त HTML document हट जाता है और


अन्य page load होता है तो stop() function call होता है | जब stop() call
होता है तो applet run हो रहा होता है इसवलए stop() का उपयोग उन
threads को suspend करने में होता है वजनकी आिश्यकता applet के अदृश्य
(invisible) होने पर नहीं होती है | इन threads को start() function में re-
start वकया जा सकता है जब यूजर applet युक्त पेज पर पुनः लौटता है |
Applet Life Cycle ....

• destroy() – जब applet को memory से पूर्यतया हटाना होता है तब इस


function को call वकया जाता है | इस function में applet द्वारा प्रयु क्त समस्त
resources (संसार्नों) को free (मुक्त) वकया जाता है |

• destroy() function से पूिय सदै ि stop() function को call वकया जाता है |


Program#1 - HelloWorld Applet
Single line comments

import Applet class from applet package and Graphics class


from the awt package.

HelloWorld class declaration as public

This method paint() is defined by AWT and must be


overridden By the applet class

drawString() is member of Graphics class


• Java में वकसी Window में ऊपरी बायााँ (upper-left corner) कोना वनदे शां क
(0,0) को व्यक्त करता है |

• NOTE : Chrome and Firefox no longer supports Netscape Plugin


Application Programming Interface (NPAPI) (technology required for
Java applets).
Applet को run कैसे करें ?
वकसी applet को दो तरीके से run वकया जा सकता है –

1. Applet को Java enabled browser में run करना |

इसके वलए हमें एक HTML file बनानी होगी और उसमें applet को load करने के

वलए नीचे वदए अनुसार <applet> या <object> tag का प्रयोग करना होगा |
Width और height attributre applet window
के पररमाप (dispaly area) को वनर्ाय ररत करता है
|

2. appletviewer tool का प्रयोग करके | यह applet को एक window में run करता


है | यह applet को run करने का एक शीघ्र और आसान तरीका है |
Applet को run कैसे करें ? ......

2(अ) - $ appletviewer HelloWorld.html

2(ब) – इसमें पहले applet प्रोग्राम को javac के


द्वारा compile करते है तथा उसके पश्चात्
appletviewer से उसे run करते है –

$ appletviewer HelloWorld
Applet की सीमाएं (limitations)
• Applet library को लोड नहीं कर सकता और न ही native function को
define कर सकता है |

• Applet फाइल को read या write नहीं कर सकता है |

• Applet कुछ वनवश्चत system properties को read नहीं कर सकता है |

• Applet netwrok कनेक्शन नहीं कर सकता | केिल host जहााँ से यह लोड


हुआ है उससे कनेक्ट हो सकता है |

• यह वकसी प्रोग्राम को स्टाटय नहीं कर सकता है |


Chapter 3 - Graphics
Graphics – Draw an arc
• Supporting methods from java.awt.Graphics class
• void drawArc(int x, int y, int width, int height, int startAngle, int
arcAngle): draws an outline arc.
• void fillArc(int x, int y, int width, int height, int startAngle, int
arcAngle): draws a filled (solid) arc.
• where
• x and y: Indicates the x and y coordinates where the arc is to be
drawn
• width and height: Indicates the width and height of the arc
• startAngle: Dictates the starting angle of the arc
Draw Arc
• g.drawArc(60, 70, 200, 200, 0, 180);
• g.fillArc(300, 70, 200, 200, 0, 270);
Graphics - Draw Lines
• void drawLine(int x1, int y1, int x2, int y2): x1 and y1 are starting
point coordinates and x2 and y2 are ending point coordinates of the
line.
• g.drawLine(60,90, 150, 200);
• g.drawLine(110, 120, 250, 270);
Callback Method
• A callback method is called by the system implicitly; programmer
writes the method but he will not call. It is called automatically
whenever the system requires it; for example, main() method is a call
back method. All the methods of the applet like init(), start() etc. are
callback methods.
Graphics – Draw Rectangle
• 3 types of rectangles Java supports
• Right-angled rectangles
• Round-cornered rectangles
• 3-D Rectangles
• There is not function like drawSquare() ????
• void drawRect(int x, int y, int width, int height): Draws an outline
rectangle with the left-top coordinates of x and y and with
the width and height specified.
• void fillRect(int x, int y, int width, int height): Draws a solid rectangle
with the left-top coordinates of x and y and with
the width and height specified
Draw Rectangles … Round corner
1. drawRoundRect(int x, int y, int width, int height, int xbend, int ybend);
2. fillRoundRect(int x, int y, int width, int height, int xbend, int ybend);
Draw Rectangle – 3D rectangles
• 1. void draw3DRect(int x, int y, int width, int height, boolean
raised): This method draws an outline 3D rectangle with the above
specified parameters. The last boolean parameter true indicates
elevation above the drawing surface and false indicates etching
(lowering) into the surface.
• 2. void fill3DRect(int x, int y, int width, int height, boolean
raised): This method draws a solid 3D rectangle with the above
specified parameters. The last boolean parameter trueindicates
elevation above the drawing surface and false indicates etching into
the surface.
3D – rectangles
Graphics – Draw Oval/Ellipse
• void drawOval(int x, int y, int width, int height);
• void fillOval(int x, int y, int width, int height);
• g.drawOval(50, 80, 150, 100);
• g.fillOval(230, 80, 150, 100);
Graphics – draw polygon
• drawPolygon(int[] x, int[] y, int numberofpoints) : draws a polygon
with the given set of x and y points.
• fillPolygon(int[] x, int[] y, int numberofpoints) : draws a filled
polygon with the given set of x and y points.
• drawPolygon( Polygon p ) - Draws the specified polygon.
Example – polygon
• int xCoords[] = { 50, 200, 300, 150, 50, 50 };
• int yCoords[] = { 100, 0, 50, 300, 200, 100 };
• int xFillCoords[] = { 450, 600, 700, 550, 450, 450 };
• g.drawPolygon(xCoords, yCoords, 6);
• g.fillPolygon(xFillCoords, yCoords, 6);
Draw Polylines -
• drawPolyline( int xPoints[], int yPoints[], int points )
- draws a sequence of connected lines.
- last point is different from the first point then the polyline is not
closed.
Clipping
• https://www.javatpoint.com/computer-graphics-clipping
Clipping -

• वकसी image या window के visible (दृश्य) और invisible (अदृश्य) भाग को


पृथक करने की वजस प्रविया का उपयोग वकया जाता है उसे clipping कहते है
| clipping के द्वारा visible तथा invisible भाग में प्रत्यके अियि को वचन्वन्हत
वकया जाता है | visible portion को रखा जाता है तथा invisible portion को
नजरं दाज वकया जाता है |
Types of Lines -
• Visible - A line or lines entirely inside the window is considered visible
• Invisible - A line entirely outside the window is considered invisible
• Clipped - A line partially inside the window and partially outside is clipped. For
clipping point of intersection of a line with the window is determined.
Clipping -
Clipping
void clipRect(int x, int y, int width,
int height)Intersects the current clip with the
specified rectangle.
Shape getClip()Gets the current clipping area.

Rectangle getClipBounds()Returns the bounding rectangle of the


current clipping area.
Rectangle getClipBounds(Rectangle r)Returns the bounding
rectangle of the current clipping area.

boolean hitClip(int x, int y, int width, int height)Returns true if the


specified rectangular area might intersect the current clipping
area.
abstract void setClip(int x, int y, int width, int height)Sets the
current clip to the rectangle specified by the given
coordinates.
abstract void setClip(Shape clip)Sets the current clipping area to an
arbitrary clip shape.

You might also like