Consider the following program:
import java.awt.*; import java.applet.*; public class CircleSquare extends Applet { public void paint (Graphics g) { // Draw a red, filled circle: g.setColor (Color.red); g.fillOval (50,50,70,70); // Draw an unfilled blue square next to it: g.setColor (Color.blue); g.drawRect (140,50,80,70); } }Note:
import java.awt.*; import java.applet.*; public class CircleSquare2 extends Applet { public void paint (Graphics g) { // Mix red and blue: range is 0-255 g.setColor (new Color (255, 0, 255)); g.fillOval (50,50, 70,70); // Mix some red, with mostly green and blue g.setColor (new Color (100, 255, 255)); g.fillRect (140,50, 80,70); } }Link to applet
Consider the following program:
import java.awt.*; import java.applet.*; public class Face extends Applet { public void paint (Graphics g) { g.drawOval (40, 40, 120, 150); // Head. g.drawOval (57, 75, 30, 20); // Left eye. g.drawOval (110, 75, 30, 20); // Left eye. g.fillOval (68, 81, 10, 10); // Left pupil. g.fillOval (121, 81, 10, 10); // Right pupil. g.drawOval (85, 100, 30, 30); // Nose. g.fillArc (60, 125, 80, 40, 180, 180); // Mouth. g.drawOval (25, 92, 15, 30); // Left ear. g.drawOval (160, 92, 15, 30); // Right ear. } }Note:
Are there more methods for drawing? How does one find out?
Applets can be made more interesting by using the surrounding HTML to write stuff, include pictures and anything you can do with HTML.
For example, let's make a tiny modification to both the Face program and the Face HTML file:
import java.awt.*; import java.applet.*; public class Face2 extends Applet { public void paint (Graphics g) { g.drawOval (40, 40, 120, 150); // Head. g.drawOval (57, 75, 30, 20); // Left eye. g.drawOval (110, 75, 30, 20); // Left eye. g.fillOval (68, 81, 10, 10); // Left pupil. g.fillOval (121, 81, 10, 10); // Right pupil. g.drawOval (85, 100, 30, 30); // Nose. g.fillArc (60, 125, 80, 40, 180, 180); // Mouth. g.drawOval (25, 92, 15, 30); // Left ear. g.drawOval (160, 92, 15, 30); // Right ear. g.drawString ("Burrp!", 180, 165); // Draw a string. } }
<HTML> <title>Hello</title> <BODY> <applet code="Face2.class" width=300 height=200> </applet> <br> Oh excuse me, I just finished lunch and er .... well, how are you today? </body> </html>
You can also place multiple applets on the same webpage:
<HTML> <title>Hello</title> <BODY bgcolor="#FFFFFF"> First applet: <p> <applet code="Face.class" width=300 height=200> </applet> <p> Second applet: <p> <applet code="Face2.class" width=300 height=200> </applet> </body> </html>