Results 1 to 3 of 3
Thread: set coordinate to an image
- 04-01-2008, 03:55 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
- 04-01-2008, 05:13 AM #2
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(); } }; }
- 04-01-2008, 08:08 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Image Verification
By peiceonly in forum Java ServletReplies: 2Last Post: 04-04-2009, 07:38 AM -
X&Y Coordinate Drawing on jPanel
By BHCluster in forum Java 2DReplies: 2Last Post: 03-27-2008, 10:47 AM -
Uploading image using JSP
By Java Tip in forum Java TipReplies: 0Last Post: 01-11-2008, 09:16 AM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
how to set an image size
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks