SlideShare a Scribd company logo
https://www.facebook.com/Oxus20 
oxus20@gmail.com 
Java Applet & Graphics 
Java Applet 
Java Graphics 
Analog Clock 
Prepared By: Khosrow Kian 
Edited By: Abdul Rahman Sherzad
Table of Contents 
»Java Applet 
˃Introduction and Concept 
˃Demos 
»Graphics 
˃Introduction and Concept 
»Java Applet Code 
2 
https://www.facebook.com/Oxus20
Java Applet 
»An applet is a subclass of Panel 
˃It is a container which can hold GUI components 
˃It has a graphics context which can be used to draw images 
»An applet embedded within an HTML page 
˃Applets are defined using the <applet> tag 
˃Its size and location are defined within the tag 
»Java Virtual Machine is required for the browsers to execute the applet 
3 
https://www.facebook.com/Oxus20
Java Applets vs. Applications 
»Applets - Java programs that can run over the Internet using a browser. 
˃The browser either contains a JVM (Java Virtual Machine) or loads the Java plugin 
˃Applets do not require a main(), but in general will have a paint(). 
˃An Applet also requires an HTML file before it can be executed. 
˃Java Applets are also compiled using the javac command, but are run either with a browser or with the applet viewer command. 
»Applications - Java programs that run directly on your machine. 
˃Applications must have a main(). 
˃Java applications are compiled using the javac command and run using the java command. 
4 
https://www.facebook.com/Oxus20
Java Applets vs. Applications 
Feature 
Application 
Applet 
main() method 
Present 
Not present 
Execution 
Requires JRE 
Requires a browser like Chrome, Firefox, IE, Safari, Opera, etc. 
Nature 
Called as stand-alone application as application can be executed from command prompt 
Requires some third party tool help like a browser to execute 
Restrictions 
Can access any data or software available on the system 
cannot access any thing on the system except browser’s services 
Security 
Does not require any security 
Requires highest security for the system as they are untrusted 
5 
https://www.facebook.com/Oxus20
Java Applet Advantages 
»Execution of applets is easy in a Web browser and does not require any installation or deployment procedure in real-time programming. 
»Writing and displaying (just opening in a browser) graphics and animations is easier than applications. 
»In GUI development, constructor, size of frame, window closing code etc. are not required. 
6 
https://www.facebook.com/Oxus20
Java Applet Methods 
»init() 
˃Called when applet is loaded onto user’s machine. 
»start() 
˃Called when applet becomes visible (page called up). 
»stop() 
˃Called when applet becomes hidden (page loses focus). 
»destroy() 
˃Guaranteed to be called when browser shuts down. 
7 
https://www.facebook.com/Oxus20
Introduction to Java Graphics 
»Java contains support for graphics that enable programmers to visually enhance applications 
»Java contains many more sophisticated drawing capabilities as part of the Java 2D API 
˃Color 
˃Font and FontMetrics 
˃Graphics2D 
˃Polygon 
˃BasicStroke 
˃GradientPaint and TexturePaint 
˃Java 2D shape classes 
8 
https://www.facebook.com/Oxus20
9 
https://www.facebook.com/Oxus20
Java Coordinate System 
»Upper-Left Corner of a GUI component has the coordinates (0, 0) 
»X-Coordinate (horizontal coordinate) 
˃horizontal distance moving right from the left of the screen 
»Y-Coordinate (vertical coordinate) 
˃vertical distance moving down from the top of the screen 
»Coordinate units are measured in pixels. 
˃A pixel is a display monitor’s smallest unit of resolution. 
https://www.facebook.com/Oxus20 
10
All Roads Lead to JComponent 
»Every Swing object inherits from JComponent 
» JComponent has a few methods that can be overridden in order to draw special things 
˃public void paint(Graphics g) 
˃public void paintComponent(Graphics g) 
˃public void repaint() 
»So if we want custom drawing, we take any JComponent and extend it... 
˃JPanel is a good choice 
11 
https://www.facebook.com/Oxus20
Draw Line Example 
import java.awt.Graphics; 
import javax.swing.JApplet; 
public class DrawLine extends JApplet { 
@Override 
public void init() { 
} 
public void paint(Graphics g){ 
g.drawLine(20,20, 100,100); 
} 
} 
https://www.facebook.com/Oxus20 
12
Draw Rectangles Example 
import java.awt.Graphics; 
import javax.swing.JApplet; 
public class DrawRect extends JApplet { 
@Override 
public void init() { 
super.init(); 
} 
public void paint(Graphics g) { 
g.drawRect(20, 20, 100, 100); 
g.fillRect(130, 20, 100, 100); 
g.drawRoundRect(240, 20, 100, 100, 10, 10); 
} 
} 
https://www.facebook.com/Oxus20 
13
Draw Ovals Example 
import java.awt.Graphics; 
import javax.swing.JApplet; 
public class DrawOval extends JApplet { 
@Override 
public void init() { 
} 
public void paint(Graphics g) { 
g.drawOval(20, 20, 100, 100); 
g.fillOval(130, 20, 100, 100); 
} 
} 
https://www.facebook.com/Oxus20 
14
Simple Calculator Example 
import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.Panel; 
import java.awt.TextField; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
public class Calculator extends Applet implements ActionListener { 
String operators[] = { "+", "-", "*", "/", "=", "C" }; 
String operator = ""; 
int previousValue = 0; 
Button buttons[] = new Button[16]; 
TextField txtResult = new TextField(10); 
15 
https://www.facebook.com/Oxus20
public void init() { 
setLayout(new BorderLayout()); 
add(txtResult, "North"); 
txtResult.setText("0"); 
Panel p = new Panel(); 
p.setLayout(new GridLayout(4, 4)); 
for (int i = 0; i < 16; i++) { 
if (i < 10) { 
buttons[i] = new Button(String.valueOf(i)); 
} else { 
buttons[i] = new Button(operators[i % 10]); 
} 
buttons[i].setFont(new Font("Verdana", Font.BOLD, 18)); 
p.add(buttons[i]); 
add(p, "Center"); 
buttons[i].addActionListener(this); 
} 
} 
16 
https://www.facebook.com/Oxus20
public void actionPerformed(ActionEvent ae) { 
int result = 0; 
String caption = ae.getActionCommand(); 
int currentValue = Integer.parseInt(txtResult.getText()); 
if (caption.equals("C")) { 
txtResult.setText("0"); 
previousValue = 0; 
currentValue = 0; 
result = 0; 
operator = ""; 
} else if (caption.equals("=")) { 
result = 0; 
if (operator == "+") 
result = previousValue + currentValue; 
else if (operator == "-") 
result = previousValue - currentValue; 
else if (operator == "*") 
result = previousValue * currentValue; 
else if (operator == "/") 
result = previousValue / currentValue; 
txtResult.setText(String.valueOf(result)); 
} 
17 
https://www.facebook.com/Oxus20
End - Simple Calculator Example 
else if (caption.equals("+") || caption.equals("-") 
|| caption.equals("*") || caption.equals("/")) { 
previousValue = currentValue; 
operator = caption; 
txtResult.setText("0"); 
} else { 
int value = currentValue * 10 + Integer.parseInt(caption); 
txtResult.setText(String.valueOf(value)); 
} 
} 
} 
18 
https://www.facebook.com/Oxus20
OUTPUT - Simple Calculator Example 
19 
https://www.facebook.com/Oxus20
Example of Graphics and Applet 
20 
https://www.facebook.com/Oxus20
Analog Clock Example 
21 
https://www.facebook.com/Oxus20 
import java.applet.Applet; 
import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.util.Calendar; 
public class AnalogClock extends Applet implements Runnable { 
private static final long serialVersionUID = 1L; 
private static final double TWO_PI = 2.0 * Math.PI; 
private Calendar nw = Calendar.getInstance(); 
int width = 200, hight = 200; 
int xcent = width / 2, ycent = hight / 2; 
int minhand, maxhand; 
double rdns; 
int dxmin, dymin, dxmax, dymax; 
double radins, sine, cosine; 
double fminutes; 
Thread t = null; 
Boolean stopFlag;
22 
https://www.facebook.com/Oxus20 
public void start() { 
t = new Thread(this); 
stopFlag = false; 
t.start(); 
} 
public void run() { 
for (;;) { 
try { 
updateTime(); 
repaint(); 
Thread.sleep(1000); 
if (stopFlag) 
break; 
} catch (InterruptedException e) { 
} 
} 
} 
public void stop() { 
stopFlag = true; 
t = null; 
} 
private void updateTime() { 
nw.setTimeInMillis(System.currentTimeMillis()); 
}
23 
https://www.facebook.com/Oxus20 
public void paint(Graphics g) { 
g.setFont(new Font("Gabriola", Font.BOLD + Font.ITALIC, 160)); 
g.setColor(Color.RED); 
g.drawString("XUS", 300, 270); 
g.setFont(new Font("Consolas", Font.BOLD + Font.ITALIC, 100)); 
g.setColor(Color.GREEN); 
g.drawString("20", 550, 270); 
g.setColor(Color.black); 
g.fillOval(100, 100, 200, 200); 
Graphics2D g1 = (Graphics2D) g; 
int hours = nw.get(Calendar.HOUR); 
int minutes = nw.get(Calendar.MINUTE); 
int seconds = nw.get(Calendar.SECOND); 
int millis = nw.get(Calendar.MILLISECOND); 
minhand = width / 8; 
maxhand = width / 2; 
rdns = (seconds + ((double) millis / 1000)) / 60.0; 
drw(g1, rdns, 0, maxhand - 20); 
g1.setColor(Color.BLUE); 
g1.drawString( 
String.format("%02d : %02d :%02d ", hours, minutes, seconds), 
minhand + 150, maxhand + 170);
24 
https://www.facebook.com/Oxus20 
minhand = 0; // Minute hand starts in middle. 
maxhand = width / 3; 
fminutes = (minutes + rdns) / 60.0; 
drw(g1, fminutes, 0, maxhand); 
minhand = 0; // Minute hand starts in middle. 
maxhand = width / 4; 
drw(g1, (hours + fminutes) / 12.0, 0, maxhand); 
g1.setColor(Color.gray); // set b ackground of circle 
g1.drawOval(100, 100, 200, 200); // draw a circle 
g1.setColor(Color.WHITE); 
g1.setFont(new Font("Consulas", Font.BOLD + Font.ITALIC, 15)); 
g1.drawString("12", 190, 120); 
g1.drawString("6", 195, 290); 
g1.drawString("3", 280, 200); 
g1.drawString("6", 110, 200); 
g1.setFont(new Font("Consulas", Font.BOLD + Font.ITALIC, 15)); 
g1.setStroke(new BasicStroke(2, BasicStroke.JOIN_MITER, 
BasicStroke.JOIN_BEVEL)); 
}
End - Analog Clock Example 
public void drw(Graphics2D g, double prct, int minRadius, int maxRadius) { 
radins = (0.5 - prct) * TWO_PI; 
sine = Math.sin(radins); 
cosine = Math.cos(radins); 
dxmin = xcent + (int) (minRadius * sine); 
dymin = ycent + (int) (minRadius * cosine); 
dxmax = xcent + (int) (maxRadius * sine); 
dymax = ycent + (int) (maxRadius * cosine); 
g.setColor(Color.WHITE); 
g.setBackground(Color.cyan); 
g.setFont(new Font("Consulas", Font.BOLD + Font.ITALIC, 12)); 
g.drawLine(dxmin + 100, dymin + 100, dxmax + 100, dymax + 100); 
} 
} 
25 
https://www.facebook.com/Oxus20
OUTPUT - Analog Clock Example 
26 
https://www.facebook.com/Oxus20
END 
https://www.facebook.com/Oxus20 
27

