2

I'm trying to make image scrollable using jPanel and jFrame in java. I'm draw my image in drawQuestion class then I add drawQuestion to jScrollPanel and It doesn't scroll. Please Tell me where my error is I've been trying to find for days and I still cant find it. Sorry for my bad English.

main.java:

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class main{

private static int width = 800;
private static int height = 450;

public static void main(String[] args){
    JFrame window = new JFrame("DPA Physics 2013 9 class");
    drawQuestion question = new drawQuestion();
    JScrollPane scroll = new JScrollPane(question);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(width,height);
    window.setLocationRelativeTo(null);

    window.add(scroll);

    window.setVisible(true);
}
}

drawQuestion.java:

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class drawQuestion extends JPanel{
private static final long serialVersionUID = 1L;

public void paintComponent(Graphics g){ 
    super.paintComponent(g);

    Image image1 = new ImageIcon(this.getClass().getResource("Page1.png")).getImage();

    g.drawImage(image1,0,0,this);
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1) `drawQuestion` should be called `DrawQuestion` or better `DrawableQuestion` and it should *return a preferred size the same size as the image.* 2) `Image image1 = new ImageIcon(this.getClass().getResource("Page1.png")).getImage();` The image should be either loaded when the panel is created, or passed to the panel in the constructor. 3) Far easier is to display the image in a `JLabel` in a scroll pane. [E.G.](http://stackoverflow.com/a/13463684/418556) – Andrew Thompson Apr 27 '13 at 07:00

1 Answers1

4

You've not specified a (preferred) size for the drawQuestion panel, which means all the layouts now think it's 0x0 in size....

You should NEVER load or perform any task that may take even a small amount of time to complete within any paint method...

You need to override the getPreferredSize method to return the required size of the panel. This will allow the scroll pane to determine if the panel needs scrolling...

public class DrawQuestion extends JPanel{
    private static final long serialVersionUID = 1L;

    private Image image1;
    public DrawQuestion() {
        image1 = new ImageIcon(this.getClass().getResource("Page1.png")).getImage();
    }

    public Dimension getPreferredSize() {
        return image1 == null ? super.getPreferredSize() : new Dimension(image1.getWidth(this), image1.getHeight(this));
    }

    public void paintComponent(Graphics g){ 
        super.paintComponent(g);

        if (image1 != null) {
            g.drawImage(image1,0,0,this);
        }
    }
}

While I'm on my wall...I'd also recommend ImageIO over ImageIcon as ImageIO will throw exceptions if the image can't be loaded...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366