Switch to full style
Graphics and animation Java code examples
Post a reply

show the locations of clicks on the image

Sat Feb 09, 2013 7:45 pm

show the locations of clicks in the image in the status message area useful when setting up Image Maps
java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
/*
* @(#)ClickArea.java 1.3 95/10/13
*
* Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
* without fee is hereby granted.
* Please refer to the file java.sun.com/copy_trademarks.html
* for further important copyright and trademark information and to
* java.sun.com/licensing.html for further important licensing
* information for the Java (tm) Technology.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/

import java.awt.Graphics;

/**
* An click feedback ImageArea class.
* This class extends the basic ImageArea Class to show the locations
* of clicks in the image in the status message area. This utility
* ImageArea class is useful when setting up ImageMaps.
*
* @author Jim Graham
* @version 1.3, 13 Oct 1995
*/
class ClickArea extends ImageMapArea {
/** The X location of the last mouse press. */
int startx;
/** The Y location of the last mouse press. */
int starty;

static String ptstr(int x, int y) {
return "("+x+", "+y+")";
}

/**
* When the user presses the mouse button, start showing coordinate
* feedback in the status message line.
*/
public boolean press(int x, int y) {
showStatus("Clicked at "+ptstr(x, y));
startx = x;
starty = y;
return false;
}

/**
* Update the coordinate feedback every time the user moves the mouse
* while he has the button pressed.
*/
public boolean drag(int x, int y) {
showStatus("Rectangle from "+ptstr(startx, starty)
+" to "+ptstr(x, y)
+" is "+(x-startx)+"x"+(y-starty));
return false;
}

/**
* Update the coordinate feedback one last time when the user releases
* the mouse button.
*/
public boolean lift(int x, int y) {
return drag(x, y);
}
}


java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
/*
* @(#)ImageMapArea.java 1.3 95/10/13
*
* Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
* without fee is hereby granted.
* Please refer to the file java.sun.com/copy_trademarks.html
* for further important copyright and trademark information and to
*java.sun.com/licensing.html for further important licensing
* information for the Java (tm) Technology.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.*;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.MalformedURLException;

/**
* The base ImageArea class.
* This class performs the basic functions that most ImageArea
* classes will need and delegates specific actions to the subclasses.
*
* @author Jim Graham
* @version 1.3, 13 Oct 1995
*/
class ImageMapArea implements ImageObserver {
/** The applet parent that contains this ImageArea. */
ImageMap parent;
/** The X location of the area (if rectangular). */
int X;
/** The Y location of the area (if rectangular). */
int Y;
/** The size().width of the area (if rectangular). */
int W;
/** The size().height of the area (if rectangular). */
int H;
/**
* This flag indicates whether the user was in this area during the
* last scan of mouse locations.
*/
boolean entered = false;
/** This flag indicates whether the area is currently highlighted. */
boolean active = false;

/**
* This is the default highlight image if no special effects are
* needed to draw the highlighted image. It is created by the
* default "makeImages()" method.
*/
Image hlImage;

/**
* Initialize this ImageArea as called from the applet.
* If the subclass does not override this initializer, then it
* will perform the basic functions of setting the parent applet
* and parsing out 4 numbers from the argument string which specify
* a rectangular region for the ImageArea to act on.
* The remainder of the argument string is passed to the handleArg()
* method for more specific handling by the subclass.
*/
public void init(ImageMap parent, String args) {
this.parent = parent;
StringTokenizer st = new StringTokenizer(args, ", ");
X = Integer.parseInt(st.nextToken());
Y = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
if (st.hasMoreTokens()) {
// hasMoreTokens() Skips the trailing comma
handleArg(st.nextToken(""));
} else {
handleArg(null);
}
makeImages();
}

/**
* This method handles the remainder of the argument string after
* the standard initializer has parsed off the 4 rectangular
* parameters. If the subclass does not override this method,
* the remainder will be ignored.
*/
public void handleArg(String s) {
}

/**
* This method loads any additional media that the ImageMapArea
* may need for its animations.
*/
public void getMedia() {
}

/**
* This method is called every animation cycle if there are any
* active animating areas.
* @return true if this area requires further animation notifications
*/
public boolean animate() {
return false;
}

/**
* This method sets the image to be used to render the ImageArea
* when it is highlighted.
*/
public void setHighlight(Image img) {
hlImage = img;
}

/**
* This method handles the construction of the various images
* used to highlight this particular ImageArea when the user
* interacts with it.
*/
public void makeImages() {
setHighlight(parent.getHighlight(X, Y, W, H));
}

/**
* The repaint method causes the area to be repainted at the next
* opportunity.
*/
public void repaint() {
parent.repaint(0, X, Y, W, H);
}

/**
* This method tests to see if a point is inside this ImageArea.
* The standard method assumes a rectangular area as parsed by
* the standard initializer. If a more complex area is required
* then this method will have to be overridden by the subclass.
*/
public boolean inside(int x, int y) {
return (x >= X && x < (X + W) && y >= Y && y < (Y + H));
}

/**
* This utility method draws a rectangular subset of a highlight
* image.
*/
public void drawImage(Graphics g, Image img, int imgx, int imgy,
int x, int y, int w, int h) {
Graphics ng = g.create();
ng.clipRect(x, y, w, h);
ng.drawImage(img, imgx, imgy, this);
}

/**
* This method handles the updates from drawing the images.
*/
public boolean imageUpdate(Image img, int infoflags,
int x, int y, int width, int height) {
if (img == hlImage) {
return parent.imageUpdate(img, infoflags, x + X, y + Y,
width, height);
} else {
return (infoflags & (ALLBITS | ERROR)) == 0;
}
}

/**
* This utility method shows a string in the status bar.
*/
public void showStatus(String msg) {
parent.getAppletContext().showStatus(msg);
}

/**
* This utility method tells the browser to visit a URL.
*/
public void showDocument(URL u) {
parent.getAppletContext().showDocument(u);
}

/**
* This method highlights the specified area when the user enters
* it with his mouse. The standard highlight method is to replace
* the indicated rectangular area of the image with the primary
* highlighted image.
*/
public void highlight(Graphics g) {
}

/**
* The checkEnter method is called when the mouse is inside the
* region to see if the area needs to have its enter method called.
* The default implementation simply checks if the entered flag is
* set and only calls enter if it is false.
*/
public boolean checkEnter(int x, int y) {
if (entered) {
return isTerminal();
} else {
entered = true;
return enter(x, y);
}
}

/**
* The checkExit method is called when the mouse is outside the
* region to see if the area needs to have its exit method called.
* The default implementation simply checks if the entered flag is
* set and only calls exit if it is true.
*/
public void checkExit() {
if (entered) {
entered = false;
exit();
}
}

/**
* The isTerminal method controls whether events propagate to the
* areas which lie beneath this one.
* @return true if the events should be propagated to the underlying
* areas.
*/
public boolean isTerminal() {
return false;
}

/**
* The enter method is called when the mouse enters the region.
* The location is supplied, but the standard implementation is
* to call the overloaded method with no arguments.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the enter
*/
public boolean enter(int x, int y) {
return enter();
}

/**
* The overloaded enter method is called when the mouse enters
* the region. This method can be overridden if the ImageArea
* does not need to know where the mouse entered.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the enter
*/
public boolean enter() {
return isTerminal();
}

/**
* The exit method is called when the mouse leaves the region.
*/
public void exit() {
}

/**
* The press method is called when the user presses the mouse
* button inside the ImageArea. The location is supplied, but
* the standard implementation is to call the overloaded method
* with no arguments.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the press
*/
public boolean press(int x, int y) {
return press();
}

/**
* The overloaded press method is called when the user presses the
* mouse button inside the ImageArea. This method can be overridden
* if the ImageArea does not need to know the location of the press.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the press
*/
public boolean press() {
return isTerminal();
}

/**
* The lift method is called when the user releases the mouse button.
* The location is supplied, but the standard implementation is to
* call the overloaded method with no arguments. Only those ImageAreas
* that were informed of a press will be informed of the corresponding
* release.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the lift
*/
public boolean lift(int x, int y) {
return lift();
}

/**
* The overloaded lift method is called when the user releases the
* mouse button. This method can be overridden if the ImageArea
* does not need to know the location of the release.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the lift
*/
public boolean lift() {
return isTerminal();
}

/**
* The drag method is called when the user moves the mouse while
* the button is pressed. Only those ImageAreas that were informed
* of a press will be informed of the corresponding mouse movements.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the drag
*/
public boolean drag(int x, int y) {
return isTerminal();
}
}




Post a reply
  Related Posts  to : show the locations of clicks on the image
 get time difference between to mouse-clicks-double clicks     -  
 Show image using before property     -  
 image slide show     -  
 Show image when click on link     -  
 Show loading image from JQuery     -  
 How to show full resolution image at clicking on thumnail     -  
 subtract number from memory locations     -  
 Applet get mouse clicks and draw circle     -  
 draw any number of lines on mouse clicks     -  
 draw any number of circles using mouse clicks     -  

Topic Tags

Java Graphics, Java Image