This presentation introduces Java Applet and Java Graphics in detail with examples and finally using the concept of both applet and graphics code the analog clock project to depict how to use them in real life challenges and applications.
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