I have a JPanel in a JScrollpane. I draw on a BufferedImage, which I display on the JPanel. In the top left corner of the JScrollpane, I want a picture, that always stays in that corner when I scroll down to see the rest of my JPanel. Here the paintComponent method of the Jpanel:
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
if (bufferedImage != null){
g.drawImage(bufferedImage, 0, 0, this);
Point p = parent.getViewPosition();
System.out.println("paintComponent(): "+ p.x + "," + p.y);
g.setColor(Color.RED);
g.fillRect(p.x + 20, p.y + 20, 200, 200);
}
}
Where parent.getViewPosition() give me the scrollPane.getViewport().getViewPosition(). When I start up, I can see the buffered image with the red rectangle in the top left corner. When I scroll down, I can see the rest of the buffered image, but the red rectangle moves up and then disappaeres and don't come again when I scroll up. In the console I can see that point p changes when I scroll:
paintComponent(): 0,0
paintComponent(): 0,10
paintComponent(): 0,20
paintComponent(): 0,30
paintComponent(): 0,40
paintComponent(): 0,50
Can anyone help me with this problem?
paint()
, useJViewPort.setScrollMode(JViewport.Xxx)
, use ownRepaintManager
, but I'd be useGlassPane
orJLayer
– Unmeriting