Introduction
In this article, we discuss graphics programming, using Applets in Java. Also, what Applets are and their advantages and disadvantages.
What is Applet?
First, we have a look at what Applets are.
A Java Applet is a small application written in Java and delivered to users in the form of bytecode. Java Applets can be written in any programming language that compiles to Java byte-code. An Applet is a special type of program that is embedded in the webpage to generate the dynamic content and that runs inside the browser and works at the client-side. The user launches the Java Applet from a web page. Java Applets were introduced in the first version of the Java language, in 1995.
Advantages of Applet
Java Applets have the following advantages:
- They are simple to make and they are supported by most web browsers.
- They are faster in nature.
- At the same time, the same Applet can work on "all" installed versions of Java.
- An Applet can be debugged and developed directly, by simply creating the main routine.
- Applets that are not trusted have no connection to the local machine, they can only access the server, from which it comes
- Makes a connection between server and client.
Disadvantages of Applet
Java Applets have following disadvantages:
- for properly executing Applets we require the Java plug-in.
- mobile browsers running Android or Apple iOS, don't run Java Applets.
- In client-side scripting, achieving the desired goal is impossible for security reasons.
- Some Applets require a specific JRE. This is discouraged.
- If an Applet requires a newer JRE than available on the system or a specific JRE, then the user running it for the first time will need to wait for the large JRE download to complete.
Graphics in Applets
The java.awt.Graphics class provides many methods for graphics programming.
Some commonly used public methods in the Graphics class:
- abstract void setClor(Color clr) sets the Graphics current color to the specified color.
- abstract void drawString(String strng, int a, int b) draws the specified string.
- void drawRect(int a, int b, int wdth, int hgt) draws a rectangle with the specified width and height.
- abstract void fillRect(int a, int b, int wdth, int hgt) fills a rectangle with the default color and specified width and height.
- abstract void drawOval(int a, int b, int wdth, int hgt) draws an oval.
- abstract void fillOval(int a, int b, int wdth, int hgt) fills an oval with the default color.
- abstract boolean drawImage(Image img, int a, int b, int hgt, ImageObserver obsrvr) draws the specified image.
- abstract void drawArc(int a, int b, int wdth, int hgt, int startAngle, intarcAngle) draws a circular or elliptical arc.
- abstract void fillarc(int a, int b, int wdth, int hgt, int startAngle, int arcAngle) fills a circular or elliptical arc.
- abstract void drawLine(int a1, int b1, int a2, int b2) draws a line.
- abstract void setFont(Font fnt) sets the current graphics font to the specified font.
Draw a simple string in Java
In this program, we show how to design a string in graphics programming.
- import java.awt.*;
- import java.applet.Applet;
- public class GrpStringEx extends Applet {
- public void paint(Graphics grp)
- {
- grp.setColor(Color.blue);
- grp.drawString("welcome-to-appletworld-in-java", 150, 150);
- }
- }
-
-
-
Output
To execute our code by the appletviewer tool, write in the command prompt:
javac GrpStringEx.java
appletviewer GrpStringEx.java
Draw a rectangle in Java
In this example, we draw a simple rectangle.
- import java.awt.*;
- import java.applet.Applet;
- public class GrpRectEx extends Applet
- {
- public void paint(Graphics grp)
- {
- grp.setColor(Color.blue);
- grp.drawRect(100, 50, 150, 150);
- }
- }
-
-
-
-
Output
Fill color in a rectangle in Java
In this example, we fill in the color of our rectangle, usnig the grp.fillRect method.
- import java.awt.*;
- import java.applet.Applet;
- public class GrpRectEx extends Applet
- {
- public void paint(Graphics grp)
- {
- grp.setColor(Color.blue);
- grp.drawRect(100, 50, 150, 150);
- grp.fillRect(100, 50, 150, 150);
- }
- }
-
-
-
-
Output
Draw an oval and an arc in Java
In this example, we create an oval and an arc with specified co-ordinates.
- import java.awt.*;
- import java.applet.Applet;
- public class GrpOvalEx extends Applet
- {
- public void paint(Graphics grp)
- {
- grp.setColor(Color.blue);
- grp.drawOval(100, 50, 150, 150);
- grp.drawArc(100, 150, 150, 150, 0, 180);
- }
- }
-
-
-
-
Output
Fill color in oval and arc in Java
In this example, we fill an oval and an arc with a color, using the fillOval and fillArc methods.
- import java.awt.*;
- import java.applet.Applet;
- public class GrpOvalEx extends Applet
- {
- public void paint(Graphics grp)
- {
- grp.setColor(Color.blue);
- grp.fillOval(100, 50, 150, 150);
- grp.setColor(Color.red);
- grp.fillArc(100, 150, 150, 150, 0, 180);
- }
- }
-
-
-
-
Output
Draw a line in Java
In this example, we draw a simple line with specified co-ordinates.
- import java.awt.*;
- import java.applet.Applet;
- public class GrpLineEx extends Applet
- {
- public void paint(Graphics grp)
- {
- grp.setColor(Color.blue);
- grp.drawLine(50, 30, 400, 30);
- }
- }
-
-
-
-
Output