Ready to Develop?

northcarolinawryΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

115 εμφανίσεις

Why Java?

Ready to Develop?

Java technology is an object
-
oriented, platform
-
independent, multithreaded
programming environment.
Whether developing
applications for consumers or
mission
-
critical deployments,
you can depend on a
consistent programming
envir
onment.


Enables you to
efficiently deploy exciting new
applications and services


Enables applications to
securely access key system
resources while protecting
your data


Allows you to do more
with your applications, from
real
-
time capabilities to high
-
per
formance multitasking
virtual machines to broad
access to system capabilities


Runs across more than
seven billion devices including
mobile, desktop, television,
and countless embedded
devices


Java is one of the most
popular application platforms
on the
planet. Which version
of Java is right for you?


Java Platform,
Standard Edition

(
Java SE
)
is the best platform for
development of desktop,
server and high
-
end
em
bedded devices, and it is
the most versatile runtime for
Java as well as many other
languages.


Java Platform, Micro
Edition

(
Java ME
) is the best
software for r
esource
-
constrained embedded
devices and has seen
widespread deployment
across mobile handsets and
digital television devices.


Java Platform,
Enterprise Edition

(
Java EE
)
is the standard for delivering
secure, robust, scalable,
multi
-
platform applications
and services across the
enterprise, from server and
desktop applications to
wireless and wireline
applications.

Learn About Java Technology

To date, the Java p
latform has attracted more than 6.5 million software developers.
It's used in every major industry segment and has a presence in a wide range of
devices, computers, and networks.


Java technology's versatility, efficiency, platform portability, and securit
y make it the
ideal technology for network computing. From laptops to datacenters, game consoles to
scientific supercomputers, cell phones to the Internet, Java is everywhere!



1.1 billion desktops run Java



930 million Java Runtime Environment downloads e
ach year



3 billion mobile phones run Java



31 times more Java phones ship every year than Apple and Android combined



100% of all Blu
-
ray players run Java



1.4 billion Java Cards are manufactured each year



Java powers set
-
top boxes, printers, Web cams, g
ames, car navigation systems,
lottery terminals, medical devices, parking payment stations, and more.

Why Software Developers Choose Java

Java has been tested, refined, extended, and proven by a dedicated community. And
numbering more than 6.5 million dev
elopers, it's the largest and most active on the
planet. With its versatilty, efficiency, and portability, Java has become invaluable to
developers by enabling them to:



Write software on one platform and run it on virtually any other platform



Create prog
rams to run within a Web browser and Web services



Develop server
-
side applications for online forums, stores, polls, HTML forms
processing, and more



Combine applications or services using the Java language to create highly
customized applications or serv
ices



Write powerful and efficient applications for mobile phones, remote processors,
low
-
cost consumer products, and practically any other device with a digital
heartbeat

Some Ways Software Developers Learn Java

Many colleges and universities offer cours
es in programming for the Java platform. In
addition, developers can also enhance their Java programming skills by reading Oracle's
Java developer web site
, subscribing to
Java technology
-
focused newsletters
, using the
Java Tutorial

and the
New to Java Programming Center
,
and signing up for Web, virtual,
or instructor
-
led
courses
.

The
Oracle Technology Network

is the world's largest community of application developers, database admins, system
admins/developers, and architects using industry
-
standard technologies in combination with Oracle products. It is
also the home of
oracle.com/technetwork/java
, the ultimate,
complete, and authoritative source of technical information about Java. Membership is free,
join today!

(In your Profile, check the Oracle Technology Network box under My
Community Memberships.)

What Is JavaFX

JavaFX exten
ds your web experience by delivering rich media and content across all the screens of your life. As users, you will be able t
o run
JavaFX applications in a browser or drag and drop them onto the desktop. It's a seamless interface!

JavaFX is powered by Jav
a:

JavaFX extends the power of Java by allowing developers to use any Java library within JavaFX applications. This way develope
rs can expand their
capabilities in Java and make use of the revolutionary presentation technology that JavaFX provides to build

engaging visual experiences.

Highlights of JavaFX:



Allows users to view JavaFX applications in a browser or break free of the
browser by dragging and dropping the same application onto the desktop



Enables an efficient designer
-
to
-
developer workflow with

Project Nile: designers
can work in their tools of choice while collaborating with Web scripters who use
the NetBeans IDE with JavaFX



Extends Java technology by enabling use of any Java library within a JavaFX
application



Allows developers to integrate
vector graphics, animation, audio, and video Web
assets into a rich, interactive, immersive application

Java

is a
programming language

originally developed by
James Gosling

at
Sun
Microsystems

(which is now a subsidiary of
Oracle Corporation
) and released in 1
995 as
a core component of Sun Microsystems'
Java platform
. The language derives much of its
syntax

from
C

and
C++

but has a simpler
object model

and fewer
low
-
level

facilities.
Java applications are typically
compiled

to
bytecode

(
class file
) that can
run on any
Java
Virtual Machine

(JVM) regardless of
computer architecture
. Java is a general
-
purpose,
concurrent, class
-
bas
ed, object
-
oriented language that is specifically designed to have as
few implementation dependencies as possible. It is intended to let application developers
"write once, run anywhere". Java is currently one of the most popular programming
languages in u
se, and is widely used from application software to web applications.
[9]
[10]

The original and
reference implementation

Java
compilers
, virtual machines, and
class
libraries

were developed by Sun from 1995. As of May 2007, in compliance with the
specifications of the
Java Community Process
, Sun relicensed most of its Java
technologies under the
GNU General Public License
. Others have also develop
ed
alternative implementations of these Sun technologies, such as the
GNU Compiler for
Java
,
GNU Classpath
, and
Dalvik
.

History

Java (software platform)#History

and
Java version history



Duke, the Java mascot

James Gosling
, Mike Sheridan, and
Patrick Naughton

initiated the Java language project
in June 1991.
[11]

Java was originally designed for interactive television, but it was too
advanced for the digital cable television industry at the time.
[12]

The language was
initially called
Oak

after an
oak tree

that stood o
utside Gosling's office; it went by the
name
Green

later, and was later renamed
Java
, from a list of random words.
[13]

Gosling
aimed to implement a
virtual machine

and a language that had a familiar
C
/
C++

style of
notation.
[14]

Sun Microsystems released the first public implementation as Java 1.0 in 1995. It
promised "
Write Once, Run Anywhere
" (WORA), providing no
-
cost run
-
times on
popular
platforms
. Fairly secure and featuring configurable

security, it allowed network
-

and file
-
access restrictions. Major web browsers soon incorporated the ability to run Java
applets

within web pages, and Java quickly became popular. With the ad
vent of
Java 2

(released initially as J2SE 1.2 in December 1998

1999), new versions had multiple
configurations built for different types of platforms. For example,
J2EE

