Sponsored sites

Wednesday 17 September 2014

Java Awt Quick Guide 3

Methods inherited

This class inherits methods from the following classes:
  • java.awt.Component
  • java.lang.Object

Choice Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showChoiceDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showChoiceDemo(){                                       

      headerLabel.setText("Control in action: Choice"); 
      final Choice fruitChoice = new Choice();

      fruitChoice.add("Apple");
      fruitChoice.add("Grapes");
      fruitChoice.add("Mango");
      fruitChoice.add("Peer");

      Button showButton = new Button("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            String data = "Fruit Selected: " 
            + fruitChoice.getItem(fruitChoice.getSelectedIndex());
            statusLabel.setText(data);
         }
      }); 

      controlPanel.add(fruitChoice);
      controlPanel.add(showButton);

      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output
AWT Choice

AWT Canvas Class

Introduction

Canvas control represents a rectangular area where application can draw something or can receive inputs created by user.

Class declaration

Following is the declaration for java.awt.Canvas class:
public class Canvas
   extends Component
      implements Accessible

Class constructors

S.N.Constructor & Description
1Canvas()
Constructs a new Canvas.
2Canvas(GraphicsConfiguration config)
Constructs a new Canvas given a GraphicsConfiguration object.

Class methods

S.N.Method & Description
1void addNotify()
Creates the peer of the canvas.
2void createBufferStrategy(int numBuffers)
Creates a new strategy for multi-buffering on this component.
3void createBufferStrategy(int numBuffers, BufferCapabilities caps)
Creates a new strategy for multi-buffering on this component with the required buffer capabilities.
4AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Canvas.
5BufferStrategy getBufferStrategy()
Returns the BufferStrategy used by this component.
6void paint(Graphics g)
Paints this canvas.
7void pdate(Graphics g)
Updates this canvas.

Methods inherited

This class inherits methods from the following classes:
  • java.awt.Component
  • java.lang.Object

Canvas Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showCanvasDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showCanvasDemo(){
      headerLabel.setText("Control in action: Canvas"); 

      controlPanel.add(new MyCanvas());
      mainFrame.setVisible(true);  
   } 

   class MyCanvas extends Canvas {

      public MyCanvas () {
         setBackground (Color.GRAY);
         setSize(300, 300);
      }

      public void paint (Graphics g) {
         Graphics2D g2;
         g2 = (Graphics2D) g;
         g2.drawString ("It is a custom canvas area", 70, 70);
      }
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output
AWT Canvas

AWT Image Class

Introduction

Image control is superclass for all image classes representing graphical images.

Class declaration

Following is the declaration for java.awt.Image class:
public abstract class Image
   extends Object

Field

Following are the fields for java.awt.Image class:
  • protected float accelerationPriority -- Priority for accelerating this image.
  • static int SCALE_AREA_AVERAGING -- Use the Area Averaging image scaling algorithm.
  • static int SCALE_DEFAULT -- Use the default image-scaling algorithm.
  • static int SCALE_FAST -- Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image.
  • static int SCALE_REPLICATE -- Use the image scaling algorithm embodied in the ReplicateScaleFilter class.
  • static int SCALE_SMOOTH -- Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.
  • static Object UndefinedProperty -- The UndefinedProperty object should be returned whenever a property which was not defined for a particular image is fetched.

Class constructors

S.N.Constructor & Description
1Image()

Class methods

S.N.Method & Description
1void flush()
Flushes all reconstructable resources being used by this Image object.
2float getAccelerationPriority()
Returns the current value of the acceleration priority hint.
3ImageCapabilities getCapabilities(GraphicsConfiguration gc)
Returns an ImageCapabilities object which can be inquired as to the capabilities of this Image on the specified GraphicsConfiguration.
4abstract Graphics getGraphics()
Creates a graphics context for drawing to an off-screen image.
5abstract int getHeight(ImageObserver observer)
Determines the height of the image.
6abstract Object getProperty(String name, ImageObserver observer)
Gets a property of this image by name.
7Image getScaledInstance(int width, int height, int hints)
Creates a scaled version of this image.
8abstract ImageProducer getSource()
Gets the object that produces the pixels for the image.
9abstract int getWidth(ImageObserver observer)
Determines the width of the image.
10void setAccelerationPriority(float priority)
Sets a hint for this image about how important acceleration is.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

Image Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showImageDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showImageDemo(){
      headerLabel.setText("Control in action: Image"); 

      controlPanel.add(new ImageComponent("resources/java.jpg"));
      mainFrame.setVisible(true);  
   }
	
   class ImageComponent extends Component {

      BufferedImage img;

      public void paint(Graphics g) {
         g.drawImage(img, 0, 0, null);
      }

      public ImageComponent(String path) {
         try {
            img = ImageIO.read(new File(path));
         } catch (IOException e) {
            e.printStackTrace();
         }
      }

      public Dimension getPreferredSize() {
         if (img == null) {
            return new Dimension(100,100);
         } else {
            return new Dimension(img.getWidth(), img.getHeight());
         }
      }
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output
AWT Image

AWT Scrollbar Class

Introduction

Scrollbar control represents a scroll bar component in order to enable user to select from range of values.

Class declaration

Following is the declaration for java.awt.Scrollbar class:
public class Scrollbar
   extends Component
      implements Adjustable, Accessible

Field

Following are the fields for java.awt.Image class:
  • static int HORIZONTAL --A constant that indicates a horizontal scroll bar.
  • static int VERTICAL --A constant that indicates a vertical scroll bar.

Class constructors

S.N.Constructor & Description
1Scrollbar()
Constructs a new vertical scroll bar.
2Scrollbar(int orientation)
Constructs a new scroll bar with the specified orientation.
3Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values.

Class methods

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Scrollbar. AdjustmentListener[] getAdjustmentListeners()
Returns an array of all the adjustment listeners registered on this scrollbar. <T extends EventListener>T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this Scrollbar.
S.N.Method & Description
1void addAdjustmentListener(AdjustmentListener l)
Adds the specified adjustment listener to receive instances of AdjustmentEvent from this scroll bar.
2void addNotify()
Creates the Scrollbar's peer.
3int getBlockIncrement()
Gets the block increment of this scroll bar.
4int getLineIncrement()
Deprecated. As of JDK version 1.1, replaced by getUnitIncrement().
5int getMaximum()
Gets the maximum value of this scroll bar.
6int getMinimum()
Gets the minimum value of this scroll bar.
7int getOrientation()
Returns the orientation of this scroll bar.
8int getPageIncrement()
Deprecated. As of JDK version 1.1, replaced by getBlockIncrement().
9int getUnitIncrement()
Gets the unit increment for this scrollbar.
10int getValue()
Gets the current value of this scroll bar.
11boolean
getValueIsAdjusting()
Returns true if the value is in the process of changing as a result of actions being taken by the user.
12int getVisible()
Deprecated. As of JDK version 1.1, replaced by getVisibleAmount().
13int getVisibleAmount()
Gets the visible amount of this scroll bar.
14protected String paramString()
Returns a string representing the state of this Scrollbar.
15protected void processAdjustmentEvent(AdjustmentEvent e)
Processes adjustment events occurring on this scrollbar by dispatching them to any registered AdjustmentListener objects.
16protected
1void processEvent(AWTEvent e)
Processes events on this scroll bar.
17void removeAdjustmentListener(AdjustmentListener l)
Removes the specified adjustment listener so that it no longer receives instances of AdjustmentEvent from this scroll bar.
18void setBlockIncrement(int v)
Sets the block increment for this scroll bar.
19void setLineIncrement(int v)
Deprecated. As of JDK version 1.1, replaced by setUnitIncrement(int).
20void setMaximum(int newMaximum)
Sets the maximum value of this scroll bar.
21void setMinimum(int newMinimum)
Sets the minimum value of this scroll bar.
22void setOrientation(int orientation)
Sets the orientation for this scroll bar.
23void setPageIncrement(int v)
Deprecated. As of JDK version 1.1, replaced by setBlockIncrement().
24void setUnitIncrement(int v)
Sets the unit increment for this scroll bar.
25void setValue(int newValue)
Sets the value of this scroll bar to the specified value.
26void setValueIsAdjusting(boolean b)
Sets the valueIsAdjusting property.
27void setValues(int value, int visible, int minimum, int maximum)
Sets the values of four properties for this scroll bar: value, visibleAmount, minimum, and maximum.
28void setVisibleAmount(int newAmount)
Sets the visible amount of this scroll bar.

Methods inherited

This class inherits methods from the following classes:
  • java.awt.Component
  • java.lang.Object

Scrollbar Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showScrollbarDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showScrollbarDemo(){                                       
      headerLabel.setText("Control in action: Scrollbar"); 

      final Scrollbar horizontalScroller = new Scrollbar(Scrollbar.HORIZONTAL);
      final Scrollbar verticalScroller = new Scrollbar();
      verticalScroller.setOrientation(Scrollbar.VERTICAL);
      horizontalScroller.setMaximum (100);
      horizontalScroller.setMinimum (1);
      verticalScroller.setMaximum (100);
      verticalScroller.setMinimum (1);

      horizontalScroller.addAdjustmentListener(new AdjustmentListener() {

         @Override
         public void adjustmentValueChanged(AdjustmentEvent e) {
            statusLabel.setText("Horozontal: "
               +horizontalScroller.getValue() 
               +" ,Vertical: "
               + verticalScroller.getValue());
            }
         });

      verticalScroller.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
               statusLabel.setText("Horozontal: "
               +horizontalScroller.getValue() 
               +" ,Vertical: "+ verticalScroller.getValue());
            }
         });

      controlPanel.add(horizontalScroller);
      controlPanel.add(verticalScroller);

      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output
AWT Scrollbar

AWT Dialog Class

Introduction

Dialog control represents a top-level window with a title and a border used to take some form of input from the user.

Class declaration

Following is the declaration for java.awt.Dialog class:
public class Dialog
extends Window

Field

Following are the fields for java.awt.Image class:
  • static Dialog.ModalityType DEFAULT_MODALITY_TYPE -- Default modality type for modal dialogs.

Class constructors

S.N.Constructor & Description
1Dialog(Dialog owner)
Constructs an initially invisible, modeless Dialog with the specified owner Dialog and an empty title.
2Dialog(Dialog owner, String title)
Constructs an initially invisible, modeless Dialog with the specified owner Dialog and title.
3Dialog(Dialog owner, String title, boolean modal)
Constructs an initially invisible Dialog with the specified owner Dialog, title, and modality.
4Dialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc)
Constructs an initially invisible Dialog with the specified owner Dialog, title, modality and GraphicsConfiguration.
5Dialog(Frame owner)
Constructs an initially invisible, modeless Dialog with the specified owner Frame and an empty title.
6Dialog(Frame owner, boolean modal)
Constructs an initially invisible Dialog with the specified owner Frame and modality and an empty title.
7Dialog(Frame owner, String title)
Constructs an initially invisible, modeless Dialog with the specified owner Frame and title.
8Dialog(Frame owner, String title, boolean modal)
Constructs an initially invisible Dialog with the specified owner Frame, title and modality.
9Dialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc)
Constructs an initially invisible Dialog with the specified owner Frame, title, modality, and GraphicsConfiguration.
10Dialog(Window owner)
Constructs an initially invisible, modeless Dialog with the specified owner Window and an empty title.
11Dialog(Window owner, Dialog.ModalityType modalityType)
Constructs an initially invisible Dialog with the specified owner Window and modality and an empty title.
12Dialog(Window owner, String title)
Constructs an initially invisible, modeless Dialog with the specified owner Window and title.
13Dialog(Window owner, String title, Dialog.ModalityType modalityType)
Constructs an initially invisible Dialog with the specified owner Window, title and modality.
14Dialog(Window owner, String title, Dialog.ModalityType modalityType, GraphicsConfiguration gc)
Constructs an initially invisible Dialog with the specified owner Window, title, modality and GraphicsConfiguration

No comments:

Post a Comment