Results 1 to 2 of 2

Thread: Java applet -> Adapter class...

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Java applet -> Adapter class...

    Ok

    A bit further into the book now, and it's shown how to do mouse and keyboard functions. Then cut them down more to using adapters. As its challenge, it says try putting in the keyboard listeners using an adapter.

    Ok...

    hmm

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    /*
    <applet code="AdapterMnK.class" width=200 height=60>
    </applet>
    */
    
    public class AdapterMnK extends Applet {
    
    	public String msg = "";
    	public int msgX = 0, msgY = 10;
    
    	public void init() {
    		addMouseListener(new MyMouseAdapter(this));
    		addMouseMotionListener(new MyMouseMotionAdapter(this));
    		addKeyListener(new MyKeyListenerAdapter(this));
    	}
    
    
    	public void paint(Graphics g) {
    		g.drawString(msg, msgX, msgY);
    	}
    }
    
    class MyMouseAdapter extends MouseAdapter {
    	AdapterMnK adapterDemo;
    
    	public MyMouseAdapter(AdapterMnK adapterDemo) {
    		this.adapterDemo = adapterDemo;
    	}
    	
    	public void mouseClicked(MouseEvent me) {
    		adapterDemo.showStatus("Mouse Clicked");
    	}
    }
    
    class MyMouseMotionAdapter extends MouseMotionAdapter {
    	AdapterMnK adapterDemo;
    
    	public MyMouseMotionAdapter(AdapterMnK adapterDemo) {
    		this.adapterDemo = adapterDemo;
    	}
    
    	public void mouseDragged(MouseEvent me) {
    		adapterDemo.showStatus("Mouse dragged");
    	}
    }
    
    class MyKeyListenerAdapter extends KeyAdapter {
    	AdapterMnK adapterDemo;
    
    	public MyKeyListenerAdapter(AdapterMnK adapterdemo) {
    		this.adapterDemo = adapterdemo;
    	}
    
    	public void KeyTyped(KeyEvent ke) {
    		msg += ke.getKeyChar();
    		repaint();
    	}
    }
    Now to explain a bit. the book showed the mouse coding (which worked).
    I attempted to add the keyboard bit (which doesn't work).

    It doesn't like the repaint. So I'm thinking that I've put it in the wrong place and it cannot see the paint/repaint to run it.

    I seem to be missing a bit... somewhere

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Java applet -> Adapter class...

    Ok I have changed and managed to get it to compile... And run... But only the Mouse events are working...

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    /*
    <applet code="AdapterMnK.class" width=200 height=60>
    </applet>
    */
    
    public class AdapterMnK extends Applet {
    
    	public String msg = "";
    	public int msgX = 0, msgY = 10;
    
    	public void init() {
    		addMouseListener(new MyMouseAdapter(this));
    		addMouseMotionListener(new MyMouseMotionAdapter(this));
    		addKeyListener(new MyKeyListenerAdapter(this));
    	}
    
    
    	public void paint(Graphics g) {
    		g.drawString(msg, msgX, msgY);
    	}
    }
    
    class MyMouseAdapter extends MouseAdapter {
    	AdapterMnK adapterDemo;
    
    	public MyMouseAdapter(AdapterMnK adapterDemo) {
    		this.adapterDemo = adapterDemo;
    	}
    	
    	public void mouseClicked(MouseEvent me) {
    		adapterDemo.showStatus("Mouse Clicked");
    	}
    }
    
    class MyMouseMotionAdapter extends MouseMotionAdapter {
    	AdapterMnK adapterDemo;
    
    	public MyMouseMotionAdapter(AdapterMnK adapterDemo) {
    		this.adapterDemo = adapterDemo;
    	}
    
    	public void mouseDragged(MouseEvent me) {
    		adapterDemo.showStatus("Mouse dragged");
    	}
    }
    
    class MyKeyListenerAdapter extends KeyAdapter {
    	AdapterMnK adapterDemo;
    
    	public MyKeyListenerAdapter(AdapterMnK adapterdemo) {
    		this.adapterDemo = adapterDemo;
    	}
    
    	public void KeyTyped(KeyEvent ke) {
    		adapterDemo.showStatus("Key Typed");
    		adapterDemo.msg += ke.getKeyChar();
    		adapterDemo.repaint();
    	}
    }

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width