targeted
enterprise applications and the greatly stripped
-
down version
J2ME

for mobile

applications (Mobile Java).
J2SE

designated the Standard Edition. In 2006, for marketing
purposes, Sun renamed new
J2

versions as
Java EE
,
Java ME
, and
Java SE
, respective
ly.

In 1997, Sun Microsystems approached the
ISO/IEC JTC1

standards body and later the
Ecma International

to formalize Java, but it so
on withdrew from the process.
[15]

Java
remains a
de facto

standard, controlled through the
Java Community Process
.
[16]

At one
time, Sun made most of its Java implementations availabl
e without charge, despite their
proprietary software

status. Sun generated revenue from Java through the selling of
licenses for specialized products such as the Java Enterprise System. Sun
distinguishes
between its
Software Development Kit (SDK)

and
Runtime Environment (JRE)

(a
subset
of the SDK); the primary distinction involves the JRE's lack of the compiler, utility
programs, and header files.

On November 13, 2006, Sun released much of Java as
open source software

under the
terms of the
GNU General Public License

(GPL). On May 8, 2007, Sun finished the
process, making all of Java's core code available under
free software
/open
-
source
distribution terms, aside from a small portion of code to which Sun did not hold the
copyright.
[17]

Sun's vice
-
president Rich Green has said that Sun's ideal role with regards to Java is as an
"evangelist."
[18]

Following
Oracle Corporation
's acquisition of Sun Microsystems in 2009

2010, Oracle
has described itself as the "steward of Java technology with a relentless commitment to
fostering a community of participatio
n and transparency".
[19]

Principles

There were five primary goals in the creation of the Java language:
[20]

1.

It should be "simple, object oriented, and familiar".

2.

It should be "robust and secure".

3.

It should have "an architecture
-
neutral and portable environment".

4.

It should execute with "high performan
ce".

5.

It should be "interpreted, threaded, and dynamic".

Platform

An edition of the
Java platform

is the name for a bundle of related programs from Sun
which allow for developing and running programs written in the
Java programming
language
. The platform is not specific to any one processor or operating system, but
rather an execution engine (called a
virtual machine
) and a compiler with a set of libraries
that are implemented for various hardware and operating systems so that Java programs
can run identically on all of them.



Java Card
: A
technology that allows small Java
-
based applications (
applets
) to be
run securely on
smart cards

and similar small
-
memor
y
-
footprint devices.



Java ME

(Micro Edition): Specifies several different sets of libraries (known as
profiles) for devices which are sufficiently limited that supplying the full set of
Jav
a libraries would take up unacceptably large amounts of storage.



Java SE

(Standard Edition): For general
-
purpose use on desktop PCs, servers and
similar devices.



Java EE

(Enterprise Edition): Java SE plus various APIs useful for
multi
-
tier

client

server

enterprise applications
.

As of September 2009
, the current version of the Java Platform is specified as either 1.6.0
or 6 (both refer to the same version). Version 6 is the product version, while 1.6.0 is the
developer version.

The Java Platform consists of several programs, each of wh
ich provides a distinct portion
of its overall capabilities. For example, the Java compiler, which converts Java source
code into Java bytecode (an intermediate language for the
Java Virtual
Machine

(JVM)),
is provided as part of the
Java Development Kit

(JDK). The
Java Runtime En
vironment

(JRE), complementing the JVM with a
just
-
in
-
time (JIT) compiler
, converts intermediate
bytecode into native machine code on the fly. Also supplie
d are extensive libraries, pre
-
compiled in which are several other components, some available only in certain editions.

The essential components in the platform are the Java language compiler, the libraries,
and the runtime environment in which Java interm
ediate bytecode "executes" according
to the rules laid out in the virtual machine specification.

Java Platform diagram from Sun

Java Virtual Machine

Main article:
Java Virtual Machine

The he
art of the Java Platform is the concept of a "virtual machine" that executes
Java
bytecode

programs. This bytecode is the same no matter what hardware or operating
system the program is running und
er. There is a JIT compiler within the
Java Virtual
Machine
, or JVM. The JIT compiler translates the Java bytecode into native processor
instructions at run
-
time and caches the native code in memory during execution.

The use of bytecode as an intermediate
language permits Java programs to run on any
platform that has a virtual machine available. The use of a JIT compiler means that Java
applications, after a short delay during loading and once they have "warmed up" by being
all or mostly JIT
-
compiled, tend
to run about as fast as native programs. Since JRE
version 1.2, Sun's JVM implementation has included a
just
-
in
-
time compiler

instead of an
interpreter.

Alt
hough Java programs are
platform independent
, the code of the Java Virtual Machine
(JVM) that execute these programs is not; every supported operating platform has its own
JVM.

Class libraries

Main article:
Java Class Library

In most modern operating systems, a large body of reusable code is provided to simplify
the programmer's job. This code is typically provided
as a set of
dynamically loadable
libraries

that applications can call at runtime. Because the Java Platform is not dependent
on any

specific operating system, applications cannot rely on any of the pre
-
existing OS
libraries. Instead, the Java Platform provides a comprehensive set of its own standard
class libraries containing much of the same reusable functions commonly found in
moder
n operating systems. The majority of the system library is also written in Java. For
instance,
Swing

library paints the user interface and handles the events itself, eliminating
ma
ny subtle differences between how different platforms handle even similar
components.

The Java class libraries serve three purposes within the Java Platform. First, like other
standard code libraries, the Java libraries provide the programmer a well
-
known
set of
functions to perform common tasks, such as maintaining lists of items or performing
complex string parsing. Second, the class libraries provide an abstract interface to tasks
that would normally depend heavily on the hardware and operating system. T
asks such as
network access and file access are often heavily intertwined with the distinctive
implementations of each platform. The
java.net

and
java.io

libraries implement
an abstraction layer in native OS code, then provide a standard interface for the
Java
applications to perform those tasks. Finally, when some underlying platform does not
support all of the features a Java application expects, the class libraries work to gracefully
handle the absent components, either by emulation to provide a substitu
te, or at least by
providing a consistent way to check for the presence of a specific feature.

Languages

See also:
JVM programming langua
ges
,
JVM Languages

The word Java, by itself, usually refers to the Java programming language which was
designed for use with the Java Platform. Programming languages are typical
ly outside of
the scope of the phrase "platform", although the Java programming language is listed as a
core part of the Java platform. The language and runtime are therefore commonly
considered a single unit.

Nevertheless, third parties have produced a nu
mber of
compilers

or
interpreters

which
target the JVM. Some of these are for existing lan
guages, while others are for extensions
to the Java language itself. These include:



BeanShell

a
lightweight scripting

for Java



Clojure




Groovy




JRuby
, a
Ruby

interpreter



Jython
, a
Python

inte
rpreter that includes jythonc, a Python
-
to
-
Java bytecode
compiler



