java.awt.Graphics class in Applet has methods for working with graphics.
Method | Description |
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. |
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World!", 50, 25);
}
}
<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>
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)
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);
}
}
<html>
<body>
<applet code="ImageGrpcsEx.class" width="300" height="300">
</applet>
</body>
</html>
For this purpose, the image is required to be moved.
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){}
}
}
}
<html>
<body>
<applet code="ImageGrpcsEx.class" width="300" height="300">
</applet>
</body>
</html>
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");
}
}
<html>
<body>
<applet code="EventApplet.class" width="300" height="300">
</applet>
</body>
</html>
Note: also read about the Java Applet
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…