Hiya,
I am trying to add a click counter to this code so that each time a disk is moved the number of moves is displayed on the screen. It is only one counter for all the disks, and if possible I'd like to display the number of clicks on screen.
I think that it is something like
saySomething("Mouse released; # of clicks: " + e.getClickCount(), e);
but cant get it to work.
Thanks for your help in advance.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Geese extends
Applet implements MouseListener,
MouseMotionListener {
private Point goose1, goose2, goose3, fox4, fox5, fox6, mouse;
private int select;
// declare two instance variables at the head of the program
private Image dbImage;
private Graphics dbg;
public void init(){
this.addMouseMotionListener(this);
this.addMouseListener(this);
select = 0;
goose1 = new Point (11,11);
goose2 = new Point (141,11);
goose3 = new Point (271,11);
fox4 = new Point (11,156);
fox5 = new Point (141,156);
fox6 = new Point (272,156);
mouse = new Point();
}
public void paint( Graphics g ){
g.setColor (Color.red);
g.fillOval(goose1.x, goose1.y, 30,30);
g.fillOval(goose2.x, goose2.y, 30,30);
g.fillOval(goose3.x, goose3.y, 30,30);
g.setColor (Color.yellow);
g.fillOval(fox4.x, fox4.y, 30,30);
g.fillOval(fox5.x, fox5.y, 30,30);
g.fillOval(fox6.x, fox6.y, 30,30);
}
public void mouseDragged(MouseEvent e){
mouse = e.getPoint();
if (select==1) goose1 = mouse;
if (select==2) goose2 = mouse;
if (select==3) goose3 = mouse;
if (select==4) fox4 = mouse;
if (select==5) fox5 = mouse;
if (select==6) fox6 = mouse;
repaint();
}
public void mouseMoved (MouseEvent e){}
//select a counter using the mouse
public void mousePressed (MouseEvent e){
mouse = e.getPoint();
if (mouse.x > goose1.x && mouse.x < goose1.x + 30 &&
mouse.y > goose1.y && mouse.y < goose1.y + 30) select = 1;
if (mouse.x > goose2.x && mouse.x < goose2.x + 30 &&
mouse.y > goose2.y && mouse.y < goose2.y + 30) select = 2;
if (mouse.x > goose3.x && mouse.x < goose3.x + 30 &&
mouse.y > goose3.y && mouse.y < goose3.y + 30) select = 3;
if (mouse.x > fox4.x && mouse.x < fox4.x + 30 &&
mouse.y > fox4.y && mouse.y < fox4.y + 30) select = 4;
if (mouse.x > fox5.x && mouse.x < fox5.x + 30 &&
mouse.y > fox5.y && mouse.y < fox5.y + 30) select = 5;
if (mouse.x > fox6.x && mouse.x < fox6.x + 30 &&
mouse.y > fox6.y && mouse.y < fox6.y + 30) select = 6;
}
public void mouseReleased(MouseEvent e){
}
// required for the interface
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
//** Update - Method, implements double buffering */
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
}