java.awt.Graphics class provides many methods for graphics programming.
Let's see a program:
import java.applet.Applet; import java.awt.*; public class GraphicsExample extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); g.drawArc(90,150,30,30,30,270); g.fillArc(270,150,30,30,0,180); } }
<html> <body> <applet code="GraphicsExample.class" width="350" height="250"> </applet> </body> </html>
Applet is mostly used in games and animation. For this purpose image is required to be displayed. The java.awt.Graphics class provide a method drawImage() to display the image.
The general form of drawImage() is
public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): // is used draw the specified image.
The java.applet.Applet class provides getImage() method that returns the object of Image.
public Image getImage(URL u, String image){}
import java.awt.*; import java.applet.*; public class DisplayImageExa extends Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),"tapuuu.jpg"); } public void paint(Graphics g) { g.drawImage(picture, 30,30, this); } }
In the above example, drawImage() method of Graphics class is used to display the image. The Component class implements ImageObserver interface. So current class object would also be treated as ImageObserver because Applet class indirectly extends the Component class.
<html> <body> <applet code="DisplayImage.class" width="300" height="300"> </applet> </body> </html>
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc