|
Example |
Topic |
1 |
|
2 |
|
3 |
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
8 |
|
|
9 |
Example 1: Displaying multiple strings: Using paint ( ) method

import java.applet.*;
import java.awt.*;
public
class DrawingLines extends
Applet
{
int
width,
height;
public
void
init()
{
width
= getSize().width;
height
= getSize().height;
setBackground(
Color.black
);
}
public
void
paint( Graphics g )
{
g.setColor(
Color.green
);
for
( int
i = 0; i < 10; ++i )
{
g.drawLine(
width,
height,
i * width
/ 10, 0 );
}
}
}
Example 3: Calculations using init ( ) method
![]() |
![]() |
|||
![]() |
Example 4: Calling methods

Example 5: do while control structure
import java.awt.Graphics;
import javax.swing.JApplet;
public class DoWhileTest extends JApplet
{
// draw lines on applet
public void paint( Graphics g )
{
super.paint( g ); // call paint method inherited from JApplet
int counter = 1; // initialize counter
do
{
g.drawOval( 110 - counter * 10, 110 - counter * 10,
counter * 20, counter * 20 );
++counter;
} while ( counter <= 10 ); // end do...while
} // end method paint
} // end class DoWhileTest

Example 6: switch control structure
import java.awt.Graphics;
import javax.swing.*;
public class SwitchTest extends JApplet
{
int choice;
public void init()
{
String input;
input = JOptionPane.showInputDialog(
"Enter 1 to draw lines\n" +
"Enter 2 to draw rectangles\n" +
"Enter 3 to draw ovals\n" );
choice = Integer.parseInt( input );
}
public void paint( Graphics g )
{
for ( int i = 0; i < 10; i++ )
{
switch( choice ) {
case 1:
g.drawLine( 10, 10, 250, 10 + i * 10 );
break;
case 2:
g.drawRect( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break;
case 3:
g.drawOval( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break;
default:
JOptionPane.showMessageDialog(
null, "Invalid value entered" );
} // end switch
} // end for
} // end paint()
} // end class SwitchTest

// Finding the maximum of three floating-point numbers.
import java.awt.Container;
import javax.swing.*;
public class MaximumTest extends JApplet
{
// initialize applet by obtaining user input and creating GUI
public void init()
{
// obtain user input
String s1 = JOptionPane.showInputDialog("Enter first floating-point value" );
String s2 = JOptionPane.showInputDialog("Enter second floating-point value" );
String s3 = JOptionPane.showInputDialog("Enter third floating-point value" );
// convert user input to double values
double number1 = Double.parseDouble( s1 );
double number2 = Double.parseDouble( s2 );
double number3 = Double.parseDouble( s3 );
double max = maximum( number1, number2, number3 ); // method call
// create JTextArea to display results
JTextArea outputArea = new JTextArea();
// display numbers and maximum value
outputArea.setText( "number1: " + number1 + "\nnumber2: " +
number2 + "\nnumber3: " + number3 + "\nmaximum is: " + max );
// get applet's GUI component display area
Container container = getContentPane();
// attach outputArea to Container c
container.add( outputArea );
} // end method init
// maximum method uses Math class method max to help
// determine maximum value
public double maximum( double x, double y, double z )
{
return Math.max( x, Math.max( y, z ) );
} // end method maximum
} // end class Maximum
