Results 1 to 2 of 2
  1. #1
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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();
            }
        };
    }

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 10:04 PM
  2. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  3. Replies: 0
    Last Post: 07-16-2007, 08:45 PM
  4. About servlet-mapping
    By Heather in forum Java Servlet
    Replies: 1
    Last Post: 07-14-2007, 06:00 PM
  5. Replies: 1
    Last Post: 07-06-2007, 06:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •