Results 1 to 7 of 7
  1. #1
    Ryan.Vincent is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default JScrollPane is Cutting off my Image upon Scrolling

    Hello all,


    I am new to the forums and I am having an issue with Java (clearly).


    I am working on a program which is printing out essentially bar graphs which indicate the comparison between 2 plants in genetic data. I have all of the output working:

    JScrollPane is Cutting off my Image upon Scrolling-issue1.png

    However, when I scroll down within the JScrollPane, the image I have drawn is not properly redrawn:

    JScrollPane is Cutting off my Image upon Scrolling-issue2.png


    Also, here is a picture of the entire frame:

    JScrollPane is Cutting off my Image upon Scrolling-completeform.png


    Here is the code that is associated with this portion of the project:

    Java Code:
    class CompareToRefBars extends JPanel { //I need to give all information for the repaint
      Vector<Integer> listOfXVals;
      Vector<Integer> correspondingPlantNum;
      Vector<Vector <Byte>> plantComparison;
      BufferedImage myImage;
      
      public CompareToRefBars(Vector<Vector <Byte>> plantComp) {
        listOfXVals = new Vector();
        correspondingPlantNum = new Vector();
        plantComparison = plantComp;
        myImage = null;
        setPreferredSize(new Dimension(super.getWidth()-25, plantComparison.elementAt(0).size()*15+20));
      }
      
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //this.setPreferredSize(new Dimension(getWidth(), plantComparison.elementAt(0).size()*15+20)); 
        //draw(myImage.getGraphics());
        if (myImage != null){
          g.drawImage(myImage, 0, 0, this);
        }      
      }
      
      public void draw() {
        myImage = new BufferedImage(getWidth(), plantComparison.elementAt(0).size()*10+25, BufferedImage.TYPE_BYTE_BINARY);
        Graphics g = myImage.createGraphics();
        g.clearRect(0, 0, myImage.getWidth(), myImage.getHeight());
        g.setColor(new Color(132,137,208));
        g.fillRect(0, 0, myImage.getWidth(), myImage.getHeight());
        if (listOfXVals.size() > 0 && correspondingPlantNum.size() > 0) {
          for (int j=0; j<listOfXVals.size(); j++) {
            int y=5;
            int x2 = listOfXVals.elementAt(j)-13;
            int lab = correspondingPlantNum.elementAt(j);
            for (int i=0; i<plantComparison.elementAt(lab-1).size(); i++, y+=10)
            {
              if (plantComparison.elementAt(lab-1).elementAt(i) == -1)
                g.setColor(Color.GRAY.darker());
              else if (plantComparison.elementAt(lab-1).elementAt(i) == 0)
                g.setColor(Color.BLACK);
              else
                g.setColor(Color.WHITE);
              g.fillRect(x2, y, 25, 10);
            }
            g.setColor(Color.BLACK);
          }
        }
        listOfXVals = new Vector();
        correspondingPlantNum = new Vector();
        g.dispose();
      }
      
      public void printX() {
        System.out.println("In Function");
        System.out.println(listOfXVals.size());
        for (int i=0; i<listOfXVals.size(); i++) {
          System.out.println("All X: " + listOfXVals.elementAt(i));
        }
      }
      public void addXVal(int x) { listOfXVals.add(x); }
      public void addPlantNum(int num) { correspondingPlantNum.add(num); }
    }

    Drawing and Painting of the JPanel:
    Java Code:
    //Here is the call to draw the BufferedImage and to repaint the Panel.
    //Called from a separate JPanel's paint function, because I cannot paint my JPanel until
    //the current JPanel has painted (it gives required information).
    CompareToRefBars otherOut;
    otherOut.draw(); //Draw the image.
    otherOut.repaint(); //Repaint the JPanel to include the image
    Declaration of the JPanel:
    [CODE]
    CompareToRefBars outCompToRef;
    JScrollPane scroller = new JScrollPane(outCompToRef);
    [CODE]

    Also, I am not sure if this is a beginner or advanced issue, so I posted here.



    Any help would be greatly appreciated!




    EDIT - Whenever I click a button elsewhere on the form, the image automatically updates to be correctly displayed.
    For example, if I click the Top Left part of the JFrame (where the "Restore", "Move",...,"Close" options are), the image will repaint itself.

    EDIT 2 - I have reposted the fixed code, which is working for me.


    -Ryan Vincent
    Last edited by Ryan.Vincent; 04-19-2012 at 06:35 PM. Reason: Code Fix.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JScrollPane is Cutting off my Image upon Scrolling

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: JScrollPane is Cutting off my Image upon Scrolling

    You should not override JPanel's paint method but rather its paintComponent. Your super method would make a lot more sense if you do this since you appear to be calling super's paintComponent inside of paint which doesn't make sense.

    Edit: Also, it makes no sense to call update(...) in a Swing application. AWT, yes, this is fine, but Swing, no.
    Last edited by Fubarable; 04-19-2012 at 05:01 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JScrollPane is Cutting off my Image upon Scrolling

    You're doing the painting all wrong. That's old AWT code, did you copy it from some archive?

    Go through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    If you still have a problem after overriding the correct methods [and NOT overriding update(..) and paint(...)], post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: JScrollPane is Cutting off my Image upon Scrolling

    Also, your draw method should not be called from within paint or paintComponent, but should be called before calling repaint(). And don't forget to dispose of any Graphics objects that you yourself have created (the one obtained by calling getGraphics on your BufferedImage) to prevent running out of resources.

  6. #6
    Ryan.Vincent is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: JScrollPane is Cutting off my Image upon Scrolling

    Thank you very much Fubarable & DarrylBurke!

    The combination of both of your inputs helped me to get the problem fixed, and helped educate me in better java painting.


    DarrylBurke: The code was originally all in AWT, and I switch the extended classes over to swing (Panel to JPanel, etc.), but did not know that paintComponent(Graphics g) was supposed to be overridden, not paint(Graphics g).


    As a courtesy to others, should I repost the new code excerpt(s), or is that non-typical?


    Ryan Vincent

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JScrollPane is Cutting off my Image upon Scrolling

    The code isn't all that long, so yes, you could post it as an example of good practice.

    We might also nit-pick it further ;)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Automatic scrolling of JScrollPane
    By chayan in forum AWT / Swing
    Replies: 2
    Last Post: 07-03-2010, 06:51 AM
  2. Getting JScrollPane to stop auto-scrolling
    By Shadow in forum AWT / Swing
    Replies: 8
    Last Post: 10-21-2008, 09:52 PM
  3. Cutting parts of an image and reordering them
    By pedjacar in forum Java 2D
    Replies: 2
    Last Post: 10-16-2008, 10:46 AM
  4. How to display scrolling text and image on a JFrame
    By Abhi_vk in forum AWT / Swing
    Replies: 1
    Last Post: 06-20-2008, 11:19 PM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 09:16 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
  •