Results 1 to 5 of 5
  1. #1
    daredavil82 is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default hai everyone....

    i have develop word processor applicatiom....menu for inserting image for insert image in document not working well...i can insert image inside the document...but i cant save that image inside my document..and also cannot resize that image..image became static..i will lose the image went i save it..plz help.....

    public void actionPerformed(ActionEvent e)
    {
    fc = new JFileChooser();
    fc.addChoosableFileFilter(m_gifFilter);
    fc.addChoosableFileFilter(m_jpgFilter);
    fc.setComponentOrientation(java.awt.ComponentOrien tation.RIGHT_TO_LEFT);
    fc.setDialogTitle("\u0645\u0627\u0633\u0648\u0642\ u06A9\u0646\u0020\u0627\u064A\u0645\u064A\u062C");


    fc.setFileFilter(m_gifFilter);
    fc.removeChoosableFileFilter(m_rtfFilter);
    Thread runner = new Thread()
    {
    public void run()
    {
    if (fc.showOpenDialog(OJMain.this) != JFileChooser.APPROVE_OPTION)
    return;
    OJMain.this.repaint();
    File fChoosen = fc.getSelectedFile();
    ImageIcon icon = new ImageIcon(fChoosen.getPath());
    int w = icon.getIconWidth();
    int h = icon.getIconHeight();
    if (w<=0 || h<=0)
    {

    JOptionPane.showMessageDialog(OJMain.this,"\u06A9\ u06AC\u0627\u06AC");
    fChoosen.getPath(), error ,JOptionPane.WARNING_MESSAGE);
    return;
    }

    MutableAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setIcon(attr, icon);
    int p = tpOpenJawi.getCaretPosition();
    try
    {
    m_doc.insertString(p, " ", attr);
    }
    catch (BadLocationException ex)
    {}


    fc.addChoosableFileFilter(m_rtfFilter);
    fc.setFileFilter(m_rtfFilter);
    fc.removeChoosableFileFilter(m_gifFilter);
    fc.removeChoosableFileFilter(m_jpgFilter);
    }
    };
    runner.start();
    }


    });

    OJToolbar.add(jbInsertImage);

    jbRTL.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/rtl.png")));
    jbRTL.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));

    jbRTL.setMaximumSize(new java.awt.Dimension(31, 31));
    jbRTL.setMinimumSize(new java.awt.Dimension(31, 31));
    jbRTL.setPreferredSize(new java.awt.Dimension(31, 31));
    jbRTL.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jbRTLActionPerformed(evt);
    }
    });

  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 java.util.Enumeration;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextPaneImage implements ActionListener {
        JFileChooser fileChooser = new JFileChooser("images");
        JTextPane textPane;
        double scale = 0.5;
    
        public void actionPerformed(ActionEvent e) {
            String ac = e.getActionCommand();
            if(ac.equals("OPEN"))
                openDialog();
            if(ac.equals("REMOVE"))
                removeIcon();
            if(ac.equals("RESIZE")) {
                ImageIcon icon = removeIcon();
                if(icon != null)
                    insertIcon(scale(icon));
            }
        }
    
        private void openDialog() {
            if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                if(isSupportedImage(file)) {
                    BufferedImage image = null;
                    try {
                        image = ImageIO.read(file);
                    } catch(IOException e) {
                        System.out.println("Read error for " + file.getName() +
                                           ": " + e.getMessage());
                    }
                    //System.out.println("fileName = " + file.getName());
                    ImageIcon icon = new ImageIcon(image, file.getName());
                    //JOptionPane.showMessageDialog(textPane, icon, "", -1);
                    insertIcon(icon);
                }
                textPane.requestFocusInWindow();
            }
        }
    
        private boolean isSupportedImage(File file) {
            String ext = getExtension(file);
            return ImageIO.getImageReadersBySuffix(ext).hasNext();
        }
    
        private String getExtension(File file) {
            String s = file.getPath();
            int dot = s.lastIndexOf(".");
            return s.substring(dot+1);
        }
    
        private void insertIcon(ImageIcon icon) {
            StyledDocument doc = textPane.getStyledDocument();
            String name = "icon:" + icon.getDescription();
            Style style = doc.addStyle(name, null);
            StyleConstants.setIcon(style, icon);
            try {
                doc.insertString(doc.getLength(), " ", style);
            } catch(BadLocationException e) {
                System.out.printf("bad location error: %s%n", e.getMessage());
            }
        }
    
        private ImageIcon removeIcon() {
            int pos = textPane.getCaretPosition();
            DefaultStyledDocument doc =
                (DefaultStyledDocument)textPane.getStyledDocument();
            Element charElement = doc.getCharacterElement(pos);
            //System.out.printf("charElement(%d):%n", pos);
            //showAttributes(charElement.getAttributes());
            //System.out.println("---------------");
            Icon icon = getIcon(charElement.getAttributes());
            if(icon == null)
                return null;
            String description = ((ImageIcon)icon).getDescription();
            //System.out.printf("icon description = %s%n", description);
            int start = charElement.getStartOffset();
            int end = charElement.getEndOffset();
            try {
                doc.remove(start, end-start);
            } catch(BadLocationException e) {
                System.out.println("bad location for start = " + start +
                                   " end = " + end + ": " + e.getMessage());
            }
            doc.removeStyle("icon:" + description);
            return (ImageIcon)icon;
        }
    
        private void showAttributes(AttributeSet attrs) {
            Enumeration e = attrs.getAttributeNames();
            while(e.hasMoreElements()) {
                Object name = e.nextElement();
                Object value = attrs.getAttribute(name);
                //System.out.printf("name: %s  value: %s%n", name, value);
            }
        }
    
        private Icon getIcon(AttributeSet attrs) {
            Enumeration e = attrs.getAttributeNames();
            while(e.hasMoreElements()) {
                Object name = e.nextElement();
                //System.out.println("name = " + name);
                if(name.toString().equals("icon"))
                    return (Icon)attrs.getAttribute(name);
            }
            return null;
        }
    
        private ImageIcon scale(ImageIcon icon) {
            int w = (int)(scale*icon.getIconWidth());
            int h = (int)(scale*icon.getIconHeight());
            int type = BufferedImage.TYPE_INT_RGB;
            BufferedImage image = new BufferedImage(w, h, type);
            Graphics2D g2 = image.createGraphics();
            g2.drawImage(icon.getImage(), 0, 0, w, h, null);
            g2.dispose();
            return new ImageIcon(image);
        }
    
        private JScrollPane getTextComponent() {
            textPane = new JTextPane();
            return new JScrollPane(textPane);
        }
    
        private JPanel getUIPanel() {
            String[] ids = { "open", "remove", "resize" };
            JPanel panel = new JPanel();
            for(int j = 0; j < ids.length; j++) {
               JButton button = new JButton(ids[j]);
               button.setActionCommand(ids[j].toUpperCase());
               button.addActionListener(this);
               panel.add(button);
            }
            return panel;
        }
    
        public static void main(String[] args) {
            TextPaneImage test = new TextPaneImage();
            JFrame f = new JFrame("Set caret to remove/resize");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test.getTextComponent());
            f.getContentPane().add(test.getUIPanel(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    daredavil82 is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Thumbs up thank you for your reply.....

    my friend tq very much for ur reply....can u help.....plz....can i resize image without click on button...like in open office or microsoft word..went we insert the image inside document it automaticly can resize with just playing around the image...can mover the picture around in the document...tq.:)

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

    Default

    image inside document it automaticly can resize with just playing around the image...can mover the picture around in the document
    Two suggestions for this:
    1 You could put a glass pane over the JRootPane of your application. Add a MouseListener and a MouseMotionListener to it and manipulate the image on the glass pane. You could remove it from the textPane on dragGesture recognition and insert the resized image back into the textPane on mouseReleased. You could draw a resizing rectangle with drag handles on the glass pane and around the image to accommodate the manipulation.
    2 You could do all the text and images on a JPanel. You can use the LineBreakMeasurer and TextLayout classes to manage the text layout. Add the MouseListener and MouseMotionListener to the panel and you could manipultate the image with them. The LineBreakMeasurer class api comments section includes example code for layout of text.

  5. #5
    enfosteNown is offline Member
    Join Date
    Jan 2009
    Location
    Trinidad and Tobago
    Posts
    1
    Rep Power
    0

Posting Permissions

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