Home Java Example Java Awt Adapters Example



Adapters Example
Posted on: December 18, 2008 at 12:00 AM
In the following program code, the adapter class has been used. This class has been used as an anonymous inner class to draw a rectangle within an applet.

Adapters Example

     

In the following program code, the adapter class has been used. This class has been used as an anonymous inner class to draw a rectangle within an applet. This example demonstrates the functionality of the mouse press. That is on every click of the mouse from top left corner, we get a rectangle on the release of the bottom right.

The following program demonstrates the functionality of adapter class.

 

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

public class AdapterDemo extends Applet{
  public void init(){
  addMouseListener(
  new MouseAdapter(){
  int topX, bottomY;
  public void mousePressed(MouseEvent me){
  topX = me.getX();
  bottomY = me.getY();
  }
  public void mouseReleased(MouseEvent me){
  Graphics g = AdapterDemo.this.getGraphics();
  g.drawRect(topX, bottomY, me.getX()-topX, me.getY()-bottomY);
  }
  });
  }
}

Output of the program:

C:\newprgrm>javac AdapterDemo.java

C:\newprgrm>appletviewer AdapterDemo.html

C:\newprgrm>

Download this example.

 

 

Related Tags for Adapters Example:
cclassappletsedapplethisappanonymouswithadaprogramtorectangleadapterdrawramrawangleeitaptuseinnoasmthinletinneradclproppsrecteeishallfollowcodcodewingssthappleplprodeonogrolonon


More Tutorials from this section

Ask Questions?    Discuss: Adapters Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.