Rhino
, a
JavaScript

interpreter




Scala


JavaPlatform



Java Example Categories And Packages

Java Language Fundamentals

Basic Java Examples

(14)

Break Statement

(2)

Continue Statement
(2)

Data types

(8)

Do While Loop

(1)

Final

(1)

For Loop

(17)

If Els
e Statement

(5)

Operators

(4)

Sorting Algorithms

(2)

Static

(2)

Switch Statement

(3)

Threads

(4)

While Loop

(1)



Commonly Used Java Classes

Calendar

(16)

Java Date Class

(6)

Java Date Formatting

(14)

Java String Examples

(16)

Java StringBuffer Examples

(8)

StringTokenizer

(7)

Wrapper Classes And Other Classes Of java.lang Package

BigInteger

(1)

Boolean Wrapper Class

(5)

Byte Wrapper Class

(6)

Double Wrapper Class

(8)

Float Wrapper Class

(8)

Integer Wrapper Class

(12)

Long Wrapper Class

(6)

Math Class

(12)

Short Wrapper Class

(6)

File And IO

BufferedInputStream

(2)

BufferedOutputStream

(2)

ByteArrayInputStream

(2)

DataInputStream

(10)

DataOutputStream

(13)

File And Directory

(25)

FileInputStream

(3)

FileOutputStream

(6)

InputStream

(2)

PushbackInputStream

(1)





Java Collections And Data Structure
s ( java.util package )

ArrayList

(14)

Arrays

(9)

Collections

(33)

Comparator

(2)

Enumeration

(1)

Hashmap

(8)

HashSet

(7)

Hashtable

(11)

Iterator

(2)

LinkedHashMap

(8)

LinkedHashSet

(7)

LinkedList

(16)

ListIterator

(6)

Searching

(7)

Sorting

(7)

TreeMap

(12)

TreeSet

(11)

Vector

(17)

Swing

JLabel

(22)

JTextField

(13)



Applets And AWT

Applet

(28)

AWT Button

(11)

AWT Checkbox

(12)

AWT CheckboxGroup

(3)

AWT Choice

(9)

AWT Color

(13)

AW
T Font

(7)

AWT Frame

(3)

AWT Label

(10)

AWT List

(21)





Miscellaneous Classes

Runtime

(8)

System

(9)



Java Zip

Adler32

(1)

CheckedInputStream

(2)

CheckedOutputStream

(2)

CRC32

(1)

Deflater

(1)

ZipEntry

(8)

ZipFile

(6)

ZipInputStream

(3)

ZipOutputStream

(4)

Calculate Circle Area using Java Example

1.

/*

2.


Calculate Circle Area using Java Example

3.


This Calculate Circle

Area using Java Example shows how to
calculate

4.


area of circle using it's radius.

5.

*/

6.



7.

import

java.io.BufferedReader
;

8.

import

java.io.IOException
;

9.

import

java.io.InputStreamReader
;

10.



11.

public

class

CalculateCircleAreaExample
{

12.



13.


public

static

void

main
(
Str
ing
[]

args
)

{

14.



15.



int

radius
=

0
;

16.



System
.
out
.
println
(
"Please enter radius of a circle"
)
;

17.



18.



try

19.



{

20.




//get the radius from console

21.




BufferedReader

br
=

new

BufferedReader
(
new

InputStreamReader
(
System
.
in
))
;

22.




radius
=

Integer
.
parseInt
(
br.
readLine
())
;

23.



}

24.



//if invalid value was entered

25.



catch
(
NumberFormatException

ne
)

26.



{

27.




System
.
out
.
println
(
"Invalid radius value"

+

ne
)
;

28.




System
.
exit
(
0
)
;

29.



}

30.



catch
(
IOException

ioe
)

31.



{

32.




System
.
out
.
println
(
"IO Error :"

+

ioe
)
;

33.




System
.
exit
(
0
)
;

34.



}

35.



36.



/*

37.




* Area of a circle is

38.




* pi * r * r

39.




* where r is a radius of a circle.

40.




*/

41.



42.



//NOTE : use Math.PI constant to get value of pi

43.



double

area
=

Math
.
PI

*

radius
*

radius
;

44.



45.



System
.
out
.
println
(
"Area of a circle is "

+

area
)
;

46.


}

47.

}

48.



49.

/*

50.

Output of Ca
lculate Circle Area using Java Example would be

51.

Please enter radius of a circle

52.

19

53.

Area of a circle is 1134.1149479459152

54.

*/

55.

*

56.



Even Odd Number Example

57.



This Java Even Odd Number Example shows how to check if the given

58.



number is even or odd.

59.

*/

60.



61.

publi
c

class

FindEvenOrOddNumber
{

62.



63.


public

static

void

main
(
String
[]

args
)

{

64.



65.



//create an array of 10 numbers

66.



int
[]

numbers
=

new

int
[]{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
}
;

67.



68.



for
(
int

i
=
0
;

i
<

numbers.
length
;

i
++
){

69.



70.




/*

71.





* use modulus operator to check if the n
umber
is even or odd.


72.





* If we divide any number by 2 and reminder
is 0 then the number is

73.





* even, otherwise it is odd.

74.





*/

75.



76.





if
(
numbers
[
i
]
%
2

==

0
)

77.






System
.
out
.
println
(
numbers
[
i
]

+

" is
even number."
)
;

78.





else

79.


System
.
out
.
println
(
numbe
rs
[
i
]

+

" is odd number."
)
;

80.




}

81.



82.


}

83.

}

84.



85.

/*

86.

Output of the program would be

87.

1 is odd number.

88.

2 is even number.

89.

3 is odd number.

90.

4 is even number.

91.

5 is odd number.

92.

6 is even number.

93.

7 is odd number.

94.

8 is even number.

95.

9 is odd number.

96.

10 is even number.

97.

*/

98.

/
*

99.



Java Factorial Example

100.



This Java Factorial Example shows how to calculate factorial of

101.



a given number using Java.

102.

*/

103.



104.

public

class

NumberFactorial
{

105.



106.


public

static

void

main
(
String
[]

args
)

{

107.



108.



int

number
=

5
;

109.



110.



/*

111.




* Factorial of any numb
er is !n.

112.




* For example, factorial of 4 is 4*3*2*1.

113.



*/

114.



115.



int

factorial
=

number
;

116.



117.



for
(
int

i
=
(
number
-

1
)
;

i
>

1
;

i
--
)

118.



{

119.




factorial
=

factorial
*

i
;

120.



}

121.



122.


System
.
out
.
println
(
"Factorial of a number is "

+

factorial
)
;

123.


}

124.

}

125.



126.

/*

127.

Output of the
Factorial program would be

128.

Factorial of a number is 120

129.

*/

130.

/*

131.


Java Factorial Using Recursion Example

132.


This Java example shows how to generate factorial of a given
number

133.


using recursive function.

134.

*/

