Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Examples

 

 

Example

Topic

1

2

3

4

5

6

  7

Finding Maximum of Numbers

8

9

 

 

Example 1: Displaying multiple strings: Using paint ( ) method

 

import javax.swing.JApplet;  // import class JApplet
import java.awt.Graphics;     // import class Graphics
 
public class WelcomeApplet2 extends JApplet
   public void paint( Graphics g )
   {
      g.drawString( "Welcome to", 25, 25 );
      g.drawString( "Java Programming!", 25, 40 );
   }
}
 

 

Example 2: Drawing Lines

 

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 );

      }

   }

}

What is the output?

 

Example 3: Calculations using init ( ) method

 
import java.awt.Graphics;  
 
import javax.swing.*;      
 
public class AdditionApplet extends JApplet
{
   double sum; 
 
   // 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 );
      number2 = Double.parseDouble( secondNumber );
 
      // add numbers
      sum = number1 + number2;
   }
 
   // draw results in a rectangle on applet’s background
   public void paint( Graphics g )
   {
      // call inherited 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 );
 
      // draw results as a String at (25, 25)
      g.drawString( "The sum is " + sum, 25, 25 );
 
   }  // end method paint
 
}  // end class AdditionApplet

 

 

     
         
     
 

 

Example 4: Calling methods

 
import java.awt.Container;
 
import javax.swing.*;
 
public class SquareIntegers extends JApplet
{
 
   // set up GUI and calculate squares of integers from 1 to 10
   public void init()
   {
      // JTextArea to display results
      JTextArea outputArea = new JTextArea();
 
      // get applet's content pane (GUI component display area)
      Container container = getContentPane();
 
      // attach outputArea to container
      container.add( outputArea );
 
      int result; 
      String output = ""; 
 
      // loop 10 times
      for ( int counter = 1; counter <= 10; counter++ )
      {
 
         // calculate square of counter and store in result
         result = square( counter );
 
         // append result to String output
         output += "The square of " + counter + " is " + result + "\n";
 
      } 
 
      outputArea.setText( output );  // place results in JTextArea
 
   } 
 
   // square method definition
   public int square( int y )
   {
      return y * y;  
   } 
 

 

 

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

 

 Example 7

// 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