Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2007, 05:28 AM
Member
 
Join Date: Jul 2007
Posts: 39
coco is on a distinguished road
Help with image mapping
Hi, 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.

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:06 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImageMap extends JPanel { BufferedImage image; Rectangle left; Rectangle right; boolean showGrid = true; public ImageMap(BufferedImage image) { this.image = image; addMouseListener(ml); addComponentListener(cl); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if(left == null) initRects(); g2.drawImage(image, left.x, left.y, this); if(showGrid) { g2.setPaint(Color.red); g2.draw(left); g2.draw(right); } } public Dimension getPreferredSize() { return new Dimension(image.getWidth(), image.getHeight()); } private void initRects() { int w = getWidth(); int h = getHeight(); int iw = image.getWidth(); int ih = image.getHeight(); int x = (w - iw)/2; int y = (h - ih)/2; left = new Rectangle(x, y, iw/2, ih); right = new Rectangle(x+iw/2, y, iw/2, ih); } public static void main(String[] args) throws IOException { String path = "images/owls.jpg"; BufferedImage image = ImageIO.read(new File(path)); ImageMap map = new ImageMap(image); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(map)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private MouseListener ml = new MouseAdapter() { public void mousePressed(MouseEvent e) { Point p = e.getPoint(); String s = "not over image"; if(left.contains(p)) s = "left"; if(right.contains(p)) s = "right"; System.out.println("s = " + s); } }; private ComponentListener cl = new ComponentAdapter() { public void componentResized(ComponentEvent e) { left = null; repaint(); } }; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 07:29 PM
mapping servlets in user defined xml file and invoking them praneeth Advanced Java 0 07-16-2007 10:45 PM
About servlet-mapping Heather Java Servlet 1 07-14-2007 08:00 PM
Mapping of class Association with hibernate Albert Database 1 07-06-2007 08:27 AM
org.hibernate.DuplicateMappingException: Duplicate class/entity mapping project Ed Database 2 07-04-2007 07:17 AM


All times are GMT +3. The time now is 03:08 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org