More Related Content

PDF
Java Applet and Graphics
PPT
Web Security Mistakes: Trusting The Client
PPTX
C#Web Sec Oct27 2010 Final
PDF
Java Regular Expression PART I
PDF
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PDF
Everything about Object Oriented Programming
PPTX
TKP Java Notes for Teaching Kids Programming
PDF
Java Unicode with Cool GUI Examples
Java Applet and Graphics
Web Security Mistakes: Trusting The Client
C#Web Sec Oct27 2010 Final
Java Regular Expression PART I
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Everything about Object Oriented Programming
TKP Java Notes for Teaching Kids Programming
Java Unicode with Cool GUI Examples

Viewers also liked (20)

PDF
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
PDF
Java Regular Expression PART II
PPTX
Conditional Statement
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
PPTX
Structure programming – Java Programming – Theory
PDF
Object Oriented Concept Static vs. Non Static
PDF
Java Guessing Game Number Tutorial
PDF
Create Splash Screen with Java Step by Step
PDF
Web Design and Development Life Cycle and Technologies
PDF
Everything about Database JOINS and Relationships
PDF
Note - Java Remote Debug
DOCX
Core java notes with examples
PDF
Java Unicode with Live GUI Examples
PDF
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
PDF
Jdbc Complete Notes by Java Training Center (Som Sir)
DOC
Advanced core java
PPT
Java essential notes
PDF
Java Lab Manual
PDF
Java Arrays
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Regular Expression PART II
Conditional Statement
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Structure programming – Java Programming – Theory
Object Oriented Concept Static vs. Non Static
Java Guessing Game Number Tutorial
Create Splash Screen with Java Step by Step
Web Design and Development Life Cycle and Technologies
Everything about Database JOINS and Relationships
Note - Java Remote Debug
Core java notes with examples
Java Unicode with Live GUI Examples
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Jdbc Complete Notes by Java Training Center (Som Sir)
Advanced core java
Java essential notes
Java Lab Manual
Java Arrays
Ad

