Need explanation about Adapter Class in JAVA
import java.awt.*; import java.applet.*; import java.awt.event.*; public class AdapterDemo extends Applet { public void init() { addMouseListener(new MyMouseAdapter(this)); addMouseMotionListener(new MyMouseMotionAdapter(this)); } } class MyMouseAdapter extends MouseAdapter { AdapterDemo adapterDemo; public MyMouseAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo; } public void mouseClicked(MouseEvent me) { adapterDemo.showStatus("Mouse Clicked"); }} class MyMouseMotionAdapter extends MouseMotionAdapter { AdapterDemo adapterDemo; public MyMouseMotionAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo; } public void mouseDragged(MouseEvent me) { adapterDemo.showStatus("Mouse Dragged"); } }
import java.awt.*; import java.awt.event.*; import java.applet*; class SampleFrame extends Frame { SampleFrame(String title) { super(title); MyWindowAdapter adapter = new MyWindowAdapter(this); addWindowListener(adapter); } public void paint(Graphics g) { g.drawString("This is in frame window", 10, 40); } } class MyWindowAdapter extends WindowAdapter { SampleFrame sampleFrame; public MyWindowAdapter(SampleFrame sampleFrame) { this.sampleFrame = sample Frame; } public void windowClosing(WindowEvent we) { sampleFrame.setVisible(false); } } public class AppletFrame extends Applet { Frame f; public void init() { f = new SampleFrame("A Frame Window"); f.setSize(250, 250); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false);} public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); } }
Actually, I have to create a new Frame window. So, the code in my book is given with using Adapter Class. As I don't know adapter class, I didn't understand it
Hey Lyrae. Do you know this?
The basic concept of an adapter is to provide a (empty) class that implements one (or more) interface(s). You then extend that class and override methods to get partial functionality from the interface(s). So as an example, in the case of ``` class MyWindowAdapter extends WindowAdapter ``` WindowAdapter is a class described by java doc as "An abstract adapter class for receiving window events. ...". Normaly if you wanted MyWindowAdapter to be a class for 'receiving window events' you'd have to implement WindowListener, WindowStateListener, and WindowFocusListener. Implementing those interfaces means that you have to define all their methods in MyWindowAdapter, which is quite verbose since you only need some of them. To avoid this the creators of java created a class WindowAdapter which already implements all those methods. This means that all you have to is extend that class and override the methods you are interested in. Adapters have another purpose as well, and that is to act as bridges between similar classes. Say we have a temperature class where everything is in Celcius. We also pretend that the get and set methods actually are very long and complicated. ``` class Temperature { int temp = 0; // In celcius public int getTemp() { return temp } public void setTemp(int pTemp) {temp = pTemp} } ``` We have an interface for fahrenheit temperature ``` interface Fahrenheit { public int getF(); public void setF(int f); } ``` We could make a new fahrenheit class but that'd probably mean we'd also have to make those long and complicated get and set methods again. Instead we save some time and create a fahrenheit adapter which handles the bridge between the two classes instead. ``` class FahrenheitAdapter extends Temperature implements Fahrenheit { public int getF() { return getTemp() * 9/5 + 30; // Convert celcuis to 2) + 30; } public void setF(int f) { setTemp((f - 32) * 5/9); } } ```
@e.mccormick Could you explain this block of code in the first program: AdapterDemo adapterDemo; public MyMouseAdapter(AdapterDemo adapterDemo) { this.adapterDemo = adapterDemo; }
The mouse adapter is an abstract class that is used to get mouse events. It has empty methods so that you can more easily create listerners. Here is what the Java docs say about this: http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html And here is an example implementation: http://www.java2s.com/Code/Java/Event/extendsMouseAdapterandimplementsMouseMotionListenertocreateamousedrawingprogramm.htm The snippet you picked is part of larger thing that is intended to do the same sort of thing as that.
Okay
Join our real-time social learning platform and learn together with your friends!