135.



136.

import

java.io.BufferedReader
;

137.

import

java.io.IOExcep
tion
;

138.

import

java.io.InputStreamReader
;

139.



140.

public

class

JavaFactorialUsingRecursion
{

141.



142.


public

static

void

main
(
String

args
[])

throws

NumberFormatException
,
IOException
{

143.



144.



System
.
out
.
println
(
"Enter the number: "
)
;

145.



146.



//get input from the user

147.



Buffered
Reader

br
=
new

BufferedReader
(
new

InputStreamReader
(
System
.
in
))
;

148.



int

a
=

Integer
.
parseInt
(
br.
readLine
())
;

149.



150.



//call the recursive function to generate factorial

151.



int

result
=

fact
(
a
)
;

152.



153.



154.



System
.
out
.
println
(
"Factorial of the number is: "

+

result
)
;

155.


}

156.



157.


static

int

fact
(
int

b
)

158.


{

159.



if
(
b
<=

1
)

160.




//if the number is 1 then return 1

161.




return

1
;

162.



else

163.




//else call the same function with the value
-

1

164.




return

b
*

fact
(
b
-
1
)
;

165.


}

166.

}

167.



168.

/*

169.

Output of this Java example would be

170.



171.

Enter the number:

172.

5

173.

Factorial

of the number is: 120

174.

*/

Java Interface example

1.

/*

2.

Java Interface example.

3.

This Java Interface example describes how interface is defined and

4.

being used in Java language.

5.



6.

Syntax of defining java interface is,

7.

<modifier> interface <interface
-
name>{

8.



//
members and methods()

9.

}

10.

*/

11.



12.

//declare an interface

13.

interface

IntExample
{

14.



15.


/*

16.



Syntax to declare method in java interface is,

17.



<modifier> <return
-
type> methodName(<optional
-
parameters>);

18.



IMPORTANT : Methods declared in the interface are implicitly p
ublic
and abstract.

19.



*/

20.



21.


public

void

sayHello
()
;

22.


}

23.

}

24.

/*

25.

Classes are extended while interfaces are implemented.

26.

To implement an interface use implements keyword.

27.

IMPORTANT : A class can extend only one other class, while it

28.

can implement n number of
interfaces.

29.

*/

30.



31.

public

class

JavaInterfaceExample
implements

IntExample
{

32.


/*

33.



We have to define the method declared in implemented interface,

34.



or else we have to declare the implementing class as abstract class.

35.



*/

36.



37.


public

void

sayHello
(){

38.


Sys
tem
.
out
.
println
(
"Hello Visitor !"
)
;

39.


}

40.



41.


public

static

void

main
(
String

args
[]){

42.


//create object of the class

43.


JavaInterfaceExample javaInterfaceExample
=

new

JavaInterfaceExample
()
;

44.


//invoke sayHello(), declared in IntExample interface.

45.


javaInterfaceExample.
sayHello
()
;

46.


}

47.

}

48.



49.

/*

50.

OUTPUT of the above given Java Interface example would be :

51.

Hello Visitor !

52.

*/

Java Class example

1.

/*

2.

Java Class example.

3.

This Java class example describes how class is defined and being used

4.

in Java language.

5.



6.

S
yntax of defining java class is,

7.

<modifier> class <class
-
name>{

8.



// members and methods

9.

}

10.

*/

11.



12.

public

class

JavaClassExample
{

13.


/*

14.



Syntax of defining memebers of the java class is,

15.



<modifier> type <name>;

16.



*/

17.


private

String

name
;

18.


/*

19.



Syntax of

defining methods of the java class is,

20.



<modifier> <return
-
type> methodName(<optional
-
parameter
-
list>)
<exception
-
list>{

21.



...

22.



}

23.



*/

24.


public

void

setName
(
String

n
){

25.


//set passed parameter as name

26.


name
=

n
;

27.


}

28.


public

Strin
g

getName
(){

29.


//return the set name

30.


return

name
;

31.


}

32.


//main method will be called first when program is executed

33.


public

static

void

main
(
String

args
[]){

34.


/*

35.



Syntax of java object creation is,

36.



<class
-
name> object
-
name = new <class
-
cons
tructor>;

37.



*/

38.


JavaClassExample javaClassExample
=

new

JavaClassExample
()
;

39.


//set name member of this object

40.


javaClassExample.
setName
(
"Visitor"
)
;

41.


// print the name

42.


System
.
out
.
println
(
"Hello "

+

javaClassExample.
getName
())
;


43.


}

44.

}

45.



46.

/*

47.

OUTPUT of the above given Java Class Example would be :

48.

Hello Visitor

49.

*/

Reverse Number using Java

1.

/*

2.



Reverse Number using Java

3.



This Java Reverse Number Example shows how to reverse a given number.

4.

*/

5.



6.

public

class

ReverseNumber
{

7.



8.


public

stat
ic

void

main
(
String
[]

args
)

{

9.



10.



//original number

11.



int

number
=

1234
;

12.



int

reversedNumber
=

0
;

13.



int

temp
=

0
;

14.



15.



while
(
number
>

0
){

16.



17.




//use modulus operator to strip off the last
digit

18.




temp
=

number
%
10
;

19.



20.




//create the reversed number

21.




reve
rsedNumber
=

reversedNumber
*

10

+

temp
;

22.




number
=

number
/
10
;

23.



24.



}

25.



26.



//output the reversed number

27.



System
.
out
.
println
(
"Reversed Number is: "

+

reversedNumber
)
;

28.


}

29.

}

30.



31.

/*

32.

Output of this Number Reverse program would be

33.

Reversed Number is: 4321

34.

*/

Swap
Numbers Java Example

1.

/*

2.


Swap Numbers Java Example

3.


This Swap Numbers Java Example shows how to

4.


swap value of two numbers using java.

5.

*/

6.



7.

public

class

SwapElementsExample
{

8.



9.


public

static

void

main
(
String
[]

args
)

{

10.



11.



int

num1
=

10
;

12.



int

num2
=

20
;

13.



14.



System
.
out
.
println
(
"Before Swapping"
)
;

15.



System
.
out
.
println
(
"Value of num1 is :"

+

num1
)
;

16.



System
.
out
.
println
(
"Value of num2 is :"

+
num2
)
;

17.



18.



//swap the value

19.



swap
(
num1, num2
)
;

20.


}

21.



22.


private

static

void

swap
(
int

num1,
int

num2
)

{

23.




int

temp
=

num1
;

24.



num1
=

num2
;

25.



num2
=

temp
;

26.



27.



System
.
out
.
println
(
"After Swapping"
)
;

28.



System
.
out
.
println
(
"Value of num1 is :"

+

num1
)
;

29.



System
.
out
.
println
(
"Value of num2 is :"

+
num2
)
;

30.



31.


}

32.

}

