The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

mouseClicked(MouseEvent e)

Posted by John Liu on September 24, 2000 at 2:26 AM

the counter num is increased twice as I put buttom only one time.

Should the counter be one as I click the mouse one time.
Why is it always increase twice each times?

Please help me soluted this problem. thanks
=============================================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.awt.Graphics;

public class imag extends Applet implements ActionListener,
MouseListener,MouseMotionListener{

Image image;

Button buttonCounter;
boolean bMouseDownFlag=false;
boolean bMouseUpFlag=false;
boolean bCounter=false;
boolean done=false;
private boolean pause=false;


Point pts[]=new Point[9999];
int num=0,ptindex=0,ptindexx=0;

TextField mousetext1;


public void init(){
image=getImage( getCodeBase(),"Hongkong.bmp");
addMouseListener(this);

buttonCounter=new Button("Counter");
add(buttonCounter);
buttonCounter.addActionListener(this);

mousetext1=new TextField(3);
add(mousetext1);

addMouseListener(this);
addMouseMotionListener(this);
}

public void start(){}

public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if(bCounter==true){
addNum( num);
pts[ptindex]=new Point(e.getX(),e.getY());
repaint();
ptindex++;
}
}

void addNum(int num) {
this.num=num+1;
String s=String.valueOf(this.num);
mousetext1.setText( s );
}

public void mouseEntered(MouseEvent e){};
public void mouseExited(MouseEvent e){};
public void mouseMoved(MouseEvent e){};

public void run(){
try{
while(!done){
if(!pause) repaint();}
}catch(Exception e){
System.out.println("*****finally ****");
}
}


public void paint(Graphics g){
int loop;

g.setColor(Color.red);
for(loop=0;loop g.fillRect(pts[loop].x,pts[loop].y,3,3);
}
}

public void update(Graphics g){
paint(g);
}


public void actionPerformed(ActionEvent e){
if(e.getSource()==buttonCounter){
bCounter=!bCounter;
}
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us