codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  image mapping  sam_02 at 07:54 on Thursday, March 15, 2007
 

is there any way to map the image to handle mouse clicks? not a java applet,

click an object on the left, follows the left() function, the object on the right does right() function..

??

  Re: image mapping  crwood at 21:50 on Thursday, March 15, 2007
 


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ImageMapping extends JPanel {
BufferedImage image;
Rectangle[] cells = new Rectangle[0];
Dimension size = new Dimension(400,400);
int selectedIndex = -1;

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if(image == null) makeImage();
// For dynamic resizing behavior.
int x = (getWidth() - image.getWidth())/2;
int y = (getHeight() - image.getHeight())/2;
g2.drawImage(image, x, y, this);
cells[0].setLocation(x, y);
cells[1].setLocation(x+image.getWidth()/2, y);
for(int j = 0; j < cells.length; j++) {
Color color = Color.red;
if(j == selectedIndex) {
color = Color.green.darker();
}
g2.setPaint(color);
g2.draw(cells[j]);
}
}

private void makeImage() {
int w = 240;
int h = 240;
int type = BufferedImage.TYPE_INT_RGB;
image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setBackground(new Color(220,220,240));
g2.clearRect(0,0,w,h);
int r = Math.min(w,h)/3;
g2.setPaint(Color.pink);
g2.draw(new Ellipse2D.Double(w/2-r, h/2-r, 2*r, 2*r));
g2.setPaint(Color.blue);
double thetaInc = Math.toRadians(72);
double theta = -Math.PI/2 - thetaInc/2;
for(int j = 0; j < 5; j++) {
double x1 = w/2 + r*Math.cos(theta);
double y1 = h/2 + r*Math.sin(theta);
theta += thetaInc/2;
double ctrlx = w/2 + 2*r*Math.cos(theta);
double ctrly = h/2 + 2*r*Math.sin(theta);
theta += thetaInc/2;
double x2 = w/2 + r*Math.cos(theta);
double y2 = h/2 + r*Math.sin(theta);
g2.draw(new QuadCurve2D.Double(x1, y1, ctrlx, ctrly, x2, y2));
}
g2.dispose();
cells = new Rectangle[2];
cells[0] = new Rectangle(w/2,h);
cells[1] = new Rectangle(w/2,h);
}

public Dimension getPreferredSize() {
return size;
}

public static void main(String[] args) {
ImageMapping test = new ImageMapping();
test.addMouseMotionListener(test.mml);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}

private MouseMotionListener mml = new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
boolean hover = false;
for(int j = 0; j < cells.length; j++) {
if(cells[j].contains(p)) {
hover = true;
if(j != selectedIndex) {
// We've entered a new cell, change selectedIndex.
// Call your methods here, eg,
// if(j == 0) left();
selectedIndex = j;
repaint();
}
break;
}
}
if(!hover && selectedIndex != -1) {
selectedIndex = -1;
repaint();
}
}
};
}


  Re: image mapping  sam_02 at 03:33 on Friday, March 16, 2007
 

thanks for the great example.., i'll jus spend some time to study the code..










CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me
•  pls help me with this..
•  Re: Security - Code verify
•  Job @ EarlySail
•  Job @ EarlySail (perl)


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007