0

I am trying to get the JScrollPane to scroll at all times in this program. I am also trying to get this to scroll even when the image shrinks after the button is clicked. I can get the scroll pane to scroll only when the JFrame is maximized. Also once the image inside the JScrollPane is reduced by half when clicking on the button the JScrollPane no longer appears. I thought I could have the JScrollPane have the horizontal and vertical scrollbars at all times by making the Panel that holds it much much larger than the JFrame that holds the Panel. Please see the code below and help debug it if possible.

package Ex3;

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.AWTException;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.Image;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;




public class Ex3MF { 

  JFrame jframe = new JFrame("Crappy Screen Grabber 1.0");
  ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("screenshot-ex3mf.png"));
  JLabel jl;
  JScrollPane jsp;
  Container c = jframe.getContentPane();
  JButton button1 = new JButton("Resize Image");
  static Ex3MF e;
  static Toolkit t = Toolkit.getDefaultToolkit();
  static Dimension d = t.getScreenSize();
  static int x_width = d.width/2;
  static int x_height = d.width/2;
  JPanel p1 = new JPanel();
  JPanel p2 = new JPanel();


  void setupJFrame() {
      jframe.setSize(300, 300);
      jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jframe.setLocationRelativeTo(null);
      button1.addActionListener(new TrimImage());
      p2.setSize(200,100);
      //p2.setLayout(new BorderLayout());
      p2.add(button1);
      jframe.add(p2, BorderLayout.NORTH);


  }

  void image2ScrollPane(ImageIcon ic) {
      setupJFrame();
      p1.setSize(3000,3000);
      p1.setLayout(new BorderLayout());

      if (jl == null) {
          jl = new JLabel("Crappy Screencapture", ic, JLabel.CENTER);
      } else {
          jl.setIcon(ic);
      } 
      if (jsp == null) {
          jsp = new JScrollPane(jl);
      } else {
          jsp.setViewportView(jl);
      }
      p1.add(jsp);
      //c.add(p1);

      //p1.add(jl); //and the jlabel to the panel
     // jsp = new JScrollPane(p1); //and the panel to the ScrollPane

     // c.add(jsp); //add the JScrollPane to the Container

      jframe.add(p1, BorderLayout.CENTER);
      jframe.setVisible(true);
      jframe.pack();


  }

  void createScreenCapture() {
      try {
          Robot ro = new Robot();
          Toolkit t = Toolkit.getDefaultToolkit();
          final Dimension d = t.getScreenSize();
          Rectangle re = new Rectangle(d.width, d.height);
          BufferedImage image = ro.createScreenCapture(re);


          //write screen capture to disk
          ImageIO.write(image, "png", new File("screenshot-ex3mf.png"));
      } catch(IOException ioe) {
          System.out.println("IOException thrown " + ioe);
      }
        catch(AWTException awte) {
          System.out.println("AWTException thrown " + awte);
      }



  }

  ImageIcon resizeTheCapture() {

      Image image = icon.getImage();
      x_width = x_width / 2;
      x_height = x_height / 2;

      Image newimg = image.getScaledInstance((x_width), (x_height), java.awt.Image.SCALE_SMOOTH);
      return new ImageIcon(newimg);


  }

  public static void main(String[] args) {
      e = new Ex3MF();
      e.createScreenCapture();
      e.image2ScrollPane(e.icon);


      System.out.println("\nProgram complete...");

  }


} //end class

The code that controls the button follows this sentence.

package Ex3;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class TrimImage implements ActionListener {

    public void actionPerformed(ActionEvent ae) {
        Ex3MF ex3 = new Ex3MF();
        ex3.e.image2ScrollPane(ex3.e.resizeTheCapture());

    }


}

Thank-you for reading all of this. I appreciate any annoying snobby comments given and also any actual help towards making me a better programmer and other people across the world better programmers that are reading this after searching in their favorite search engine. :)

user3808269
  • 1,321
  • 3
  • 21
  • 40
  • 2
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). This means it should be one copy/paste, no redundant imports, and factor out the images with a placeholder component. But if you must use images.. One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Aug 16 '16 at 15:27

1 Answers1

0

Your JScrollPane will match the size of its contents. If your image within the JScrollPane is smaller than your JScrollPane, then scroll bars are not needed. I advice you to increase the size of your image in order to force your JScrollPane to scroll.

You can specify the size of a BufferedImage. Read about BufferedImages here and use this as an example of how to draw an image on a BufferedImage.

eighthrazz
  • 331
  • 1
  • 6
  • Thank-you for the answer @eightrazz. I will try this when I get a chance. :) I think you are correct that the scrollbars only appear when the image is beyond the size of the JScrollPane. I think I want to programmatically shrink the size of the JScrollPane when I shrink the image so I could possibly have scrollbars on an icon size images just for fun but obviously not for practical purposes. – user3808269 Aug 17 '16 at 17:24