Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 19th, 2004, 09:33 AM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default awt event model help

Hello to everyone,
the applet below is a basic applet to let me understand the awt event model 1.1.
My question is when i lect click in the applet window(and i hold down the mouse pressed) for the FIRST TIME i don't see the message "Pressed" as i would expect to see.
Why does this happen?

Any help would be appreciated
wishes
thanasis
================================================== ==
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class DoodlerNew extends Applet
  implements MouseListener, MouseMotionListener
{
    Point lineStart = new Point(0,0);
    int circleSize = 16;
    Graphics g = getGraphics();

    public void init()
    {
        setBackground(Color.cyan);
        addMouseListener( this );
        addMouseMotionListener( this );

    }

    public void paint(Graphics g)
    {
        g.setColor(Color.blue);
        g.drawString("Left button draws...",1,10);
        g.drawString("Right button or shift-left button erases...",1,20);
        g.drawString("Double click clears screen...",1,30);
    }

/*==========NEW EVENT MODEL =================*/
public void mouseEntered( MouseEvent e ) {
      // called when the pointer enters the applet's rectangular area
      showStatus("Hello....");
   }
//=============================================
   public void mouseExited( MouseEvent e ) {
      // called when the pointer leaves the applet's rectangular area
      showStatus("goodbye....");
   }


//=============================================
   public void mouseReleased( MouseEvent e ) { // called after a button is released
          showStatus(e.toString());
        // e.consume();
         g.drawString("Released",e.getX(),e.getY());
   }
//=============================================
   public void mouseMoved( MouseEvent e ) {
   // called during motion when no buttons are down

     // e.consume();
   }
  //=============================================
   public void mousePressed( MouseEvent e ) {
    // called after a button is pressed down

      // "Consume" the event so it won't be processed in the
      // default manner by the source which generated it.
     // e.consume();
      int x= e.getX();
      int y= e.getY();
      showStatus(e.toString());
        if ( e.isMetaDown() || e.isShiftDown()) // right button
            setForeground(getBackground());
        else // left button
            setForeground(Color.blue);

        if ( e.getClickCount() == 2 ) // double click
        {
            setForeground(getBackground());
            repaint();
        }
        else
        { lineStart.move(x,y);
            g.drawString("Pressed", x, y);

        }

   }
  //=============================================
   public void mouseClicked( MouseEvent e ) {
      // called after a press and release of a mouse button
      // with no motion in between
      // (If the user presses, drags, and then releases, there will be
      // no click event generated.)
       int x= e.getX();
      int y= e.getY();
      g.drawString("Clicked",x,y);

   }
//=============================================
   public void mouseDragged( MouseEvent e ) {
    // called during motion with buttons down
     int x = e.getX();
     int y = e.getY();
     showStatus(e.toString());
        g = getGraphics();
        if ( e.isMetaDown() || e.isShiftDown()) //right button - erase
        { // we'll erase with an oval to cut a wider swath
            g.fillOval(x - (circleSize / 2),
                 y - (circleSize / 2 ),
                circleSize, circleSize );
        }
        else
            g.drawLine(lineStart.x, lineStart.y,e.getX(), e.getY());

        lineStart.move(x,y);


    // e.consume();
   }
//=============================================
}


 
Old October 20th, 2004, 07:48 AM
Authorized User
 
Join Date: Jul 2004
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I understand applets right, when your applet first loads it does not have 'focus'. The first mouse click you make gives focus to the applet panel, thereafter redirecting mouse events to the applet handler rather than the browser page handler.

You may find a workaround if you can somehow redirect the mouse handler events from the browser DOM to the applet handler. I do not think there is a solution from within the applet. Most people just manage by ensuring that their users have to click on the applet at least once.
 
Old October 20th, 2004, 08:10 AM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks my friend






Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception AWT-EventQueue-0 native method Floetic Java GUI 1 April 21st, 2008 02:21 AM
java.awt.event error in swing app. Mr. Monkey Java GUI 2 March 27th, 2007 09:48 AM
.NET model vs. pre .NET model. raychoudhury BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 0 January 11th, 2007 06:11 AM
AWT / Swings dharmesh_g Java GUI 7 June 30th, 2005 05:11 PM
Images in Frame AWT Window cyrusds_asp Java GUI 1 November 2nd, 2003 09:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.