Showing posts with label GUI Components. Show all posts
Showing posts with label GUI Components. Show all posts

Draw Panel - Java Sample Program

//  DrawPanel.java
//  Using drawLine to connect the corners of a panel.
import java.awt.Graphics; 
import javax.swing.JPanel;

public class DrawPanel extends JPanel
{
   // draws an X from the corners of the panel
   public void paintComponent( Graphics g )
   {
      // call paintComponent to ensure the panel displays correctly
      super.paintComponent( g );
      
      int width = getWidth(); // total width   
      int height = getHeight(); // total height

      // draw a line from the upper-left to the lower-right
      g.drawLine( 0, 0, width, height );
      
      // draw a line from the lower-left to the upper-right
      g.drawLine( 0, height, width, 0 );
   } // end method paintComponent
} // end class DrawPanel


// DrawPanelTest.java
// Application to display a DrawPanel.
import javax.swing.JFrame;

public class DrawPanelTest
{
   public static void main( String[] args )
   {
      // create a panel that contains our drawing
      DrawPanel panel = new DrawPanel();
      
      // create a new frame to hold the panel
      JFrame application = new JFrame();
      
      // set the frame to exit when it is closed
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      application.add( panel ); // add the panel to the frame      
      application.setSize( 250, 250 ); // set the size of the frame
      application.setVisible( true ); // make the frame visible    
   } // end main
} // end class DrawPanelTest


Name Dialog - Java Sample Program

// NameDialog.java
// Basic input with a dialog box.
import javax.swing.JOptionPane;

public class NameDialog
{
   public static void main( String[] args )
   {
      // prompt user to enter name
      String name =                                          
         JOptionPane.showInputDialog( "What is your name?" );
      
      // create the message
      String message =                                              
         String.format( "Welcome, %s, to Java Programming!", name );

      // display the message to welcome the user by name 
      JOptionPane.showMessageDialog( null, message );
   } // end main
} // end class NameDialog

Dialog Box - Java Sample Program

// Dialog1.java
// Using JOptionPane to display multiple lines in a dialog box.
import javax.swing.JOptionPane; // import class JOptionPane

public class Dialog1
{
   public static void main( String[] args )
   {
      // display a dialog with a message 
      JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" );
   } // end main
} // end class Dialog1

Media Tracker - Java Sample Program

//The following code snippet illustrates how to use a MediaTracker to wait while an image is prepared:
//file: StatusImage.java

import java.awt.*;
import javax.swing.*;

public class StatusImage extends JComponent
{
boolean loaded = false;
String message = "Loading...";
Image image;

public StatusImage( Image image ) { this.image = image; }

public void paint(Graphics g) {

if (loaded)
g.drawImage(image, 0, 0, this);
else {
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
g.drawString(message, 20, 20);
}
}

public void loaded() {
loaded = true;
repaint();
}

public void setMessage( String msg ) {
message = msg;
repaint();
}

public static void main( String [] args ) {
JFrame frame = new JFrame("TrackImage");
Image image = Toolkit.getDefaultToolkit().getImage( args[0] );
StatusImage statusImage = new StatusImage( image );

frame.add( statusImage );
frame.setSize(300,300);
frame.setVisible(true);

MediaTracker tracker = new MediaTracker( statusImage );

int MAIN_IMAGE = 0;

tracker.addImage( image, MAIN_IMAGE );

try {
tracker.waitForID( MAIN_IMAGE ); }
catch (InterruptedException e) {}
if ( tracker.isErrorID( MAIN_IMAGE ) )
statusImage.setMessage( "Error" );
else
statusImage.loaded();
}
}

Perimeter and Area of a Rectangle - Java Sample Program

//Given the length and width of a rectangle, this Java program determines its area and perimeter.
//Filename: RectangleProgram.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RectangleProgram extends JFrame
{
private JLabel lengthL, widthL, areaL, perimeterL;
private JTextField lengthTF, widthTF, areaTF, perimeterTF;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

private static final int WIDTH = 400;
private static final int HEIGHT = 300;

public RectangleProgram()
{
//Create the four labels
lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
widthL = new JLabel("Enter the width: ",SwingConstants.RIGHT);
areaL = new JLabel("Area: ", SwingConstants.RIGHT);
perimeterL = new JLabel("Perimeter: ", SwingConstants.RIGHT);

//Create the four text fields
lengthTF = new JTextField(10);
widthTF = new JTextField(10);
areaTF = new JTextField(10);
perimeterTF = new JTextField(10);

//Create Calculate Button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);

//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);

//Set the title of the window
setTitle("Area and Perimeter of a Rectangle");

//Get the container
Container pane = getContentPane();

//Set the layout
pane.setLayout(new GridLayout(5, 2));

//Place the components in the pane
pane.add(lengthL);
pane.add(lengthTF);
pane.add(widthL);
pane.add(widthTF);
pane.add(areaL);
pane.add(areaTF);
pane.add(perimeterL);
pane.add(perimeterTF);
pane.add(calculateB);
pane.add(exitB);

//Set the size of the window and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double width, length, area, perimeter;
length = Double.parseDouble(lengthTF.getText());
width = Double.parseDouble(widthTF.getText());
area = length * width;
perimeter = 2 * (length + width);
areaTF.setText("" + area);
perimeterTF.setText("" + perimeter);
}
}

