awt event 1.1 mousePressed method problem

T

Thanasis \(sch\)

Hi to group,
I got a trouble with the awt applet below.
The FIRST TIME i left click and hold the mouse down (pressed) on Canvas, i
don't get the message "Pressed" as i would expect. However from the second
time and on i do get the message
Why does this happen?(the code is below)

regards
Thanasis.
================================================================================

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

public class DoodlerNewEvent 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
Graphics g =getGraphics();
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();
Graphics g =getGraphics();
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 // single click
{
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.)
Graphics g =getGraphics();
int x= e.getX();
int y= e.getY();
g.drawString("Clicked",x,y);

}
//=============================================
public void mouseDragged( MouseEvent e ) {
// called during motion with buttons down
Graphics g = getGraphics();
int x = e.getX();
int y = e.getY();
showStatus(e.toString());

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();
}
//=============================================
}// end applet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,538
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top