Categories: Java

Graphics in Applet

java.awt.Graphics class in Applet has methods for working with graphics.

Methods of the Graphics class:
MethodDescription
public abstract void drawString(String str, int x, int y)Draws the provided string.
public void drawRect(int x, int y, int width, int height)Draws a rectangle with the provided width and height.
public abstract void fillRect(int x, int y, int width, int height)Used to draw a rectangle with the provided width and height and default color.
public abstract void drawOval(int x, int y, int width, int height)Used to create an oval with a specified width and height.
public abstract void fillOval(int x, int y, int width, int height)Used to draw oval with a default color of specified width and height.
public abstract void drawLine(int x1, int y1, int x2, int y2)Used to draw lines connecting the points (x1, x1) and (x2, y2).
public abstract booleandrawImage(Image img, int x, int y, ImageObserver observer)Used to create a specific image.
public abstract void drawArc(int x, int y, int width, int height, intstartAngle, intarcAngle)Used to create a circular arc.
public abstract void fillArc(int x, int y, int width, int height, intstartAngle, intarcAngle)Used for filling circular arc.
public abstract void setColor(Color c)Used to set a color to the object.
public abstract void setFont(Font font)Used to set font.
HelloWorld.class
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello World!", 50, 25);
    }
}
Hello.html
<html>

<head>
    <TITLE> A Simple Program </TITLE>
</head>

<body>
    Here is the output of my program:
    <applet code="HelloWorld.class" width="150" height="25"></applet>
</body>

</html>
Displaying Image in Applet:

The java.awt.Graphics class provides a method, drawImage() to display the image.

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)
Example: ImageGrpcsEx.class
import java.awt.*;  
import java.applet.*;  
  
  
public class DisplayImage extends Applet {  
  
  Image picture;  
  
  public void init() {  
    picture = getImage(getDocumentBase(),"xyz.jpg");  
  }  
    
  public void paint(Graphics g) {  
    g.drawImage(picture, 100, 30, this);  
  }  
      
  }  
Display.html
<html>  
<body>  
<applet code="ImageGrpcsEx.class" width="300" height="300">  
</applet>  
</body>  
</html>  
Output:
Animation in Applet:

For this purpose, the image is required to be moved.

Example: ImageGrpcsEx.class
import java.awt.*;  
import java.applet.*;  
  
  
public class DisplayImage extends Applet {  
  
  Image picture;  
  
  public void init() {  
    picture = getImage(getDocumentBase(),"xyz.jpg");  
  }  
    
  public void paint(Graphics g) {  
      
  for(int i=0;i<500;i++){  
      g.drawImage(picture, i,30, this);  
  
      try{Thread.sleep(100);}catch(Exception e){}  
    }  
  }  
      
  }  
Display.html
<html>  
<body>  
<applet code="ImageGrpcsEx.class" width="300" height="300">  
</applet>  
</body>  
</html>
EventHandling in Applet:

As we perform event handling in AWT or Swing, we can perform it in applet also.

Let’s look at a simple example of event handling in an applet that publishes a message when a button is clicked.

import java.applet.*;  
import java.awt.*;  
import java.awt.event.*;  
public class EventApplet extends Applet implements ActionListener{  
Button b;  
TextField tf;  
  
public void init(){  
tf=new TextField();  
tf.setBounds(30,40,150,20);  
  
b=new Button("Click");  
b.setBounds(80,150,60,50);  
  
add(b);add(tf);  
b.addActionListener(this);  
  
setLayout(null);  
}  
  
 public void actionPerformed(ActionEvent e){  
  tf.setText("Welcome");  
 }   
}  
Display.html:
<html>  
<body>  
<applet code="EventApplet.class" width="300" height="300">  
</applet>  
</body>  
</html>  

Note: also read about the Java Applet

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago