-
why does mouseClicked get calle more than once
Hey when I click, the mouseClicked evt handles it but why does it say that I click more then once. For example I output the word "test" in the mousClicked method, and I only click once. Instead of it outputing the word "test" once it outputs it 2+ times, why?
They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.
-
Could you post your code?
-
public void mouseClicked(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
Graphics screen = getGraphics();
screen.setColor(Color.gray);
screen.fillRect(325,60,80,20);
if(x<35||x>315||y<35||y>315)
{
}
else
moved(x/35,y/35);
}
public void moved(int x,int y)
{
Graphics screen = getGraphics();
if(player==1)
{
if(board[y][x]==-1)
screen.drawString("Not yours",325,75);
else
{
if(board[y][x]==1)
{
screen.setColor(Color.yellow);
screen.drawOval((x*35)+5,(y*35)+5,25,25);
if(oldx!=0&&oldy!=0)
{
if(x==oldx&&y==oldy)
{
}
else
{
System.out.println(oldx+" "+oldy);
screen.setColor(Color.green);
screen.drawOval((x*35)+5,(y*35)+5,25,25);
}
}
oldy=y;oldx=x;
System.out.println("test");
}
}
}
}
They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.
-
This code doesn't explain it, you should post the complete code. If you have
"echo" mouseEvents the the reason should be how you set up and add the
listener.
eschew obfuscation
-
import static java.lang.System.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class checkers extends Applet implements MouseListener, MouseMotionListener
{
int[][]board = new int[9][9];
int player=1,c=0,turn=0,oldx=0,oldy=0;
public void paint(Graphics screen)
{
drawBoard();
placePieces();
setBoard();
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
Graphics screen = getGraphics();
screen.setColor(Color.gray);
screen.fillRect(325,60,80,20);
if(x<35||x>315||y<35||y>315)
{
}
else
moved(x/35,y/35);
}
public void mouseReleased(MouseEvent evt){}
public void mouseMoved(MouseEvent evt){}
public void mouseDragged(MouseEvent evt){}
public void mousePressed(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseExited(MouseEvent evt){}
public void drawBoard()
{
Graphics screen = getGraphics();
setBackground(Color.gray);
int co=-1;
screen.drawRect(33,33,(8*35)+4,(8*35)+4);
for(int x=1;x<=8;x++)
for(int y=1;y<=8;y++)
{
if(co==1)
{
screen.setColor(Color.black);
screen.fillRect(x*35,y*35,35,35);
co=-1;
}
else if(co==-1)
{
screen.setColor(Color.gray);
screen.fillRect(x*35,y*35,35,35);
co=1;
}
if(y==8)
co*=-1;
}
screen.setColor(Color.red);
screen.drawString("Messages: ",330,50);
}
public void placePieces()
{
Graphics screen = getGraphics();
screen.setColor(Color.red);
if(c==0)
{
for(int x=1;x<=8;x++)
for(int y=7;y<=8;y++)
screen.fillOval((x*35)+5,(y*35)+5,25,25);
screen.setColor(Color.white);
for(int x=1;x<=8;x++)
for(int y=1;y<=2;y++)
screen.fillOval((x*35)+5,(y*35)+5,25,25);
}
}
public void moved(int x,int y)
{
Graphics screen = getGraphics();
if(player==1)
{
if(board[y][x]==-1)
screen.drawString("Not yours",325,75);
else
{
if(board[y][x]==1)
{
screen.setColor(Color.yellow);
screen.drawOval((x*35)+5,(y*35)+5,25,25);
if(oldx!=0&&oldy!=0)
{
if(x==oldx&&y==oldy)
{
}
else
{
System.out.println(oldx+" "+oldy);
screen.setColor(Color.green);
screen.drawOval((x*35)+5,(y*35)+5,25,25);
}
}
oldy=y;oldx=x;
System.out.println("test");
}
}
}
}
public void setBoard()
{
if(c==0)
{
for(int x=1;x<=2;x++)
for(int y=1;y<=8;y++)
board[x][y]=-1;
for(int x=7;x<=8;x++)
for(int y=1;y<=8;y++)
board[x][y]=1;
for(int x=1;x<9;x++)
{
for(int y=1;y<9;y++)
System.out.print(board[x][y]);
System.out.println();
}
}
System.out.println();
}
}
They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.
-
You add your mouseListeners in the paint method, how many listeners do you really
want ?? , the paint method is invoked on every screen refresh...
add this method, its autoinvoked by the browser on startup:
Code:
public void init () {
addMouseListener(this);
addMouseMotionListener(this);
}
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|