0

My graphics paint area is too large and requires a scrollable function to increase the area that can be made visible. I am having trouble finding out how to do this.

I have tried add a JScrollPane but even when I am able to make it show up, it doesn't actually let me scroll through my drawing area. I already read the documentation on Custom Graphics but couldn't find anything about scroll bars. I am currently looking to see if I am able to somehow change the limits that the scroll bar has set (right now seems to be fixed to the size of the window) but was hoping I could get guidance.

public class Practice extends JPanel {

private String text;

Practice(){
    text="Test";
}
Practice(String x){
    text=x;
}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.BLACK);
    //drawing a bunch of shapes and text
}

public static void main(String[] args) {
    JFrame j = new JFrame("Test");
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setSize(500, 500);

    JScrollPane scrollPane = new JScrollPane(new Practice("Test"));
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    j.add(scrollPane);

    j.setVisible(true);        
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
stack ex
  • 111
  • 5
  • In order for `JScrollPane` to know when it should display/use scroll bars, the view port view (in this case, your `Practice` component) needs to provide appropriate sizing hints. These can be provided by overriding the `getPreferredSize` method and returning an appropriate size for the component, size, `new Dimension(1000, 1000)` for a test – MadProgrammer Apr 03 '20 at 00:18
  • @MadProgrammer Would you be willing to show me what that would look like. Is this a method I build or have to call? Sorry never done Java Graphics but been trying to learn it to earn some extra points on an assignment – stack ex Apr 03 '20 at 00:33

0 Answers0