33.


/*

34.

Output of Swap Numbers example would be

35.

Before Swapping

36.

Value of num1
is :10

37.

Value of num2 is :20

38.

After Swapping

39.

Value of num1 is :20

40.

Value of num2 is :10

41.

*/

Swap Numbers Without Using Third Variable
Java Example

1.

/*

2.


Swap Numbers Without Using Third Variable Java Example

3.


This Swap Numbers Java Example shows how to

4.


swap va
lue of two numbers without using third variable using
java.

5.

*/

6.



7.

public

class

SwapElementsWithoutThirdVariableExample
{

8.



9.


public

static

void

main
(
String
[]

args
)

{

10.



11.



int

num1
=

10
;

12.



int

num2
=

20
;

13.



14.



System
.
out
.
println
(
"Before Swapping"
)
;

15.



System
.
out
.
println
(
"Value of num1 is :"

+

num1
)
;

16.



System
.
out
.
println
(
"Value of num2 is :"

+
num2
)
;

17.



18.



//add both the numbers and assign it to first

19.



num1
=

num1
+

num2
;

20.



num2
=

num1
-

num2
;

21.



num1
=

num1
-

num2
;

22.



23.



System
.
out
.
println
(
"Before Swapping"
)
;

24.



System
.
out
.
println
(
"Value of num1 is :"

+

num1
)
;

25.



System
.
out
.
println
(
"Value of num2 is :"

+
num2
)
;

26.


}

27.



28.



29.

}

30.



31.

/*

32.

Output of Swap Numbers Without Using Third Variable example would be

33.

Before Swapping

34.

Value of num1 is :10

35.

Value of num2 is :20

36.

Before Swapping

37.

Value o
f num1 is :20

38.

Value of num2 is :10

39.

*/

Create New Thread Using Runnable Example

1.

/*

2.


Create New Thread Using Runnable Example

3.


This Java example shows how to create a new thread by
implementing

4.


Java Runnable interface.

5.

*/

6.



7.

/*

8.


* To create a thread using Ru
nnable, a class must implement

9.


* Java Runnable interface.

10.


*/

11.

public

class

CreateThreadRunnableExample
implements

Runnable
{

12.



13.


/*

14.



* A class must implement run method to implement Runnable

15.



* interface. Signature of the run method is,

16.



*

17.



* public v
oid run()

18.



*

19.



* Code written inside run method will constite a new thread.

20.



* New thread will end when run method returns.

21.



*/

22.


public

void

run
(){

23.



24.



for
(
int

i
=
0
;

i
<

5
;

i
++
){

25.




System
.
out
.
println
(
"Child Thread : "

+

i
)
;

26.



27.




try
{

28.





Thread
.
sleep
(
5
0
)
;

29.




}

30.




catch
(
InterruptedException

ie
){

31.





System
.
out
.
println
(
"Child thread
interrupted! "

+

ie
)
;

32.




}

33.



}

34.



35.



System
.
out
.
println
(
"Child thread finished!"
)
;

36.


}

37.



38.


public

static

void

main
(
String
[]

args
)

{

39.



40.



/*

41.




* To create new thread, use

42.




* Thre
ad(Runnable thread, String threadName)

43.




* constructor.

44.




*

45.




*/

46.



47.



Thread

t
=

new

Thread
(
new

CreateThreadRunnableExample
()
,
"My Thread"
)
;

48.



49.



/*

50.




* To start a particular thread, use

51.




* void start() method of Thread class.

52.




*

53.




* Please note t
hat, after creation of a thread it
will not start

54.




* running until we call start method.

55.




*/

56.



57.



t.
start
()
;

58.



59.



for
(
int

i
=
0
;

i
<

5
;

i
++
){

60.



61.




System
.
out
.
println
(
"Main thread : "

+

i
)
;

62.



63.




try
{

64.





Thread
.
sleep
(
100
)
;

65.




}

66.




catch
(
InterruptedException

ie
){

67.





System
.
out
.
println
(
"Child thread
interrupted! "

+

ie
)
;

68.




}

69.



}

70.



System
.
out
.
println
(
"Main thread finished!"
)
;

71.


}

72.

}

73.



74.

/*

75.

Typical output of this thread example would be

76.



77.

Main thread : 0

78.

Child Thread : 0

79.

Child Thread : 1

80.

Main thread : 1

81.

Main threa
d : 2

82.

Child Thread : 2

83.

Child Thread : 3

84.

Main thread : 3

85.

Main thread : 4

86.

Child Thread : 4

87.

Child thread finished!

88.

Main thread finished!

89.



90.

*/

Get Current Thread Example

1.

/*

2.


Get Current Thread Example

3.


This Java example shows how to get reference of current th
read
using

4.


currentThread method of Java Thread class.

5.

*/

6.



7.

public

class

GetCurrentThreadExample
{

8.



9.


public

static

void

main
(
String
[]

args
)

{

10.



11.



/*

12.




* To get the reference of currently running thread,
use

13.




* Thread currentThread() method of Thread cl
ass.

14.




*

15.




* This is a static method.

16.




*/

17.



18.



Thread

currentThread
=

Thread
.
currentThread
()
;

19.



System
.
out
.
println
(
currentThread
)
;

20.



21.


}

22.

}

23.



24.

/*

25.

Output of the example would be

26.

Thread[main,5,main]

27.

*/

Applets

An applet is a program written in the Java prog
ramming language that can be
included in an HTML page, much in the same way an image is included in a page.
When you use a Java technology
-
enabled browser to view a page that contains an
applet, the applet's code is transferred to your system and executed
by the browser's
Java Virtual Machine (JVM). For information and examples on how to include an
applet in an HTML page, refer to this description of the
<APPLET>

tag.

Examp
le

The following example is made simple enough to illustrate the essential use of Java
applets through its java.applet package. It also uses classes from the Java
Abstract
Window Toolkit

(AWT) for producing actual output (in this case, the "
Hello, world!
"
message).

import

java.applet.Applet
;

import

java.awt.*
;



// Applet code for the "Hello, world!"
example.

// This should be saved in a file named as
"HelloWorld.java".

public

class

HelloWorld
extends

Applet

{


// This method is mandatory, but can be empty (i.e., have
no actual code).


public

void

init
()

{

}




// This method is mandatory, but can b
e empty.(i.e.,have
no actual code).


public

void

stop
()

{

}




// Print a message on the screen (x=20, y=10).


public

void

paint
(
Graphics

g
)

{


g.
drawString
(
"Hello, world!"
, 20,10
)
;




// Draws a circle on the screen (x=40, y=30).


g.
drawArc
(
40,3
0,20,20,0,360
)
;


}

}

Additional simple applets are available at Wikiversity.
[19]

For
compilation
, t
his code is saved on a plain
-
ASCII

