Graphics Programming Using Applet In Java

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:
  1. They are simple to make and they are supported by most web browsers.
  2. They are faster in nature.
  3. At the same time, the same Applet can work on "all" installed versions of Java.
  4. An Applet can be debugged and developed directly, by simply creating the main routine.
  5. Applets that are not trusted have no connection to the local machine, they can only access the server, from which it comes
  6. Makes a connection between server and client.

Disadvantages of Applet

 
Java Applets have following disadvantages:
  1. for properly executing Applets we require the Java plug-in.
  2. mobile browsers running Android or Apple iOS, don't run Java Applets.
  3. In client-side scripting, achieving the desired goal is impossible for security reasons.
  4. Some Applets require a specific JRE.  This is discouraged.
  5. 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:
  1. abstract void setClor(Color clr) sets the Graphics current color to the specified color.
  2. abstract void drawString(String strng, int a, int b) draws the specified string.
  3. void drawRect(int a, int b, int wdth, int hgt) draws a rectangle with the specified width and height.
  4. abstract void fillRect(int a, int b, int wdth, int hgt) fills a rectangle with the default color and specified width and height.
  5. abstract void drawOval(int a, int b, int wdth, int hgt) draws an oval.
  6. abstract void fillOval(int a, int b, int wdth, int hgt) fills an oval with the default color.
  7. abstract boolean drawImage(Image img, int a, int b, int hgt, ImageObserver obsrvr) draws the specified image.
  8. abstract void drawArc(int a, int b, int wdth, int hgt, int startAngle, intarcAngle) draws a circular or elliptical arc.
  9. abstract void fillarc(int a, int b, int wdth, int hgt, int startAngle, int arcAngle) fills a circular or elliptical arc.
  10. abstract void  drawLine(int a1, int b1, int a2, int b2) draws a line.
  11. 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.
  1. import java.awt.*;  
  2. import java.applet.Applet;  
  3. public class GrpStringEx extends Applet {  
  4.  public void paint(Graphics grp)  
  5.  {  
  6.   grp.setColor(Color.blue);  
  7.   grp.drawString("welcome-to-appletworld-in-java"150150);  
  8.  }  
  9. }  
  10. /* 
  11. <applet code="GrpStringEx.class" width="400" height="400"> 
  12. </applet>
  13. */  
Output
 
To execute our code by the appletviewer tool, write in the command prompt:
 
javac GrpStringEx.java
appletviewer GrpStringEx.java
 
fig-1.jpg
 

Draw a rectangle in Java

 
In this example, we draw a simple rectangle.
  1. import java.awt.*;  
  2. import java.applet.Applet;  
  3. public class GrpRectEx extends Applet  
  4. {  
  5.  public void paint(Graphics grp)  
  6.  {  
  7.   grp.setColor(Color.blue);  
  8.   grp.drawRect(10050150150);  
  9.  }  
  10. }  
  11. /* 
  12. <applet code="GrpRectEx.class" width="400" height="400"> 
  13. </applet> 
  14. */  
Output
 
fig-2.jpg
 

Fill color in a rectangle in Java

 
In this example, we fill in the color of our rectangle, usnig the grp.fillRect method.
  1. import java.awt.*;  
  2. import java.applet.Applet;  
  3. public class GrpRectEx extends Applet  
  4. {  
  5.  public void paint(Graphics grp)  
  6.  {  
  7.   grp.setColor(Color.blue);  
  8.   grp.drawRect(10050150150);  
  9.   grp.fillRect(10050150150);  
  10.  }  
  11. }  
  12. /* 
  13. <applet code="GrpRectEx.class" width="400" height="400"> 
  14. </applet> 
  15. */  
Output
 
FIG-3.jpg
 

Draw an oval and an arc in Java

 
In this example, we create an oval and an arc with specified co-ordinates.
  1. import java.awt.*;  
  2. import java.applet.Applet;  
  3. public class GrpOvalEx extends Applet  
  4. {  
  5.  public void paint(Graphics grp)  
  6.  {  
  7.   grp.setColor(Color.blue);  
  8.   grp.drawOval(10050150150);  
  9.   grp.drawArc(1001501501500180);  
  10.  }  
  11. }  
  12. /* 
  13. <applet code="GrpOvalEx.class" width="400" height="400"> 
  14. </applet> 
  15. */  
Output
 
fig-4.jpg
 

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.
  1. import java.awt.*;  
  2. import java.applet.Applet;  
  3. public class GrpOvalEx extends Applet  
  4. {  
  5.  public void paint(Graphics grp)  
  6.  {  
  7.   grp.setColor(Color.blue);  
  8.   grp.fillOval(10050150150);  
  9.   grp.setColor(Color.red);  
  10.   grp.fillArc(1001501501500180);  
  11.  }  
  12. }  
  13. /* 
  14. <applet code="GrpOvalEx.class" width="400" height="400"> 
  15. </applet> 
  16. */  
Output
 
fig-5.jpg
 

Draw a line in Java

 
In this example, we draw a simple line with specified co-ordinates.
  1. import java.awt.*;  
  2. import java.applet.Applet;  
  3. public class GrpLineEx extends Applet  
  4. {  
  5.  public void paint(Graphics grp)  
  6.  {  
  7.   grp.setColor(Color.blue);  
  8.   grp.drawLine(503040030);  
  9.  }  
  10. }  
  11. /* 
  12. <applet code="GrpLineEx.class" width="400" height="400"> 
  13. </applet> 
  14. */  
Output
 
fig-6.jpg