10 Replies Latest reply: Apr 30, 2010 11:54 PM by 793415 RSS

    problem adding scroll panel

    843807
      i have a problem adding a scroll panel to this image panel.....i tried creatign a object of ImagePanel but still cant get it....please help
      public class ImagePanel extends JPanel implements ChangeListener  {  
      
      String imgname="";
      
           public ImagePanel()
           {
           super();
       loadImage(imgname);public void paintComponent(Graphics g)
         {  super.paintComponent(g);
         
            if (image != null)
             {       
                  g.drawImage(image,0,0, (int) (width * zoomLevel),(int) (height * zoomLevel),null);
                 }         
         }
      
      public void loadImage(String name)
         {  
      
           imgname = name;
      
      Image loadedImage
               = Toolkit.getDefaultToolkit().getImage(name);
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(loadedImage, 0);
            try { tracker.waitForID(0); }
            catch (InterruptedException e) {}
            image = new BufferedImage(loadedImage.getWidth(null),
            loadedImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = image.createGraphics();
            g2.drawImage(loadedImage, 0,0, null);
            repaint();
         }
      
         private BufferedImage image; 
      }
      regards..
        • 1. Re: problem adding scroll panel
          793415
          nikhil_digde wrote:
          i have a problem adding a scroll panel..
          Did you mean a JScrollPane, or some 3rd party class that we do not know, and cannot help with. If you mean JScrollPane, then please be specific.
          .. to this image panel.....i tried creatign a object of ImagePanel but still cant get it..
          What is the problem?

          BTW
          1) For better help sooner, post an SSCCE.
          2) When posting code or code snippets, use one of the two standard forms of code bracketing and indenting. Your code as posted is very hard to understand.
          3) Do not [ignore exceptions|http://pscode.org/javafaq.html#stacktrace], especially in broken code.
          • 2. Re: problem adding scroll panel
            843807
            yes i want to add a JScrollPanel...i tried adding it but it was not visible....also that the super() method is used i tried creating an imagepanel object and then adding it to the scrollpanel

            it dint work



            regards,
            nikhil
            • 3. Re: problem adding scroll panel
              Darryl Burke
              yes i want to add a JScrollPanel
              Oh, so it's not a JScrollPane. You're on your own then.

              db
              • 4. Re: problem adding scroll panel
                843807
                its a JScrollPane sir!....please help...i tried it but not successful


                regards...
                • 5. Re: problem adding scroll panel
                  793415
                  You code does not compile. I suggest you address those problems first.

                  One thing that might help the compilation is to implement the standard forms of indenting code, as I suggested in my first reply.
                  • 6. Re: problem adding scroll panel
                    843807
                    thakns for teh reply....ok my code compiles now....i have removed all teh unwanted features...i want to add a scrollpane to the imagepanel.....i.e if the image is big then the scroll pane should be visible......since m going to add the option for zooming the image.......code is below..
                    ImageProcessor.java
                    
                    /////
                    
                    
                    import javax.swing.*;
                    import java.awt.*;
                    import java.awt.event.*;
                    import java.awt.image.*;
                    import java.io.*;
                    
                    public class ImageProcessor extends JFrame {  
                         
                          public ImageProcessor(String imgname){  
                          setLocation(300, 0);
                          contentPane = getContentPane();
                          panel = new ImagePanel();  
                          scrollPane = new JScrollPane(panel);
                          contentPane.add(scrollPane);
                           scrollPane=new JScrollPane(panel);
                          contentPane.add(scrollPane);
                          panel.loadImage(imgname);
                         }              
                    public static void main(String args[])
                     {
                           ImageProcessor ep = new ImageProcessor("3.jpg");
                            ep.setVisible(true);       
                    }
                       private ImagePanel panel;
                      JScrollPane scrollPane;
                          private Container contentPane;
                    }
                    ImagePanel.java
                    /////////
                    
                    
                    import javax.swing.*;
                    import java.awt.*;
                    import java.awt.geom.*;
                    import java.awt.image.*;
                    import java.awt.event.*;
                    import javax.swing.event.*;
                    
                    public class ImagePanel extends JPanel{  
                    
                    String imgname="";
                    
                         public ImagePanel()
                         {
                             super();               
                            setLayout(new BorderLayout());     
                          }                              
                                                  
                     public void paintComponent(Graphics g)
                       {  
                          super.paintComponent(g);
                          if (image != null)
                           {         
                              g.drawImage(image,0,0,null);
                           }             
                       }
                    
                     public void loadImage(String name)
                       {  
                    
                         imgname = name;
                    Image loadedImage
                             = Toolkit.getDefaultToolkit().getImage(name);
                          MediaTracker tracker = new MediaTracker(this);
                          tracker.addImage(loadedImage, 0);
                          try { tracker.waitForID(0); }
                          catch (InterruptedException e) {}
                          image = new BufferedImage(loadedImage.getWidth(null),
                          loadedImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
                          Graphics2D g2 = image.createGraphics();
                          g2.drawImage(loadedImage, 0,0, null);
                          repaint();
                       }
                       private BufferedImage image; 
                    }
                    
                    an image named 3.jpg should be present in teh bin....
                    
                    regards...
                    • 7. Re: problem adding scroll panel
                      843807
                      awaiting a reply...........

                      regards
                      • 8. Re: problem adding scroll panel
                        793415
                        nikhil_digde wrote:
                        awaiting a reply...........
                        While you are waiting, please:
                        1) Turn those two classes into an SSCCE by:
                        - Moving the 'Custom Frame' stuff into the main().
                        - Moving that main() into the ImagePanel class so it can be one source file
                        2) Make that SSCCE self contained by hot-linking to an image available off the internet.
                        3) Make the resulting mess comprehensible by using one of the standard forms of indenting and bracketing code blocks consistently.
                        (& while I'm at it)
                        4) Fixing that broken 'shift key' and applying it once at the start of each sentence.
                        5) Fixing that stuck '.' key.
                        • 9. Re: problem adding scroll panel
                          843807
                          hey dude...who ever u are....i asked u for help...if i knew everything i wouldnt have visited this forum..so if u can help me out..please do...otherwise get some help for urself.!!

                          peace!
                          • 10. Re: problem adding scroll panel
                            793415
                            nikhil_digde wrote:
                            ..get some help for urself.!!
                            Oh I did, yes, and it worked out quite well. :-)

                            Not 4 months ago, I asked about how to [Render JLabel without 1st displaying|http://forums.sun.com/thread.jspa?threadID=5423616]. Enlisting the help of the true Swing gurus (one of whom has been the only other respondent to this thread), the question was resolved within 24 hours.

                            But then, I implemented points 1 though 3 of the advice I offered to you, in my initial question. Also I generally abide by points 4 & 5, when asking or replying to questions, just because it is easier to read.

                            I get the impression you did not like either my advice, or the tone of my advice, but it did contain information that was aimed at bringing you to a satisfactory technical resolution. How you use it is up to you.

                            Of course, your statement might have meant help of a psychiatric nature, in which case I would advise you not to attack the people from whom you are asking help. That is never a good strategy.

                            If you choose to take this latest advice as an attack, and decide to come back with more jibes, I won't bother replying to them. This is just one relatively insignificant thread that will be forgotten in a week. OTOH I advise you to carefully re-read my advice, and all the linked documents (especially the document on the SSCCE), and decide whether you are more interested in:
                            a) People talking to you in a manner that you find comfortable.
                            b) A technical resolution.

                            If (b) then stop acting like an adolescent.

                            If (a) then I suggest you move to a forum where 'niceness is everything'. I have heard that JavaRanch is a forum of that type.
                            peace!
                            I think that term is "Peace out, y'all!". ;-)