Results 1 to 4 of 4
  1. #1
    neilimage is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default There is a problem. The image does'nt display on the panel2

    Is there anything wrong with the openFile or is there something else missing?
    because when i load the image using JFileChooser it gets loaded (the frame name changes to image name) but its does not display. is there any problem with the splitpane??
    here is the code:



    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    
    import javax.swing.border.BevelBorder;
    import javax.swing.border.EtchedBorder;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.awt.image.BufferedImageOp;
    import java.io.*;
    import java.awt.image.*;
    import java.util.Iterator;
    import java.awt.geom.*; 
    import javax.imageio.ImageIO;
    import javax.imageio.ImageWriter;
    import javax.imageio.stream.ImageOutputStream;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    import javax.swing.border.*;
    import java.awt.color.*;
    
    public class neil extends JFrame implements ActionListener 
    {
     BufferedImage image,img;
     public JMenuBar menuBar;
     JMenuItem grey,open;
     JMenu file,edit;
     JPanel panel1,panel2;
     JButton negativeB,embossB,brightB,sharpB,greyB,zoominB,zoomoutB,editT,openT,saveT,oriT,helpT;
     JLabel jb;
    
    
     neil()
    {
      
     try
      {
      JFrame frame = new JFrame();
      setTitle("FINAL LAYOUT");
     // setUndecorated(true);
     //getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      panel1=new JPanel();
      panel2=new JPanel();
      JLabel j1 = new JLabel("Area 1");
      JLabel j2 = new JLabel("Area 2");
     //panel1.add(j1);
      panel2.add(j2);
      JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
                    true, panel1, panel2);
        panel1.setLayout(new GridLayout(8,2));
        //panel1.setLayout(new BorderLayout());
        //splitPane.setOneTouchExpandable(true);
        splitPane.setDividerSize(2); 
        splitPane.setDividerLocation(165);
       // splitPane.getMaximumDividerLocation();
    
         getContentPane().add(splitPane);
         //JLabel jb=new JLabel(" to checck snsn snsna nsns  nsnnnnnnn nssnshshs uohoh dhbgfdjgsgjgfgdfgsdgfsdgsgfsdfgsd weather 
    
    //scccroll is working");
         splitPane.setPreferredSize(new Dimension(100,300));
    
      
          //JLabel jb=new JLabel
    
    ("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
           JScrollPane jScrollPane = new JScrollPane();
         jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
         jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
         splitPane.setRightComponent(jScrollPane);
        // splitPane.setResizeWeight(50.525);
        // splitPane.setMaximumSize(200,100);
    
         
         
      //setContentPane(panel);
      setSize(700,700);
      setVisible(true);
      
      //buttons in first panel
      
      brightB = new JButton(new ImageIcon("C://Bright.gif"));
      panel1.add(brightB);
      brightB.setVisible(false);
      brightB.addActionListener(this);
      brightB.setToolTipText("Bright");
    
      embossB= new JButton(new ImageIcon("C://Emboss.gif"));
      panel1.add(embossB);
      embossB.setVisible(false);
      embossB.addActionListener(this);
      embossB.setToolTipText("Emboss");
      
      sharpB = new JButton(new ImageIcon("C://Sharp.gif"));
      panel1.add(sharpB);
      sharpB.setVisible(false);
      sharpB.addActionListener(this);
      sharpB.setToolTipText("Sharp");
      
       greyB = new JButton(new ImageIcon("C://grey.png"));
       panel1.add(greyB); 
       greyB.addActionListener(this);
       greyB.setVisible(false);
       greyB.setToolTipText("Grey");
       
       zoominB = new JButton(new ImageIcon("C://zoomin.png"));
       panel1.add(zoominB);
       zoominB.setVisible(false);
       zoominB.addActionListener(this);
       zoominB.setToolTipText("Zoomin");
       
       zoomoutB= new JButton(new ImageIcon("C://zoomout.png"));
       panel1.add(zoomoutB);
       zoomoutB.setVisible(false);
       zoomoutB.addActionListener(this);
       zoomoutB.setToolTipText("Zoomout");
       
       negativeB = new JButton(new ImageIcon("C://Negative.gif"));
       panel1.add(negativeB);
       negativeB.setVisible(false);
       negativeB.addActionListener(this);
       negativeB.setToolTipText("Negative");
      /*greyB = new JButton(new ImageIcon("C://Gray.gif"));
        panel1.add(greyB); 
        brightB = new JButton(new ImageIcon("C://Bright.gif"));
        panel1.add(brightB);
       embossB= new JButton(new ImageIcon("C://Emboss.gif"));
        panel1.add(embossB);
        sharpB = new JButton(new ImageIcon("C://Sharp.gif"));
       panel1.add(sharpB);
       greyB = new JButton(new ImageIcon("C://Gray.gif"));
         panel1.add(greyB); 
         brightB = new JButton(new ImageIcon("C://Bright.gif"));
         panel1.add(brightB);
        embossB= new JButton(new ImageIcon("C://Emboss.gif"));
         panel1.add(embossB);
         sharpB = new JButton(new ImageIcon("C://Sharp.gif"));
        panel1.add(sharpB);
        greyB = new JButton(new ImageIcon("C://Gray.gif"));
          panel1.add(greyB); */
      //ho gaya :) 
    
      menuBar = new JMenuBar();
      menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
      
      file = new JMenu("File");
      open = new JMenuItem("open");
      file.add(open);
     open.addActionListener(this);
      
      edit = new JMenu("Edit");
      grey = new JMenuItem("Greyscale");
      edit.add(grey);
      
      menuBar.add(file);
      menuBar.add(edit);
      setJMenuBar(menuBar);
      
      //TOOLBAR buttons
      JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
      openT = new JButton(new ImageIcon("C://open.png"));
      toolbar.add(openT);
      openT.addActionListener(this);
      openT.setToolTipText("Open");
      
      saveT = new JButton(new ImageIcon("C://save.png"));
      toolbar.add(saveT);
      saveT.addActionListener(this);
      saveT.setToolTipText("Save");
      
      helpT = new JButton(new ImageIcon("C://help.png"));
      toolbar.add(helpT);
      helpT.addActionListener(this);
      helpT.setToolTipText("Help");
      
      
      editT= new JButton(new ImageIcon("C://edit.png"));
      toolbar.add(editT);
      editT.addActionListener(this);
      editT.setToolTipText("Edit");
      
      
      oriT= new JButton(new ImageIcon("C://Original.gif"));
      toolbar.add(oriT);
      oriT.addActionListener(this);
      oriT.setToolTipText("Original");
      
      getContentPane().add(toolbar,BorderLayout.NORTH);
     //done
     }
      catch(Exception e)
    			{
    			/*System.out.println("error:"+e.getMessage());
    			}
    			setContentPane(new JPanel()
    			{
    		
    			private static final long serialVersionUID = 1L;
    
    			public void paint(Graphics g)
    			{
    			g.drawImage(image,0,0,this);
    			}
    			});*/
    			} 
      /*setLeftComponent
      setRightComponent
      setTopComponent
      setBottomComponent*/
      
      }
    
    public void actionPerformed(ActionEvent e)
    	{
    		
    		if((e.getSource()==open) || (e.getSource()== openT ))                         
    		{ 
                 openFile();
            }
    		else if(e.getSource()== editT )
    		{
    			 greyB.setVisible(true);
    			 embossB.setVisible(true);
    			 sharpB.setVisible(true);
    			 zoominB.setVisible(true);
    			 zoomoutB.setVisible(true);
    			 greyB.setVisible(true);
    			 brightB.setVisible(true);
    			 negativeB.setVisible(true);
    		}
    		
                      }
    
    
    public void openFile()
    	{
    	try
    	{
    	JFileChooser chooser = new JFileChooser();
    	//chooser.setCurrentDirectory(new File("."));
    	int r = chooser.showOpenDialog(this);
    	if (r != JFileChooser.APPROVE_OPTION) return;
    	File f = chooser.getSelectedFile();
    	String name = f.getName();
    
    	image=ImageIO.read(chooser.getSelectedFile());
    	//box.add(new JLabel(new ImageIcon(image)));
    	//panel.add(new JScrollPane(box));
    	setSize(image.getWidth(), image.getHeight());
    	setTitle(name);
    	}
    	catch(Exception e)
    	{
    	}
    	}
    
    
    public static void main(String[] args) 
    {
     new neil(); 
    }
    }
    Last edited by Fubarable; 06-04-2011 at 07:53 PM. Reason: code tags added

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    You're not doing anything with the image you have just loaded; the paint( ... ) method is commented out but that is not the place where you want to paint the image anyway. Read the API documentation for the JFrame class to see how painting is delegated to a chid component of a JFrame.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator edit: code tags added to allow code to retain formatting (such as it is) and be more readable. Please see my signature below to see how to use these yourself.

    Regarding your question:
    You post a lot of code unrelated to your problem making it more difficult for both you and us to debug the actual problem. You're better off creating a small simple program built specifically to solve the issue at hand, and if unsuccessful, then posting it here (with code tags). Also, do your JPanel's drawing in its paintComponent method, not its paint method, and call super.paintComponent(g) as the first line of this method to help refresh the JPanel's graphics. Also, when changing the image, call repaint() on the JPanel.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You commented out code is attempting to paint the image at its actual size. There is no need to do custom painting for this. You only need to do custom painting if you scale or tile or use other painting effects with the image.

    See Background Panel Java Tips Weblog which has a solution for both approaches.

Similar Threads

  1. Display Image (png)
    By vimalaranjan in forum Java Gaming
    Replies: 2
    Last Post: 05-04-2011, 12:23 AM
  2. to display image
    By asmitarnd in forum AWT / Swing
    Replies: 6
    Last Post: 04-07-2011, 11:17 AM
  3. image display
    By asmitarnd in forum AWT / Swing
    Replies: 3
    Last Post: 04-07-2011, 11:17 AM
  4. Problem trying to display a .jpg image file on a web browser
    By allan123 in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 08-05-2010, 08:41 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
  •