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

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago