1

So I have a JPanel that's inside a JScrollPane. Now I am trying to paint something on the panel, but it's always in same spot. I can scroll in all directions but it's not moving. Whatever I paint on the panel does not get scrolled.

I already tried:

  1. A custom JViewPort
  2. switching between Opaque = true and Opaque = false

Also I considered overriding the paintComponent method of the panel but that would be really hard to implement in my code.

public class ScrollPanePaint{

public ScrollPanePaint() {
    JFrame frame = new JFrame();
    final JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(1000, 1000));
    //I tried both true and false
    panel.setOpaque(false);
    JScrollPane scrollPane = new JScrollPane(panel);
    frame.add(scrollPane);
    frame.setSize(200, 200);
    frame.setVisible(true);
    //To redraw the drawing constantly because that wat is happening in my code aswell because
    //I am creating an animation by constantly move an image by a little
    new Thread(new Runnable(){
        public void run(){
            Graphics g = panel.getGraphics();
            g.setColor(Color.blue);
            while(true){
                g.fillRect(64, 64, 3 * 64, 3 * 64);
                panel.repaint();
            }
        }
    }).start();
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new ScrollPanePaint();
        }
    });
}

}

The mistake I make is probably very easy to fix, but I just can't figure out how.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Roan
  • 1,200
  • 2
  • 19
  • 32
  • Why it is hard to implement the `paintComponent()` method on `JPanel`? – Braj May 31 '14 at 19:41
  • Because the all my drawing code would have to be inside the JPanels paintComponent method. An I need to draw on it from other subroutines. Like my entity class that draws the entity on the JPanel cannot be inside the paintComponent method. Or I might be doing things really silly. – Roan Jun 01 '14 at 13:17

1 Answers1

4

How to implement the paintComponent() on JPanel?

Override getPreferredSize() method instead of using setPreferredSize()

final JPanel panel = new JPanel(){
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        // your custom painting code here
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(40, 40);
    }
};

Some points:

  1. Override JComponent#getPreferredSize() instead of using setPreferredSize()

    Read more Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

  2. Use Swing Timer instead of Java Timer that is more suitable for Swing application.

    Read more How to Use Swing Timers

  3. Set default look and feel using UIManager.setLookAndFeel()

    Read more How to Set the Look and Feel

  4. How to fix animation lags in Java?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Still looking for how to implement this code into my game, but it certainly does what I want it to do. Thanks :) – Roan Jun 01 '14 at 13:21
  • So usefull links aswell. Thank you again. – Roan Jun 01 '14 at 13:23
  • 1
    Sounds good. Welcome you. Don't forget to close the thread if the issue is resolved. – Braj Jun 01 '14 at 14:19
  • 1
    By clicking on the green right mark in the answer. :) Take [2 minute tour of StackOverflow](http://stackoverflow.com/about). – Braj Jun 01 '14 at 15:22
  • I just wanted to thank you again. I finally found a way to implement this code. And it finally works as expected. Thank you so much!! :). – Roan Jun 07 '14 at 19:56