Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32

Thread: Image Tagging

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Image Tagging

    For drawing shapes over the top of images, you need to extend a JPanel class and override its paintComponent method.
    In that method you will call Graphics class methods to draw the image and then draw the shapes.

    To get the location of the shapes to draw you will need to have a mouse listener that can capture the locations of presses and movements. These will be saved in variables for the paintComponent method to use.

    There are several programs on the forum that do different parts of this project. Use the Search to find the code.

  2. #22
    techack is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Image Tagging

    The current version of code is giving problem here
    Java Code:
    JLabel picLabel = new JLabel(new ImageIcon( bi ));
    imgpanel.add(picLabel);
    Two issues:

    1. When I'm clicking ChooseDirectory in Menu Bar and select an image, everytime it is adding a new picLabel. I want the current panel to be updated with the new selected Image.

    2. g.drawImage isnt drawing the image since I'm using ImageIcon for drawing the image but the below code is also not drawing the reactangle.Please suggest corrections for drawing a rectangle on the image.

    Java Code:
        public void paintComponent(Graphics g2D) {
            Graphics2D g = (Graphics2D) g2D;
            //g.drawImage(bi, 0, 0, null);
            g.setColor(Color.red);
            g.drawRect(50, 100, 100, 100);
            g.fillRect(50, 100, 100, 100);
    Current VERSION of code:
    Java Code:
    public class MyProg extends JPanel implements ActionListener {
    
    	JTextArea whitebox;
    	static BufferedImage bi;
    	static JFrame frame = new JFrame(" Image Protection ");
    	JPanel imgpanel = new JPanel();
    	
    	
    	public JPanel createContentPane()
        {	//Create Swing GUI Widgets here
            JPanel totalGUI = new JPanel();
            totalGUI.setMinimumSize(new Dimension(300, 200));
            totalGUI.setPreferredSize(new Dimension(300, 600));
                    
                   
    	    imgpanel.setLocation(0,0);
    	    //imgpanel.add();
    	   
    	    totalGUI.add(imgpanel);
                    
            whitebox = new JTextArea();
            whitebox.setEditable(false);
            whitebox.setLineWrap(true);
            whitebox.setWrapStyleWord(true);
            whitebox.setMinimumSize(new Dimension(300, 200));
            whitebox.setPreferredSize(new Dimension(200, 200));
            totalGUI.add(whitebox);
            
            totalGUI.setOpaque(true);
            return totalGUI;
        }
    
        public JMenuBar createMenuBar()
        {
            //Create the menu bar.
            JMenuBar menuBar = new JMenuBar();
    
            //Add a JMenu
            JMenu fileMenu = new JMenu("File");
            JMenu imageMenu = new JMenu("Image");
            JMenu helpMenu = new JMenu("Help"); 
    
            menuBar.add(fileMenu);
            menuBar.add(imageMenu);
            menuBar.add(helpMenu);
            
            // Now we want to fill each of the menus.
                    
            JMenuItem dirChooser = new JMenuItem("Choose Directory");
            dirChooser.addActionListener(this);
            JMenuItem exit = new JMenuItem("Exit");
            exit.addActionListener(this);
            
            fileMenu.add(dirChooser);
            fileMenu.add(exit);
     
    		// Image Menu
    		JMenuItem tagIt =  new JMenuItem("Tag It");
    		tagIt.addActionListener(this);
            JMenuItem saveTag = new JMenuItem("Save Tag");
            
            imageMenu.add(tagIt);
            imageMenu.add(saveTag);
    				  
            // helpMenu
    		JMenu help = new JMenu("Help");
            JMenuItem about =  new JMenuItem("About");
            
            helpMenu.add(about);
    	   
            return menuBar;
        }
    
        private static void createAndShowGUI() {
    
            JFrame.setDefaultLookAndFeelDecorated(true);
           // JFrame frame = new JFrame(" Image Protection ");
    
            //Create and set up the content pane.
            MyProg demo = new MyProg();
            frame.setContentPane(demo.createContentPane());
            // We now also set the MenuBar of the Frame to our MenuBar 
            frame.setJMenuBar(demo.createMenuBar());
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
            
     
            
        }
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		whitebox.append(e.getActionCommand() + " Selected \n");
    		
    		if(e.getActionCommand() == "Choose Directory")
    		{
                JFileChooser fc = new JFileChooser();
                fc.addChoosableFileFilter(new ImageFilter());
                fc.setFileView(new ImageFileView());
                fc.setAccessory(new ImagePreview(fc));
    
                int returnVal = fc.showOpenDialog(frame);
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fc.getSelectedFile();
    				System.out.println(selectedFile.getPath());
    				try {
    					bi = ImageIO.read(selectedFile);
    					
    					JLabel picLabel = new JLabel(new ImageIcon( bi ));
    					imgpanel.add(picLabel);
    					
    					//frame.add(picLabel);
    				} catch (IOException e1) {
    				}
    				frame.validate();
                    
                } else if(returnVal == JFileChooser.CANCEL_OPTION) 
    			{
    				System.out.println("cancelled");
    			}
            }
    }

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Image Tagging

    everytime it is adding a new picLabel
    Java Code:
      
         JLabel picLabel = new JLabel(new ImageIcon( bi ));
         imgpanel.add(picLabel);
    That is what you have written the code to do.

    I want the current panel to be updated with the new selected Image.
    Look at the API doc for JLabel and see if there is a method to do that.

    g.drawImage isnt drawing the image
    I don't see any call to drawImage in the executable code you posted???
    Last edited by Norm; 01-06-2012 at 05:42 PM.

  4. #24
    techack is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Image Tagging

    This the remainder part of the code.
    Where and how can I call it draw the rectangle.
    Also,unable to find solution for problem#1. Moving that JLabel code to anywhere else in the program is causing some Thread Exception

    Java Code:
        public void paintComponent(Graphics g2D) {
        	super.paintComponents(g2D); //Test line
            Graphics2D g = (Graphics2D) g2D;
            //g.drawImage(bi, 0, 0, null);
            g.setColor(Color.red);
            g.drawRect(50, 100, 100, 100);
            g.fillRect(50, 100, 100, 100);

  5. #25
    techack is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Image Tagging

    <Double posting>

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Image Tagging

    Where did you put that section of code? It's missing the ending } also.
    Moving that JLabel code to anywhere else in the program is causing some Thread Exception
    Please show the full text of the error message and the code that is causing the exception.

  7. #27
    techack is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Image Tagging

    Try moving that imgpanel.add to some place suitable so that a new JLabel isnt created everytime.
    Help with the problem#2 as well...

    Java Code:
    public class MyProg extends JPanel implements ActionListener {
    
    	JTextArea whitebox;
    	static BufferedImage bi;
    	static JFrame frame = new JFrame(" Image Protection ");
    	JPanel imgpanel = new JPanel();
    	
    	
    	public JPanel createContentPane()
        {	//Create Swing GUI Widgets here
            JPanel totalGUI = new JPanel();
            totalGUI.setMinimumSize(new Dimension(300, 200));
            totalGUI.setPreferredSize(new Dimension(300, 600));
                    
                   
    	    imgpanel.setLocation(0,0);
    	    //imgpanel.add();
    	   
    	    totalGUI.add(imgpanel);
                    
            whitebox = new JTextArea();
            whitebox.setEditable(false);
            whitebox.setLineWrap(true);
            whitebox.setWrapStyleWord(true);
            whitebox.setMinimumSize(new Dimension(300, 200));
            whitebox.setPreferredSize(new Dimension(200, 200));
            totalGUI.add(whitebox);
            
            totalGUI.setOpaque(true);
            return totalGUI;
        }
    
        public JMenuBar createMenuBar()
        {
            //Create the menu bar.
            JMenuBar menuBar = new JMenuBar();
    
            //Add a JMenu
            JMenu fileMenu = new JMenu("File");
            JMenu imageMenu = new JMenu("Image");
            JMenu helpMenu = new JMenu("Help"); 
    
            menuBar.add(fileMenu);
            menuBar.add(imageMenu);
            menuBar.add(helpMenu);
            
            // Now we want to fill each of the menus.
                    
            JMenuItem dirChooser = new JMenuItem("Choose Directory");
            dirChooser.addActionListener(this);
            JMenuItem exit = new JMenuItem("Exit");
            exit.addActionListener(this);
            
            fileMenu.add(dirChooser);
            fileMenu.add(exit);
     
    		// Image Menu
    		JMenuItem tagIt =  new JMenuItem("Tag It");
    		tagIt.addActionListener(this);
            JMenuItem saveTag = new JMenuItem("Save Tag");
            
            imageMenu.add(tagIt);
            imageMenu.add(saveTag);
    				  
            // helpMenu
    		JMenu help = new JMenu("Help");
            JMenuItem about =  new JMenuItem("About");
            
            helpMenu.add(about);
    	   
            return menuBar;
        }
    
        private static void createAndShowGUI() {
    
            JFrame.setDefaultLookAndFeelDecorated(true);
           // JFrame frame = new JFrame(" Image Protection ");
    
            //Create and set up the content pane.
            MyProg demo = new MyProg();
            frame.setContentPane(demo.createContentPane());
            // We now also set the MenuBar of the Frame to our MenuBar 
            frame.setJMenuBar(demo.createMenuBar());
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
            
     
            
        }
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		whitebox.append(e.getActionCommand() + " Selected \n");
    		
    		if(e.getActionCommand() == "Choose Directory")
    		{
                JFileChooser fc = new JFileChooser();
                fc.addChoosableFileFilter(new ImageFilter());
                fc.setFileView(new ImageFileView());
                fc.setAccessory(new ImagePreview(fc));
    
                int returnVal = fc.showOpenDialog(frame);
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fc.getSelectedFile();
    				System.out.println(selectedFile.getPath());
    				try {
    					bi = ImageIO.read(selectedFile);
    					
    
    					JLabel picLabel = new JLabel(new ImageIcon( bi ));
    					imgpanel.add(picLabel);
    										
    					//frame.add(picLabel);
    				} catch (IOException e1) {
    				}
    				frame.validate();
                    
                } else if(returnVal == JFileChooser.CANCEL_OPTION) 
    			{
    				System.out.println("cancelled");
    			}
            }
    			
    		else if(e.getActionCommand() == "tagIt")
    		{
    			
    			
    			/*	I have commented this part for now		
    			if(kanvas.created)
    			{
    				String namaTag = JOptionPane.showInputDialog(this, "Enter tag"); //Tag contents
    				if(namaTag!=null)
    				{
    					tag.add(new Tag(new Rectangle(kanvas.startx, kanvas.starty, kanvas.endx-kanvas.startx, kanvas.endy-kanvas.starty), namaTag));
    					kanvas.repaint();
    				}
    			}*/
    		}
    		else if(e.getActionCommand() == "saveTag")
    		{
    			//saveTag(new File(files[index].getPath()+".tagg"));
    			//JOptionPane.showMessageDialog(this, "Tag has been saved");
    		}
    		
    	}
        @Override
        public void paintComponent(Graphics g2D) {
        	super.paintComponents(g2D); //Test line
            Graphics2D g = (Graphics2D) g2D;
            //g.drawImage(bi, 0, 0, null);
            g.setColor(Color.red);
            g.drawRect(50, 100, 100, 100);
            g.fillRect(50, 100, 100, 100);
    
        }
    }

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Image Tagging

    Please explain What your problems are now.

  9. #29
    techack is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Image Tagging

    1. When I'm clicking ChooseDirectory in Menu Bar and select an image, everytime it is adding a new picLabel. I want the current panel to be updated with the new selected Image.

    2. g.drawImage isnt drawing the image since I'm using ImageIcon for drawing the image. Also, the code is also not drawing the reactangle.Please suggest corrections for drawing a rectangle on the image.

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Image Tagging

    I want the current panel to be updated with the new selected Image.
    there are two cases here:
    The first when there is no image shown -> that is what you currently have
    the second is when there is currently an image shown. Then you need to use a different method from the JLabel class
    This will require some thought about what logic you need in the listener to detect these two cases and do what is needed for each case.

    the code is also not drawing the reactangle.
    Is the code being called?

  11. #31
    techack is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Image Tagging

    For #1,
    need to use a different method from the JLabel class
    Can you elaborate on this? Suggest which method would be appropriate?

    For#2,
    How can I call it?Can you provide the code Norm?

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Image Tagging

    Read the API doc for the JLabel class. Look for methods that use/refer to icons.

    How can I call it
    You don't call it. It is called by the java virtual machine when the component needs to be repainted or after you call the repaint method.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  2. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  3. Version tagging CVS repository files from Java or ANT
    By Kjartan in forum Advanced Java
    Replies: 5
    Last Post: 04-04-2011, 05:05 AM
  4. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 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
  •