private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

public static void main(String[] args)
{
RectangleProgram rectObject = new RectangleProgram();
}
}


***************Sample Output***************


GUI Rectangle 3 - Java Sample Program

//Java program to create a window and place four labels and four text fields
//Filename: RectangleProgramThree.java

import javax.swing.*;
import java.awt.*;
public class RectangleProgramThree extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private JLabel lengthL, widthL, areaL, perimeterL;
private JTextField lengthTF, widthTF, areaTF, perimeterTF;

public RectangleProgramThree()
{
setTitle("Area and Perimeter of a Rectangle");
lengthL =
new JLabel("Enter the length: ", SwingConstants.RIGHT);
widthL =
new JLabel("Enter the width: ", SwingConstants.RIGHT);
areaL =
new JLabel("Area: ", SwingConstants.RIGHT);
perimeterL =
new JLabel("Perimeter: ", SwingConstants.RIGHT);

lengthTF = new JTextField(10);
widthTF = new JTextField(10);
areaTF = new JTextField(10);
perimeterTF = new JTextField(10);
Container pane = getContentPane();

pane.setLayout(new GridLayout(4, 2));
pane.add(lengthL);
pane.add(lengthTF);
pane.add(widthL);
pane.add(widthTF);
pane.add(areaL);
pane.add(areaTF);
pane.add(perimeterL);
pane.add(perimeterTF);

setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args)
{
RectangleProgramThree rectObject = new RectangleProgramThree();
}
}

********Sample Output********

GUI Rectangle Window 2 - Java Sample Program

//Java program to create a window and place four labels
//Filename: RectangleProgramTwo.java

import javax.swing.*;
import java.awt.*;

public class RectangleProgramTwo extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private JLabel lengthL, widthL, areaL, perimeterL;
public RectangleProgramTwo()
{
setTitle("Area and Perimeter of a Rectangle");
lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
areaL = new JLabel("Area: ", SwingConstants.RIGHT);
perimeterL = new JLabel("Perimeter: ", SwingConstants.RIGHT);

Container pane = getContentPane();
pane.setLayout(new GridLayout(4, 1));
pane.add(lengthL);
pane.add(widthL);
pane.add(areaL);
pane.add(perimeterL);

setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args)
{
RectangleProgramTwo rectObject = new RectangleProgramTwo();
}
}

//Sample Output


GUI Rectangle Window 1 - Java Sample Program

//Java program to create a window.

import javax.swing.*;
public class RectangleProgramOne extends JFrame
{
private static final int WIDTH = 400;
private static final int HEIGHT = 300;

public RectangleProgramOne()
{
setTitle("Area and Perimeter of a Rectangle");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args)
{
RectangleProgramOne rectProg = new RectangleProgramOne();
}
}


Notice that the named constants WIDTH and HEIGHT are declared with the modifier
private. This is because we want these named constants to be used only within the class
RectangleProgram. In general, if a named constant, variable, or method is to be used only
within the specified class, then it is declared with the modifier private. Also, note that
private is a reserved word in Java.

GUI Rectangle - Java Sample Program

// This Java Program determines the area and
// perimeter of a rectangle.
import javax.swing.JOptionPane;
public class Rectangle
{
public static void main(String[] args)
{
double width, length, area, perimeter; //Line 1
String lengthStr, widthStr, outputStr; //Line 2

lengthStr =JOptionPane.showInputDialog("Enter the length: "); //Line 3
length = Double.parseDouble(lengthStr); //Line 4
widthStr =JOptionPane.showInputDialog("Enter the width: "); //Line 5
width = Double.parseDouble(widthStr); //Line 6
area = length * width; //Line 7
perimeter = 2 * (length + width); //Line 8

outputStr = "Length: " + length + "\n" +
"Width: " + width + "\n" +
"Area: " + area + " square units\n" +
"Perimeter: " + perimeter + " units\n"; //Line 9

JOptionPane.showMessageDialog(null, outputStr, "Rectangle",
JOptionPane.INFORMATION_MESSAGE); //Line 10

System.exit(0); //Line 11
}
}

//Sample Output


 The statements in Lines 1 and 2 declare various variables to manipulate the data. The statement in Line 3 displays the first dialog box of the sample run and prompts the user to enter the length of the rectangle.

 The entered length is assigned as a string to lengthStr. The statement in Line 4 retrieves the
length and stores it in the variable length.

The statement in Line 5 displays the second dialog box of the sample run and prompts the
user to enter the width of the rectangle. The entered width is assigned as a string to
widthStr. The statement in Line 6 retrieves the width and stores it in the variable width.

 The statement in Line 7 determines the area, and the statement in Line 8 determines the
perimeter of the rectangle. The statement in Line 9 creates the string containing the
desired output and assigns it to outputStr. The statement in Line 10 uses the output
dialog box to display the desired output, which is shown in the third dialog box of the

sample run. Finally, the statement in Line 11 terminates the program.