Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Awt Adapters Example

Related Tutorials


 
 

Share on Google+Share on Google+

Adapters Example

Advertisement
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.

 

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Related Tutorials

Discuss: Adapters Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
shini
July 5, 2011
Regarding log adapters

can i get sample examples for logging adapter classes
Pavan Gomladu
October 19, 2011
Introduce

Generally from this page i get information of that is the code generally create interaction between Hardware and Microprosessor as Adapter.
archie
April 21, 2012
question

Graphics g = AdapterDemo.this.getGraphics(); what does this do?
DMCA.com