General
Passing Parameter
- Demonstrate the passing of parameters in Applet File.
HTML File
<html>
<head>
<title>Passing Parameter to Applet</title>
</head>
<body>
<applet code="ParamDemo.java" height = "300" width = "300">
<param name="fontName" value="Comic Sans MS">
<param name="fontSize" value="40">
<param name="leadingfontName" value="14.80">
<param name="active" value="true">
</applet>
</body>
</html>
Java File
import java.applet.*;
import java.awt.*;
// Following HTML chunk will pass parameter to Applet. This is comment for Java Compiler but not for appletviewer, so altering the attribute of parameter never compell to recompile again.
/*/
<applet code = "ParamDemo" height = "500" width = "500">
<param name = "fontName" value = "Comic Sans MS">
<param name = "fontSize" value = "40">
<param name = "leading" value = "14.80">
<param name = "active" value = "true">
</applet>
*/
public class ParamDemo extends Applet {
String fontName;
String param;
int fontSize;
float leading;
boolean active;
public void init() {
// Param is not used here because to get the String value and again to pass to String, no temp variable (param) is needed.
fontName = getParameter("fontName");
if (fontName == null) {
fontName = "Not Specified";
}
// Param is used as temporary String variable which retrieves the parameter passed.
param = getParameter("fontSize");
try {
if (param != null) {
fontSize = Integer.parseInt(param); // Converts the number in String to Integer data type.
} else {
fontSize = 0;
}
} catch (NumberFormatException exc) {
fontSize = 0;
}
param = getParameter("leading");
try {
if (param != null) {
leading = Float.valueOf(param).floatValue(); // Converts the number in String to Float data type.
} else {
leading = 0f;
}
} catch (NumberFormatException exc) {
leading = 0f;
}
param = getParameter("active");
if (param != null) {
active = Boolean.valueOf(param).booleanValue(); // Converts the boolean value in String to Boolean data type.
}
}
public void paint(Graphics graphicsObject) {
graphicsObject.drawString("Font Name : " + fontName, 100, 50);
graphicsObject.drawString("Font Size : " + fontName, 100, 60);
graphicsObject.drawString("Leading : " + leading, 100, 70);
graphicsObject.drawString("Active : " + active, 100, 80);
}
}
Display Available Fonts
- Display the available fonts in the system.
/* This shows the available fonts in Host Computer. */ /* <applet code = "AvailableFonts" height ="300" width = "300" ></applet> */ import java.awt.*; import java.applet.*; public class AvailableFonts extends Applet { public void paint(Graphics g) { String message = ""; // Initialize string to empty first. String fontLists[]; /* I don't get the below line. */ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); fontLists = ge.getAvailableFontFamilyNames(); for (String fontList : fontLists) { message += fontList + "\n "; g.drawString(message, 10, 20); } } }
Custom Font and Size
- Demonstrating applet with custom font text and size.
import java.awt.*; import java.applet.*; // This HTML chunk is for passing parameter to appletviewer. /* <applet code = "Introduction" height = "200" width = "500"> <param name = "fontName" value = "Tahoma"> <param name = "fontSize" value = "16"> <param name = "title" value = "Introduction"> <param name = "message" value = " >> I read in Gandaki College of Information and Science."> </applet> */ public class CustomFont extends Applet{ String param; String fontName; String title; String message; int fontSize; // We are defining our custom font. So, font object is created. Font myFont; public void init() { fontName = getParameter("fontName"); if(fontName == null) { fontName = "TimesRoman"; } title = getParameter("title"); if(title == null) { title = "Untitled"; } message = getParameter("message"); if(message == null) { message = "Write your description here."; } param = getParameter("fontSize"); try{ if(param != null) { fontSize = Integer.parseInt(param); } else { fontSize = 10; // parameter pass garyo tara integer parse vayena vane } }catch(NumberFormatException exc) { fontSize = 15; // Parameter nai pass garena vane chahi yo run huncha } } public void paint(Graphics g) { // Font object Syntax : Font(font_name, Font.font_type, font_size); myFont = new Font(fontName,Font.BOLD,fontSize); g.setFont(myFont); // Setting the defined font for this object. setBackground(Color.red); // Setting background color. setForeground(Color.white); // Setting foreground color. g.drawString(title,20,50); myFont = new Font(fontName,Font.PLAIN,fontSize); // Defining new font-style for displaying message. g.setFont(myFont); // Setting the new font. g.drawString(message,20,80); } }
Draw Nepali Flag
- Draw the Nepali Flag using the Applet and graphics.
import java.applet.*; import java.awt.*; /* <applet code = "Flag" width = "500" height = "500"></applet> */ public class Flag extends Applet { static Color NEPALIFLAG = new Color(221, 12, 39); // Defines custom color public void paint(Graphics flag) { // Figure starts from (210,10) // For upper traingle int upperTriangleX[] = { 210, 360, 210 }; // +150 for upper triangle int upperTriangleY[] = { 10, 160, 160 }; // g.drawPolygon(upperTriangleX,upperTriangleY,3); // Also can be drawn like // this. Polygon upperTriangle = new Polygon(upperTriangleX, upperTriangleY, 3); flag.drawPolygon(upperTriangle); flag.setColor(NEPALIFLAG); // Setting color of our brave Gorkha Soldiers Blood flag.fillPolygon(upperTriangle); // For lower traingle int lowerTriangleX[] = { 210, 390, 210 }; // +180 for lower triangle int lowerTriangleY[] = { 160, 340, 340 }; Polygon lowerTriangle = new Polygon(lowerTriangleX, lowerTriangleY, 3); flag.drawPolygon(lowerTriangle); flag.setColor(NEPALIFLAG); flag.fillPolygon(lowerTriangle); // For standing rod int rodX[] = { 207, 210, 210, 207 }; int rodY[] = { 7, 7, 500, 498 }; Polygon rod = new Polygon(rodX, rodY, 4); flag.drawPolygon(rod); flag.setColor(Color.black); flag.fillPolygon(rod); // For borders int borderX[] = { 210, 210, 360, 210, 390, 210, 210, 396, 216, 366 }; // 3 units int borderY[] = { 7, 10, 160, 160, 340, 340, 343, 343, 163, 163 }; Polygon border = new Polygon(borderX, borderY, 10); flag.drawPolygon(border); flag.setColor(Color.blue); flag.fillPolygon(border); // For moon // One Oval overlaps other Oval to form a Crescent moon; I was unable to learn // drawArc() at this moment. flag.setColor(Color.white); flag.fillOval(240, 100, 30, 30); // Synatx : fillOval(int xPosition, int yPosition, width, height) flag.setColor(NEPALIFLAG); flag.fillOval(240, 91, 30, 30); // For star int starX[] = { 240, 250, 255, 260, 270, 265, 270, 260, 255, 250, 240, 245 }; int starY[] = { 260, 260, 250, 260, 260, 270, 280, 280, 290, 280, 280, 270 }; // Starting is 240 Polygon star = new Polygon(starX, starY, 12); flag.drawPolygon(star); flag.setColor(Color.white); flag.fillPolygon(star); } }