file with the same name as the class
and
.java

extension, i.e.
HelloWorld.java
. The resulting
HelloWorld.class

applet should be placed on the web server and is invoked wi
thin an
HTML

page by using
an
<APPLET>

or an
<OBJECT>

tag. For example:

<!DOCTYPE HTML PUBLIC


"
-
//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<
HTML
>

<
HEAD
>

<
TITLE
>
HelloW
orld_example.html
<
/
TITLE
>

<
/
HEAD
>

<
BODY
>

<
H1
>
A Java applet example
<
/
H1
>

<
P
>
Here it is:
<
APPLET

code
=
"HelloWorld.class"

WIDTH
=
"200"

HEIGHT
=
"40"
>

This is where HelloWorld.class runs.
<
/
APPLET
><
/
P
>

<
/
BODY
>

<
/
HTML
>

Displaying the
HelloWorld_example.html

page fr
om a Web server, the result
should look as this:

A Java applet example

Here it is:
Hello, world!

To minimize download time, applets are usually delivered in a form of compressed
zip

archive (having
jar

extension). If all needed classes (only one in our case) are placed in
compressed archive
example.jar
, the embedding code would look diffe
rent:

<
P
>
Here it is:
<
APPLET

code
=
"HelloWorld"

WIDTH
=
"200"

HEIGHT
=
"40"

ARCHIVE
=
"example.jar"
>

This is where HelloWorld.class runs.
<
/
APPLET
><
/
P
>

Applet inclusion is described in detail in Sun's official page about the APPLET tag.
[20]

Advantages

A Java applet can have any or all of the following advantages
[21]
:



It is simple to make it work on Linux, Microsoft Windows and Mac OS X i.e. to
make it cross platform. Applets are supported by most
web browsers
.



The same applet can work on
"all" installed versions of Java at the same time,
rather than just the latest
plug
-
in

version only. However, if an applet requires a
later version of the
Java Runtime Environment

(JRE) the client will be forced to
wait during the large download.



Most web browsers
cache

applets
, so will be quick to load when returning to a
web page. Applets also improve with use: after a first applet is run, the JVM is
already running and starts quickly (the JVM will need to restart each time the
browser starts afresh).



It can move the work fro
m the
server

to the
client
, making a web solution more
scalable with the number
of users/clients.



If a standalone program (like
Google Earth
) talks to a web server, that server
normally needs to support all previous versions in case a user has not kept his or
her client softwa
re up to date. In contrast, a properly configured browser loads
(and caches) the latest applet version, so there is no need to support legacy
versions.



The applet naturally supports the changing user state, such as figure positions on
the chessboard.



Dev
elopers can develop and debug an applet direct simply by creating a main
routine (either in the applet's class or in a separate class) and calling init() and
start() on the applet, thus allowing for development in their favorite
Java SE

development environment. All one has to do after that is re
-
test the applet in the
AppletViewer program or a web browser to ensure it conforms to security
restri
ctions.



An
untrusted

applet has no access to the local machine and can only access the
server it came from. This makes such an applet much safer to run than a
standalone executable that it could repla
ce. However, a signed applet can have
full access to the machine it is running on if the user agrees.



Java applets are fast
-

and can even have
similar performance

to nati
ve installed
software.

Disadvantages

A Java applet may have any of the following disadvantages:



It requires the Java
plug
-
in
.



Some organizations only allow software

installed by the administrators. As a
result, some users can only view applets that are important enough to justify
contacting the administrator to request installation of the Java plug
-
in.



As with any client
-
side scripting, security restrictions may mak
e it difficult or
even impossible for an untrusted applet to achieve the desired goals.



Some applets require a specific JRE. This is discouraged.
[22]




If an applet req
uires a newer JRE than available on the system, or a specific JRE,
the user running it the first time will need to wait for the large JRE download to
complete.



Java automatic installation or update may fail if a
proxy server

is used to access
the web. This makes applets with specific requirements impossible to run unless
Java is manually updated. The Java automatic updater that is part of a Java
installation also may be complex to con
figure if it must work through a proxy.



Unlike the older
applet

tag, the
object

tag needs workarounds to write a
cross
-
browser HTML document.

Swing (Java)







Example Swing widgets in Java 5 for the
X Window System

Swing

is the primary
Java

GUI

widget toolkit
. It is part of
Sun Microsystems
'
Java
F
oundation Classes

(JFC)


an
API

for providing a
graphical user inte
rface

(GUI) for
Java programs.

Swing was developed to provide a more sophisticated set of GUI
components

than the
earlier
Abstract Window Toolkit
. Swing provides a native
look and feel

that emulates the
look and feel of several platforms, and also supports a
pluggable look and feel

that allows
applications to have a look and feel unrelated to the underlying platform.

History

The
Internet Foundation Classes

(IFC) were a
graphics library

for Java originally
developed by
Netscape
Communications Corporation

and first released on December 16,
1996. On April 2, 1997,
Sun Microsystems

and
Netscape Communications Corporation

announced their intention to incorporate IFC with other technologies to form the
Java
Foundation Classes
.

Swing int
roduced a mechanism that allowed the
look and feel

of every component in an
application to be altered without making substantial changes to the application code. The
introduction of support for a
pluggable look and feel

allows Swing components to
emulate the appearance of native components while still retaining the benefits of platform
independence. This feature also makes it easy
to make an application written in Swing
look very different from native programs if desired.

Originally distributed as a separately downloadable library, Swing has been included as
part of the
Java Standard Edition

since release 1.2. The Swing classes and components
are contained in the
javax.swing

package

hierarchy.

The Swing Architecture

Swing is a platform
-
independent,
Model
-
View
-
Contr
oller

GUI

framework for Java. It
follows a single
-
threaded

programming model, and possesses the following tra
its:

Foundations

Swing is platform independent both in terms of expression (Java) and implementation
(Look
-
and
-
Feel).

Extensible

Swing is a highly partitioned architecture, which allows for the "plugging" of various
custom implementations of specified fram
ework interfaces: Users can provide their own
custom implementation(s) of these components to override the default implementations.
In general, Swing users can extend the framework by extending existing (framework)
classes and/or providing alternative impl
ementations of core components.

Swing is a component
-
based framework. The distinction between objects and
components is a fairly subtle point: concisely, a component is a well
-
behaved object with
a known/specified characteristic pattern of behaviour. Swing

objects asynchronously fire
events, have "bound" properties, and respond to a well
-
known set of commands (specific
to the component.) Specifically, Swing components are
Java Beans

com
ponents,
compliant with the Java Beans Component Architecture specifications.

Customizable

Given the programmatic rendering model of the Swing framework, fine control over the
details of rendering of a component is possible in Swing. As a general pattern,
the visual
representation of a Swing component is a composition of a standard set of elements, such
as a "border", "inset", decorations, etc. Typically, users will programmatically customize
a standard Swing component (such as a JTable) by assigning specif
ic Borders, Colors,
Backgrounds, opacities, etc., as the properties of that component. The core component
will then use these properties (settings) to determine the appropriate renderers to use in
painting its various aspects. However, it is also completel
y possible to create unique GUI
controls with highly customized visual representation.

Configurable

Swing's heavy reliance on runtime mechanisms and indirect composition patterns allows
it to respond at runtime to fundamental changes in its settings. For e
xample, a Swing
-
based application can change its look and feel at runtime. Further, users can provide their
own look and feel implementation, which allows for uniform changes in the look and feel
of existing Swing applications without any programmatic chan
ge to the application code.

Lightweight UI

Swing's configurability is a result of a choice not to use the native host OS's GUI controls
for displaying itself. Swing "paints" its controls programmatically through the use of Java
2D APIs, rather than callin
g into a native user interface toolkit. Thus, a Swing component
does not have a corresponding native OS GUI component, and is free to render itself in
any way that is possible with the underlying graphics APIs.

However, at its core every Swing component re
lies on an
AWT

container, since (Swing's)
JComponent

extends (AWT's)
Container. This allows Swing to plug into the host OS's
GUI management framework, including the crucial device/screen mappings and user
interactions, such as key presses or mouse movements. Swing simply "transposes" its
own (OS agnostic) semantics over the

underlying (OS specific) components. So, for
example, every Swing component paints its rendition on the graphic device in response to
a call to component.paint(), which is defined in (AWT) Container. But unlike AWT
components, which delegated the painting

to their OS
-
native "heavyweight" widget,
Swing components are responsible for their own rendering.

This transposition and decoupling is not merely visual, and extends to Swing's
management and application of its own OS
-
independent semantics for events fir
ed within
its component containment hierarchies. Generally speaking, the Swing Architecture
delegates the task of mapping the various flavors of OS GUI semantics onto a simple, but
generalized, pattern to the AWT container. Building on that generalized pla
tform, it
establishes its own rich and complex GUI semantics in the form of the
JComponent

model.

Loosely
-
Coupled and MVC

The Swing library makes heavy use of the
Model/View/Controller

software
design
pattern
,
[1]

which conceptually decouples the data being viewed from the user interface
controls through which it is viewed. Because of this, most Swing components have
associated
models

(which are specified in terms of Java
interfaces
), and the programmer
can use various default implementations or provide their own. The frame
work provides
default implementations of model interfaces for all of its concrete components. The
typical use of the Swing framework does not require the creation of custom models, as
the framework provides a set of default implementations that are transpa
rently, by
default, associated with the corresponding
JComponent

child class in the Swing
library. In general, only complex components, such as tables, trees and somet
imes lists,
may require the custom model implementations around the application
-
specific data
structures. To get a good sense of the potential that the Swing architecture makes
possible, consider the hypothetical situation where custom models for tables an
d lists are
wrappers over
DAO

and/or
EJB

services..

Typically, Swing component model objects are responsible

for providing a concise
interface defining events fired, and accessible properties for the (conceptual) data model
for use by the associated JComponent. Given that the overall MVC pattern is a loosely
-
coupled collaborative object relationship pattern, the

model provides the programmatic
means for attaching event listeners to the data model object. Typically, these events are
model centric (ex: a "row inserted" event in a table model) and are mapped by the
JComponent specialization into a meaningful event f
or the GUI component.

For example, the
JTable

has a model called
TableModel

that describes an interface
for how a table would access tabular data. A default implementation of this operates on a
two
-
dimensional
array
.

The view component of a

Swing JComponent is the object used to graphically
"represent" the conceptual GUI control. A distinction of Swing, as a GUI framework, is
in its reliance on programmatically
-
rendered GUI controls (as opposed to the use of the
native host OS's GUI controls
). Prior to
Java 6 Update 10
, this distinction was a source of
complications when mixing AWT controls, which use native controls, with Swin
g
controls in a GUI (see
Mixing AWT and Swing components
).

Finally, in terms of visual composition and management, Swing
favors
relative layouts

(which specify the positional relationships between components) as opposed to absolute
layouts (which specify the exact location and size of components)
. This bias towards
"fluid"' visual ordering is due to its origins in the
applet

operating environment that
framed the design and development of the original Java GUI toolkit. (Conce
ptually, this
view of the layout management is quite similar to that which informs the rendering of
HTML content in browsers, and addresses the same set of concerns that motivated the
former.)

Relationship to AWT

AWT and Swing class hierarchy

Since early v
ersions of Java, a portion of the
Abstract Window Toolkit

(AWT) has
provided platform
-
independent APIs for user interface components. In AWT, each
component is rendered and controlled by
a native peer component specific to the
underlying windowing system.

By contrast, Swing components are often described as
lightweight

because they do not
require allocation of native resources in the operating system's windowing toolkit. The
AWT components

are referred to as
heavyweight components
.

Much of the Swing API is generally a complementary extension of the AWT rather than
a direct replacement. In fact, every Swing lightweight interface ultimately exists within
an AWT heavyweight component because a
ll of the top
-
level components in Swing
(
JApplet
,
JDialog
,
JFrame
, and
JWindow
) extend an AWT top
-
level container.
Prior to
Java 6 Update 10
, the use of both lightweight and heavyweight components
within the same window was generally discouraged due to
Z
-
o
rder

incompatibilities.
However, later version of Java have fixed these issues, and both Swing and AWT
components can now be used in one GUI without Z
-
order issues.

The core rendering functionality used by Swing to draw its lightweight components is
provi
ded by
Java 2D
, another part of JFC.

Relationship to SWT

The
Standard Widget Toolkit

(SWT) is a competing toolkit originally developed

by
IBM

and now maintained by the
Eclipse

community
. SWT's implementation has more in
common with the heavyweight components of AWT. This confers benefits such as more
accurate fidelity with the underlying native windowing toolkit, at the cost of an increased
exposure to the n
ative platform in the programming model.

The advent of SWT has given rise to a great deal of division among Java desktop
developers, with many strongly favoring either
SWT

or Swing. Sun's development on
Swing continues to focus on platform look and feel (PLAF) fidelity with each platform's
windowing toolkit in the approaching
Java SE

7 release (as of December 2006
).

There has been significant debate and speculation about the performance of SWT versus
Swing; som
e hinted that SWT's heavy dependence on
JNI

would make it slower when the
GUI component and Java need to communicate data, but faster at rendering when the
data m
odel has been loaded into the GUI, but this has not been confirmed either way.
[2]

A
fairly thorough set of benchmarks in 2005 concluded that neither Swing nor SWT clearl
y
outperformed the other in the general case.
[3]

SWT serves the Windows platform very well but is considered by some
[
who?
]

to be less
effective as a technology for cross
-
platform development. By using the high
-
level
features of each native windowing toolkit, SWT returns to the issues seen in the mid
1990s (with tool
kits like zApp, Zinc, XVT and IBM/Smalltalk) where toolkits attempted
to mask differences in focus behaviour, event triggering and graphical layout. Failure to
match behavior on each platform can cause subtle but difficult
-
to
-
resolve bugs that
impact user
interaction and the appearance of the GUI.

Examples

The following is a rather simple Swing
-
based program. It displays a window (a
JFrame
)
containing a label and a button.

Notice how all instantiation and handling of all Swing components are done on the
Event
Dispatch Thread

by use of the method
EventQueue.invokeLater(Runnable)
)
and an anonymous Runnable class (see
Swing and thread safety
).

// Import the swing and AWT classes needed

import

java.awt.EventQueue
;

import

java.awt.FlowLayout
;



import

javax.swing.JButton
;

import

javax.swing.JFrame
;

import

javax.swing.JLabel
;



/**


* Basic Sw
ing example.


*/

public

class

SwingExample
{


public

static

void

main
(
String
[]

args
)

{




// Make sure all Swing/AWT instantiations and
accesses are done on the


// Event Dispatch Thread (EDT)


EventQueue
.
invokeLater
(
new

Runnable
()

{


public

void

run
()

{


// Create a JFrame, which is a Window with
"decorations", i.e.


// title, border and close
-
button


JFrame

f
=

new

JFrame
(
"Swing Example
Window"
)
;




// Set a sim
ple Layout Manager that
arranges the contained


// Components


f.
setLayout
(
new

FlowLayout
())
;




// Add some Components


f.
add
(
new

JLabel
(
"Hello, world!"
))
;


f.
add
(
new

JButton
(
"Pres
s me!"
))
;




// "Pack" the window, making it "just big
enough".


f.
pack
()
;




// Set the default close operation for the
window, or else the


// program won't exit when clicking close
button



// (The default is HIDE_ON_CLOSE, which
just makes the window


// invisible, and thus doesn't exit the
app)


f.
setDefaultCloseOperation
(
JFrame
.
DISPOSE_ON_CLOSE
)
;




// Set the visibility as true, th
ereby
displaying it


f.
setVisible
(
true
)
;


}


})
;


}

}


//Example in Swing Data Base Manipulation

Login.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.lang.StringBuffer;

import java.io
.IOException;

import java.io.*;

import java.sql.*;

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import java.util.Date;


//******************

public class Login extends JFrame implem
ents ActionListener

{

private Connection dbconn;

private static String info;

private javax.swing.JLabel a;


Container c1;

JLabel j;

JLabel l1,l2,l3,l4,l5,lmsg,lHead,lh,lm;

JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15;

JPasswordField p1;

JB
utton b1,b2,b3,b4,b5,b6;

JComboBox combo1,combo2,combo3;

JTextArea ta1,ta2;

Image img,img1,img2,img3;

Date dt;

JFormattedTextField tft1,tft2;


//myDataPanel=new DataPanel();

//********Combo Box **************

//*************For Image*****************


pub
lic void update(Graphics g)


{


paint(g);


}




public void paint(Graphics g){


if(img != null)


{


g.drawImage(img, 3, 20, this);


}


else


g.clearRect(0, 0, getSize().width, getSize().height);


if(img1 != null)


{


g.drawImage(img1, 3, 110, this);


}


else


g.clearRect(0, 0, getSize().width, getSize().height);


if(img2 != null)


{


g.drawImage(img2, 3, 270, this);


}


else


g.clearRect(0, 0, getSize().width, getSize().hei
ght);


if(img3 != null)


{


g.drawImage(img3, 3, 550, this);


}


else


g.clearRect(0, 0, getSize().width, getSize().height);


}

//********************************

public Login()

{


try


{


String url = "jdbc:o
dbc:CRES"; //make this match your data


//source name


Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );


dbconn = DriverManager.getConnection( url );


info="Connection su
ccessful
\
n";


}


catch ( ClassNotFoundException cnfex ) //yes 3 catches


{


cnfex.printStackTrace();


info=info+"Connection unsuccessful
\
n" + cnfex.toString();


}


catch ( SQLException sqlex )


{


sqlex.printStackTrace();


info=info+"Connection unsuccessful
\
n" +sqlex.toString();


}


catch ( Exception excp )


{


excp.printStackTrace();


info=info+excp.toString();


}

setTitle("CRES");

setResizable(true);

setSize(920,800);

setVisible(true);

//**********************************

c1=getContentPane();

c1.setLayout(null);

//c1.setBackground(Color.white);

//************IMAGE***************


MediaTracker mt = new MediaTracker(this);


img
= Toolkit.getDefaultToolkit().getImage("head_cres.jpg");


mt.addImage(img,0);



MediaTracker mt1 = new MediaTracker(this);


img1 = Toolkit.getDefaultToolkit().getImage("H.jpg");


mt1.addImage(img1,0);



MediaTracker mt2 = new MediaTracker
(this);


img2 = Toolkit.getDefaultToolkit().getImage("H.jpg");


mt2.addImage(img2,0);



MediaTracker mt3 = new MediaTracker(this);


img3 = Toolkit.getDefaultToolkit().getImage("H.jpg");


mt3.addImage(img3,0);


//*********************


lHe
ad=new JLabel("COMMON RECORD OF EXAMINATION AND
STUDENT");

c1.add(lHead);

lHead.setForeground(Color.red);

lHead.setFont(new Font("Times New Roman",Font.BOLD,30));


lh=new JLabel("Login....");

c1.add(lh);

lh.setForeground(Color.blue);

lh.setFont(new Font("T
imes New Roman",Font.BOLD,20));



l1=new JLabel("Login Name : ");

c1.add(l1);

l1.setForeground(Color.blue);


t1=new JTextField(20);

c1.add(t1);

setVisible(true);


l2=new
JLabel("Password : ");

c1.add(l2);

l2.setForeground(Color.blue);

p1=new JPasswordField
(20);

c1.add(p1);


//**************setBounds********************

lHead.setBounds(25,60,800,20);

lh.setBounds(15,115,800,25);

l1.setBounds(150,125,200,25);

t1.setBounds(250,125,200,25);

l2.setBounds(150,155,300,25);

p1.setBounds(250,155,200,25);

//********B
utton*****************


b1=new JButton("LOGIN");

b1.setBounds(200,200,100,25);

c1.add(b1);

b1.addActionListener(this);

b1.setForeground(Color.white);

b1.setBackground(Color.red);

b1.setVisible(true);


b2=new JButton("EXIT");

b2.setBounds(350,200,100,25);

c
1.add(b2);

b2.addActionListener(this);

b2.setForeground(Color.white);

b2.setBackground(Color.red);

b2.setVisible(true);