Displaying Graphics in Applet

To display graphics in an applet, you can use the Java Graphics class. Here is an example code snippet that demonstrates how to draw a rectangle in an applet:

import java.applet.*;
import java.awt.*;

public class MyGraphicApplet extends Applet {
    public void paint(Graphics g) {
        g.drawRect(50, 50, 100, 50);
    }
}

In this example, we have created a class called MyGraphicApplet that extends the Applet class. We have overridden the paint() method of the Applet class to draw a rectangle using the drawRect() method of the Graphics class.

The drawRect() method takes four arguments: the x-coordinate and y-coordinate of the upper-left corner of the rectangle, followed by the width and height of the rectangle.

You can run this applet using a web browser by embedding it in an HTML page. Here is an example HTML code snippet:

<html>
  <head>
    <title>My Graphic Applet</title>
  </head>
  <body>
    <applet code="MyGraphicApplet.class" width="200" height="100">
      Your browser does not support the applet tag.
    </applet>
  </body>
</html>

In this example, we have created an HTML page that embeds the applet using the <applet> tag. We have specified the name of the applet class (MyGraphicApplet.class) using the code attribute, and we have set the width and height of the applet using the width and height attributes.

When you load this HTML page in a web browser, you should see a rectangle drawn in the applet area.

Commonly used methods of Graphics class:

The Graphics class in Java provides a wide range of methods for drawing and manipulating graphical elements on a component or canvas. Here are some commonly used methods of the Graphics class:

  1. drawLine(int x1, int y1, int x2, int y2): Draws a line between two points.
  2. drawRect(int x, int y, int width, int height): Draws a rectangle with the given dimensions.
  3. fillRect(int x, int y, int width, int height): Fills a rectangle with the given dimensions.
  4. drawOval(int x, int y, int width, int height): Draws an oval with the given dimensions.
  5. fillOval(int x, int y, int width, int height): Fills an oval with the given dimensions.
  6. drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): Draws an arc of an oval with the given dimensions, starting at the specified angle and spanning the specified arc angle.
  7. fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): Fills an arc of an oval with the given dimensions, starting at the specified angle and spanning the specified arc angle.
  8. drawPolygon(int[] xPoints, int[] yPoints, int nPoints): Draws a polygon defined by the specified arrays of x and y coordinates.
  9. fillPolygon(int[] xPoints, int[] yPoints, int nPoints): Fills a polygon defined by the specified arrays of x and y coordinates.
  10. drawString(String str, int x, int y): Draws the specified string at the specified location.
  11. setColor(Color c): Sets the current color for drawing.
  12. setFont(Font font): Sets the current font for drawing.

These are just a few of the many methods available in the Graphics class. By combining these methods with other Java classes like Color and Font, you can create complex and visually appealing graphical elements in your Java applications.

Example of Graphics in applet:

Sure, here is an example code of how to use the Graphics class in an applet to draw a smiley face:

import java.applet.*;
import java.awt.*;

public class SmileyApplet extends Applet {
    public void paint(Graphics g) {
        // Set the background color
        setBackground(Color.WHITE);
        
        // Draw the face
        g.setColor(Color.YELLOW);
        g.fillOval(100, 100, 200, 200);
        
        // Draw the eyes
        g.setColor(Color.BLACK);
        g.fillOval(150, 150, 25, 25);
        g.fillOval(225, 150, 25, 25);
        
        // Draw the mouth
        g.setColor(Color.BLACK);
        g.drawArc(150, 175, 100, 50, 180, 180);
    }
}

In this example, we have created a class called SmileyApplet that extends the Applet class. We have overridden the paint() method of the Applet class to draw a smiley face using various methods of the Graphics class.

The setBackground() method of the Applet class is used to set the background color of the applet.

We use the setColor() method to set the current color for drawing, and fillOval() method to draw the yellow circle representing the face. Then, we use the setColor() and fillOval() methods again to draw the black circles representing the eyes.

Finally, we use the setColor() and drawArc() methods to draw the arc representing the mouth. We pass in the appropriate parameters to draw an arc that looks like a smile.

You can run this applet using a web browser by embedding it in an HTML page. Here is an example HTML code snippet:

<html>
  <head>
    <title>Smiley Applet</title>
  </head>
  <body>
    <applet code="SmileyApplet.class" width="400" height="400">
      Your browser does not support the applet tag.
    </applet>
  </body>
</html>

In this example, we have created an HTML page that embeds the applet using the <applet> tag. We have specified the name of the applet class (SmileyApplet.class) using the code attribute, and we have set the width and height of the applet using the width and height attributes.

When you load this HTML page in a web browser, you should see a smiley face drawn in the applet area.

myapplet.html:

Sure, here is an example HTML code for embedding an applet named MyApplet in a web page:

<!DOCTYPE html>
<html>
  <head>
    <title>My Applet</title>
  </head>
  <body>
    <h1>My Applet</h1>
    <hr>
    <applet code="MyApplet.class" archive="MyApplet.jar" width="400" height="400">
      <param name="message" value="Hello, World!">
      Your browser does not support the applet tag.
    </applet>
    <hr>
    <p>Thank you for using my applet!</p>
  </body>
</html>

In this example, we have created an HTML page that contains an applet named MyApplet. The applet is embedded in the page using the <applet> tag, and we have specified the name of the applet class (MyApplet.class) using the code attribute.

We have also specified the name of the JAR file containing the applet using the archive attribute. The JAR file should be located in the same directory as the HTML file.

We have set the width and height of the applet using the width and height attributes. We have also included a <param> tag to pass a parameter named message to the applet. The value of the parameter is set to "Hello, World!".

Finally, we have included some text to be displayed before and after the applet.

You can save this code as a file named myapplet.html and open it in a web browser to see the applet in action. Make sure that the applet class and JAR file are located in the same directory as the HTML file.