Similar to Java Applet and Graphics (20)

PPT
Graphics programming in Java
PDF
The 2016 Android Developer Toolbox [MOBILIZATION]
PDF
A More Flash Like Web?
PDF
2016 gunma.web games-and-asm.js
PDF
HTML5 - Daha Flash bir web?
PPTX
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
PPTX
Introduction To Google Android (Ft Rohan Bomle)
PDF
mobl
PPT
Basic of Applet
PPT
Google Web Toolkit
PPTX
Applet progming
PPT
WebGL: GPU acceleration for the open web
PDF
@Ionic native/google-maps
PPT
Appletsbjhbjiibibibikbibibjibjbibbjb.ppt
PPT
Applets(1)cusdhsiohisdhfshihfsihfohf.ppt
PPTX
Svcc 2013-d3
PPTX
SVCC 2013 D3.js Presentation (10/05/2013)
PPTX
PDF
MOPCON 2014 - Best software architecture in app development
PDF
Google's HTML5 Work: what's next?
Graphics programming in Java
The 2016 Android Developer Toolbox [MOBILIZATION]
A More Flash Like Web?
2016 gunma.web games-and-asm.js
HTML5 - Daha Flash bir web?
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Introduction To Google Android (Ft Rohan Bomle)
mobl
Basic of Applet
Google Web Toolkit
Applet progming
WebGL: GPU acceleration for the open web
@Ionic native/google-maps
Appletsbjhbjiibibibikbibibjibjbibbjb.ppt
Applets(1)cusdhsiohisdhfshihfsihfohf.ppt
Svcc 2013-d3
SVCC 2013 D3.js Presentation (10/05/2013)
MOPCON 2014 - Best software architecture in app development
Google's HTML5 Work: what's next?
Ad

More from OXUS 20 (7)

PPTX
Java Methods
PDF
Fundamentals of Database Systems Questions and Answers
PDF
JAVA GUI PART III
PDF
Java GUI PART II
PDF
JAVA GUI PART I
PDF
JAVA Programming Questions and Answers PART III
PDF
Object Oriented Programming with Real World Examples
Java Methods
Fundamentals of Database Systems Questions and Answers
JAVA GUI PART III
Java GUI PART II
JAVA GUI PART I
JAVA Programming Questions and Answers PART III
Object Oriented Programming with Real World Examples

Recently uploaded (20)

PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Presentation on Janskhiya sthirata kosh.
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PDF
English Language Teaching from Post-.pdf
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
UTS Health Student Promotional Representative_Position Description.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
PDF
Sunset Boulevard Student Revision Booklet
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
How to Manage Global Discount in Odoo 18 POS
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
Odoo 18 Sales_ Managing Quotation Validity
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
Onica Farming 24rsclub profitable farm business
Presentation on Janskhiya sthirata kosh.
Software Engineering BSC DS UNIT 1 .pptx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
English Language Teaching from Post-.pdf
How to Manage Starshipit in Odoo 18 - Odoo Slides
102 student loan defaulters named and shamed – Is someone you know on the list?
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
UTS Health Student Promotional Representative_Position Description.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Skill Development Program For Physiotherapy Students by SRY.pptx
Sunset Boulevard Student Revision Booklet
Introduction and Scope of Bichemistry.pptx
How to Manage Global Discount in Odoo 18 POS
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Odoo 18 Sales_ Managing Quotation Validity
How to Manage Loyalty Points in Odoo 18 Sales

Java Applet and Graphics

  • 1. https://www.facebook.com/Oxus20 [email protected] Java Applet & Graphics Java Applet Java Graphics Analog Clock Prepared By: Khosrow Kian Edited By: Abdul Rahman Sherzad
  • 2. Table of Contents »Java Applet ˃Introduction and Concept ˃Demos »Graphics ˃Introduction and Concept »Java Applet Code 2 https://www.facebook.com/Oxus20
  • 3. Java Applet »An applet is a subclass of Panel ˃It is a container which can hold GUI components ˃It has a graphics context which can be used to draw images »An applet embedded within an HTML page ˃Applets are defined using the <applet> tag ˃Its size and location are defined within the tag »Java Virtual Machine is required for the browsers to execute the applet 3 https://www.facebook.com/Oxus20
  • 4. Java Applets vs. Applications »Applets - Java programs that can run over the Internet using a browser. ˃The browser either contains a JVM (Java Virtual Machine) or loads the Java plugin ˃Applets do not require a main(), but in general will have a paint(). ˃An Applet also requires an HTML file before it can be executed. ˃Java Applets are also compiled using the javac command, but are run either with a browser or with the applet viewer command. »Applications - Java programs that run directly on your machine. ˃Applications must have a main(). ˃Java applications are compiled using the javac command and run using the java command. 4 https://www.facebook.com/Oxus20
  • 5. Java Applets vs. Applications Feature Application Applet main() method Present Not present Execution Requires JRE Requires a browser like Chrome, Firefox, IE, Safari, Opera, etc. Nature Called as stand-alone application as application can be executed from command prompt Requires some third party tool help like a browser to execute Restrictions Can access any data or software available on the system cannot access any thing on the system except browser’s services Security Does not require any security Requires highest security for the system as they are untrusted 5 https://www.facebook.com/Oxus20
  • 6. Java Applet Advantages »Execution of applets is easy in a Web browser and does not require any installation or deployment procedure in real-time programming. »Writing and displaying (just opening in a browser) graphics and animations is easier than applications. »In GUI development, constructor, size of frame, window closing code etc. are not required. 6 https://www.facebook.com/Oxus20
  • 7. Java Applet Methods »init() ˃Called when applet is loaded onto user’s machine. »start() ˃Called when applet becomes visible (page called up). »stop() ˃Called when applet becomes hidden (page loses focus). »destroy() ˃Guaranteed to be called when browser shuts down. 7 https://www.facebook.com/Oxus20
  • 8. Introduction to Java Graphics »Java contains support for graphics that enable programmers to visually enhance applications »Java contains many more sophisticated drawing capabilities as part of the Java 2D API ˃Color ˃Font and FontMetrics ˃Graphics2D ˃Polygon ˃BasicStroke ˃GradientPaint and TexturePaint ˃Java 2D shape classes 8 https://www.facebook.com/Oxus20
  • 10. Java Coordinate System »Upper-Left Corner of a GUI component has the coordinates (0, 0) »X-Coordinate (horizontal coordinate) ˃horizontal distance moving right from the left of the screen »Y-Coordinate (vertical coordinate) ˃vertical distance moving down from the top of the screen »Coordinate units are measured in pixels. ˃A pixel is a display monitor’s smallest unit of resolution. https://www.facebook.com/Oxus20 10
  • 11. All Roads Lead to JComponent »Every Swing object inherits from JComponent » JComponent has a few methods that can be overridden in order to draw special things ˃public void paint(Graphics g) ˃public void paintComponent(Graphics g) ˃public void repaint() »So if we want custom drawing, we take any JComponent and extend it... ˃JPanel is a good choice 11 https://www.facebook.com/Oxus20
  • 12. Draw Line Example import java.awt.Graphics; import javax.swing.JApplet; public class DrawLine extends JApplet { @Override public void init() { } public void paint(Graphics g){ g.drawLine(20,20, 100,100); } } https://www.facebook.com/Oxus20 12
  • 13. Draw Rectangles Example import java.awt.Graphics; import javax.swing.JApplet; public class DrawRect extends JApplet { @Override public void init() { super.init(); } public void paint(Graphics g) { g.drawRect(20, 20, 100, 100); g.fillRect(130, 20, 100, 100); g.drawRoundRect(240, 20, 100, 100, 10, 10); } } https://www.facebook.com/Oxus20 13
  • 14. Draw Ovals Example import java.awt.Graphics; import javax.swing.JApplet; public class DrawOval extends JApplet { @Override public void init() { } public void paint(Graphics g) { g.drawOval(20, 20, 100, 100); g.fillOval(130, 20, 100, 100); } } https://www.facebook.com/Oxus20 14
  • 15. Simple Calculator Example import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Font; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Calculator extends Applet implements ActionListener { String operators[] = { "+", "-", "*", "/", "=", "C" }; String operator = ""; int previousValue = 0; Button buttons[] = new Button[16]; TextField txtResult = new TextField(10); 15 https://www.facebook.com/Oxus20
  • 16. public void init() { setLayout(new BorderLayout()); add(txtResult, "North"); txtResult.setText("0"); Panel p = new Panel(); p.setLayout(new GridLayout(4, 4)); for (int i = 0; i < 16; i++) { if (i < 10) { buttons[i] = new Button(String.valueOf(i)); } else { buttons[i] = new Button(operators[i % 10]); } buttons[i].setFont(new Font("Verdana", Font.BOLD, 18)); p.add(buttons[i]); add(p, "Center"); buttons[i].addActionListener(this); } } 16 https://www.facebook.com/Oxus20
  • 17. public void actionPerformed(ActionEvent ae) { int result = 0; String caption = ae.getActionCommand(); int currentValue = Integer.parseInt(txtResult.getText()); if (caption.equals("C")) { txtResult.setText("0"); previousValue = 0; currentValue = 0; result = 0; operator = ""; } else if (caption.equals("=")) { result = 0; if (operator == "+") result = previousValue + currentValue; else if (operator == "-") result = previousValue - currentValue; else if (operator == "*") result = previousValue * currentValue; else if (operator == "/") result = previousValue / currentValue; txtResult.setText(String.valueOf(result)); } 17 https://www.facebook.com/Oxus20
  • 18. End - Simple Calculator Example else if (caption.equals("+") || caption.equals("-") || caption.equals("*") || caption.equals("/")) { previousValue = currentValue; operator = caption; txtResult.setText("0"); } else { int value = currentValue * 10 + Integer.parseInt(caption); txtResult.setText(String.valueOf(value)); } } } 18 https://www.facebook.com/Oxus20
  • 19. OUTPUT - Simple Calculator Example 19 https://www.facebook.com/Oxus20
  • 20. Example of Graphics and Applet 20 https://www.facebook.com/Oxus20
  • 21. Analog Clock Example 21 https://www.facebook.com/Oxus20 import java.applet.Applet; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Calendar; public class AnalogClock extends Applet implements Runnable { private static final long serialVersionUID = 1L; private static final double TWO_PI = 2.0 * Math.PI; private Calendar nw = Calendar.getInstance(); int width = 200, hight = 200; int xcent = width / 2, ycent = hight / 2; int minhand, maxhand; double rdns; int dxmin, dymin, dxmax, dymax; double radins, sine, cosine; double fminutes; Thread t = null; Boolean stopFlag;
  • 22. 22 https://www.facebook.com/Oxus20 public void start() { t = new Thread(this); stopFlag = false; t.start(); } public void run() { for (;;) { try { updateTime(); repaint(); Thread.sleep(1000); if (stopFlag) break; } catch (InterruptedException e) { } } } public void stop() { stopFlag = true; t = null; } private void updateTime() { nw.setTimeInMillis(System.currentTimeMillis()); }
  • 23. 23 https://www.facebook.com/Oxus20 public void paint(Graphics g) { g.setFont(new Font("Gabriola", Font.BOLD + Font.ITALIC, 160)); g.setColor(Color.RED); g.drawString("XUS", 300, 270); g.setFont(new Font("Consolas", Font.BOLD + Font.ITALIC, 100)); g.setColor(Color.GREEN); g.drawString("20", 550, 270); g.setColor(Color.black); g.fillOval(100, 100, 200, 200); Graphics2D g1 = (Graphics2D) g; int hours = nw.get(Calendar.HOUR); int minutes = nw.get(Calendar.MINUTE); int seconds = nw.get(Calendar.SECOND); int millis = nw.get(Calendar.MILLISECOND); minhand = width / 8; maxhand = width / 2; rdns = (seconds + ((double) millis / 1000)) / 60.0; drw(g1, rdns, 0, maxhand - 20); g1.setColor(Color.BLUE); g1.drawString( String.format("%02d : %02d :%02d ", hours, minutes, seconds), minhand + 150, maxhand + 170);
  • 24. 24 https://www.facebook.com/Oxus20 minhand = 0; // Minute hand starts in middle. maxhand = width / 3; fminutes = (minutes + rdns) / 60.0; drw(g1, fminutes, 0, maxhand); minhand = 0; // Minute hand starts in middle. maxhand = width / 4; drw(g1, (hours + fminutes) / 12.0, 0, maxhand); g1.setColor(Color.gray); // set b ackground of circle g1.drawOval(100, 100, 200, 200); // draw a circle g1.setColor(Color.WHITE); g1.setFont(new Font("Consulas", Font.BOLD + Font.ITALIC, 15)); g1.drawString("12", 190, 120); g1.drawString("6", 195, 290); g1.drawString("3", 280, 200); g1.drawString("6", 110, 200); g1.setFont(new Font("Consulas", Font.BOLD + Font.ITALIC, 15)); g1.setStroke(new BasicStroke(2, BasicStroke.JOIN_MITER, BasicStroke.JOIN_BEVEL)); }
  • 25. End - Analog Clock Example public void drw(Graphics2D g, double prct, int minRadius, int maxRadius) { radins = (0.5 - prct) * TWO_PI; sine = Math.sin(radins); cosine = Math.cos(radins); dxmin = xcent + (int) (minRadius * sine); dymin = ycent + (int) (minRadius * cosine); dxmax = xcent + (int) (maxRadius * sine); dymax = ycent + (int) (maxRadius * cosine); g.setColor(Color.WHITE); g.setBackground(Color.cyan); g.setFont(new Font("Consulas", Font.BOLD + Font.ITALIC, 12)); g.drawLine(dxmin + 100, dymin + 100, dxmax + 100, dymax + 100); } } 25 https://www.facebook.com/Oxus20
  • 26. OUTPUT - Analog Clock Example 26 https://www.facebook.com/Oxus20