Java Applet
// A first applet in Java.
import java.awt.Graphics;
import javax.swing.JApplet;
public class WelcomeApplet extends JApplet {
// draw text on applet background
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 100, 100 );
} // end method paint
} // end class WelcomeApplet
// Displaying multiple strings in an applet.
// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.JApplet; // import class JApplet
public class WelcomeApplet2 extends JApplet {
// draw text on applet background
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw two Strings at different locations
g.drawString( "Welcome to", 25, 25 );
g.drawString( "Java Programming!", 25, 40 );
} // end method paint
} // end class WelcomeApplet2
// Displaying text and lines.
// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.JApplet; // import class JApplet
public class WelcomeLines extends JApplet {
// draw lines and a string on applet background
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw horizontal line from (15, 10) to (210, 10)
g.drawLine( 15, 10, 210, 10 );
//使用Graphics类的drawLine方法,指示g引用的Graphics对象绘制直线。
//drawLine方法需要4个参数,分别表示第一个端点和第二个端点的X坐标、Y坐标。
//所有的坐标值都相对于该Applet的左上角坐标(0,0),drawLine方法在两个端点之间画一条直线。
// draw horizontal line from (15, 30) to (210, 30)
g.drawLine( 15, 30, 210, 30 );
// draw String between lines at location (25, 25)
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
} // end class WelcomeLines
// Adding two floating-point numbers.
// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.*; // import package javax.swing
public class AdditionApplet extends JApplet {
double sum; // sum of values entered by user
// initialize applet by obtaining values from user
public void init()
{
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
double number1; // first number to add
double number2; // second number to add
// obtain first number from user
firstNumber = JOptionPane.showInputDialog("Enter first floating-point value" );
// obtain second number from user
secondNumber = JOptionPane.showInputDialog("Enter second floating-point value" );
// convert numbers from type String to type double
number1 = Double.parseDouble( firstNumber ); //String double 字符串转化成double型的值
number2 = Double.parseDouble( secondNumber );
// add numbers
sum = number1 + number2;
} // end method init
// draw results in a rectangle on applet抯 background
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw rectangle starting from (15, 10) that is 270
// pixels wide and 20 pixels tall
g.drawRect( 15, 10, 270, 20 );
//g.drawRect( 1, 2, 3, 4 );1.2.矩形左上角的横坐标和纵坐标。3.4.非负整数,表示矩形的宽度和高度。
// draw results as a String at (25, 25)
g.drawString( "The sum is " + sum, 25, 25 );
} // end method paint
} // end class AdditionApplet