JAVA Displaying Graphics in Applet

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);
}
}

AppletDemo1.html

<html>
<body>
<applet code="GraphicsExample.class" width="350" height="250">
</applet>
</body>
</html>

Displaying Image in Applet

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.

syntax

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.

AppletDemo2.html

<html>
<body>
<applet code="DisplayImage.class" width="300" height="300">
</applet>
</body>
</html>

 

 

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment