Vote count:
0
So I have a JPanel that's inside a JScrollPane. Now I am trying to paint something on the JPanel, but it's always in same spot. I can scroll in all directions but it's not moving. Whatever I paint on the JPanel 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 JPanel 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.
asked 22 secs ago
Aucun commentaire:
Enregistrer un commentaire