Graphics in Applet in Java
In Java applets, the Graphics class plays a central role in creating and displaying graphical content. It is part of the java.awt package and provides methods for drawing shapes, text, and images on the applet’s canvas.
Overview of the Graphics Class
The Graphics class is an abstract base class that provides a platform-independent way to draw graphical components. In an applet, the paint(Graphics g) method is automatically called by the applet runtime whenever the applet needs to be rendered.
Key Features of the Graphics Class
- Drawing Shapes:
- Provides methods to draw basic shapes like lines, rectangles, ovals, and polygons.
- Rendering Text:
- Includes methods to display text in various styles and fonts.
- Customizing Appearance:
- Allows setting colors and fonts for drawing.
- Image Display:
- Provides methods to display images in the applet window.
- Clipping:
- Defines regions where drawing operations are allowed.
Important Methods in the Graphics Class
Method | Description |
drawLine(int x1, int y1, int x2, int y2) | Draws a line between the points (x1, y1) and (x2, y2). |
drawRect(int x, int y, int width, int height) | Draws the outline of a rectangle. |
fillRect(int x, int y, int width, int height) | Draws a filled rectangle. |
drawOval(int x, int y, int width, int height) | Draws the outline of an oval. |
fillOval(int x, int y, int width, int height) | Draws a filled oval. |
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) | Draws the outline of an arc. |
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) | Draws a filled arc. |
drawPolygon(int[] xPoints, int[] yPoints, int nPoints) | Draws a polygon defined by arrays of x and y coordinates. |
fillPolygon(int[] xPoints, int[] yPoints, int nPoints) | Draws a filled polygon. |
drawString(String str, int x, int y) | Draws the specified string at the specified coordinates. |
setColor(Color c) | Sets the current drawing color. |
setFont(Font font) | Sets the current font. |
Example: Drawing Shapes in an Applet
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class GraphicsApplet extends Applet {
@Override
public void paint(Graphics g) {
// Set drawing color
g.setColor(Color.BLUE);
// Draw a line
g.drawLine(20, 30, 200, 30);
// Draw a rectangle
g.drawRect(50, 50, 100, 50);
// Draw a filled rectangle
g.setColor(Color.RED);
g.fillRect(50, 120, 100, 50);
// Draw an oval
g.setColor(Color.GREEN);
g.drawOval(200, 50, 100, 50);
// Draw a filled oval
g.setColor(Color.ORANGE);
g.fillOval(200, 120, 100, 50);
// Draw a string
g.setColor(Color.BLACK);
g.drawString(“Graphics in Applet”, 100, 200);
}
}
Example: Drawing a Polygon
import java.applet.Applet;
import java.awt.Graphics;
public class PolygonApplet extends Applet {
@Override
public void paint(Graphics g) {
// Coordinates of the polygon
int[] xPoints = {50, 100, 150, 100};
int[] yPoints = {100, 50, 100, 150};
// Draw a polygon
g.drawPolygon(xPoints, yPoints, xPoints.length);
// Draw a filled polygon
g.fillPolygon(xPoints, yPoints, xPoints.length);
}
}
Rendering Text in Applets
The drawString(String str, int x, int y) method is used to display text in the applet. The x and y parameters define the baseline position of the string.
Example:
@Override
public void paint(Graphics g) {
g.setColor(Color.MAGENTA);
g.drawString(“Hello, Applet Graphics!”, 50, 50);
}
Customizing Graphics
- Changing Colors: Use the setColor(Color c) method to change the current drawing color.
g.setColor(Color.RED);
g.drawRect(50, 50, 100, 100);
- Changing Fonts: Use the setFont(Font f) method to change the font for rendering text.
g.setFont(new Font(“Serif”, Font.BOLD, 16));
g.drawString(“Custom Font”, 50, 100);
Integrating Images
The drawImage(Image img, int x, int y, ImageObserver observer) method is used to render images in an applet. Images can be loaded using the getImage() method.
Example:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class ImageApplet extends Applet {
private Image img;
@Override
public void init() {
img = getImage(getCodeBase(), “example.jpg”); // Load the image
}
@Override
public void paint(Graphics g) {
g.drawImage(img, 50, 50, this);
}
}
Advantages of Using Graphics in Applets
- Versatility:
- Provides a wide range of drawing options like shapes, text, and images.
- Custom Rendering:
- Enables developers to create highly customized user interfaces.
- Interactivity:
- Supports dynamic visual updates in response to user actions.
Limitations of Graphics in Applets
- Obsolescence:
- Applets are deprecated and no longer supported by modern browsers.
- Performance:
- Rendering complex graphics can be less efficient compared to modern frameworks like JavaFX or OpenGL.
- Platform Dependency:
- Graphics rendering can behave slightly differently across platforms.
Conclusion
The Graphics class in Java is a powerful tool for rendering shapes, text, and images in applets. While applets and the Graphics class have become less common in modern applications, they remain a significant part of Java’s legacy and are still useful for understanding basic concepts of graphical programming in Java.