Results 1 to 3 of 3
  1. #1
    nuur is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default set coordinate to an image

    hello javarians...

    can any one guide me please... how I can set a coordinate on the background image so I can placed other image on top of that background image? like a collage?

    hopefully a reply soon.. thanks in advanced !

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

    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 Collage extends JPanel {
        BufferedImage top;
        BufferedImage bottom;
        Point loc = new Point();
    
        public Collage(BufferedImage[] images) {
            top = images[0];
            bottom = images[1];
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = (getWidth() - bottom.getWidth())/2;
            int y  = (getHeight() - bottom.getHeight())/2;
            g.drawImage(bottom, x, y, this);
            g.drawImage(top, loc.x, loc.y, this);
        }
    
        private void save() {
            int w = getWidth();
            int h = getHeight();
            int type = BufferedImage.TYPE_INT_RGB;
            BufferedImage save = new BufferedImage(w, h, type);
            Graphics2D g2 = save.createGraphics();
            paint(g2);
            g2.dispose();
            try {
                ImageIO.write(save, "jpg", new File("collage.jpg"));
            } catch(IOException e) {
                System.out.println("write error: " + e.getMessage());
            }
            ImageIcon icon = new ImageIcon(save);
            JOptionPane.showMessageDialog(this, icon, "save", -1);
        }
    
        private JPanel getSouth() {
            JButton button = new JButton("save");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    save();
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) throws IOException {
            String[] ids = { "hawk", "blackBear" };
            BufferedImage[] images = new BufferedImage[ids.length];
            for(int j = 0; j < images.length; j++) {
                String path = "images/" + ids[j] + ".jpg";
                images[j] = ImageIO.read(new File(path));
            }
            Collage test = new Collage(images);
            test.addMouseListener(test.ml);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getSouth(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                loc = e.getPoint();
                repaint();
            }
        };
    }

  3. #3
    nuur is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    thanks for the code..i`ll try to look after.. thanks again !

Similar Threads

  1. Image Verification
    By peiceonly in forum Java Servlet
    Replies: 2
    Last Post: 04-04-2009, 08:38 AM
  2. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 11:47 AM
  3. Uploading image using JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-11-2008, 10:16 AM
  4. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  5. how to set an image size
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:27 PM

Posting Permissions

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