How to smoothen scrolling of JFrame in Java

前端 未结 3 1207
死守一世寂寞
死守一世寂寞 2020-12-03 16:49

I have a JFrame in my Java application that contains a JPanel where I have some drawing objects created at run-time. The problem is while scrolling the JF

3条回答
  •  一整个雨季
    2020-12-03 17:17

    Why not put the Graphics2D drawing in a (large) BufferedImage and display it in a label in a scroll-pane? Something like this (animated, 5000x5000px):

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import javax.swing.*;
    
    public class BigScrollImage {
    
        BigScrollImage() {
            final int x = 5000;
            final int y = 5000;
            final BufferedImage bi = new BufferedImage(x,y,BufferedImage.TYPE_INT_RGB);
            Graphics2D g1 = bi.createGraphics();
    
            g1.setColor(Color.BLACK);
            g1.fillRect(0, 0, x, y);
    
            g1.dispose();
    
            final JLabel label = new JLabel(new ImageIcon(bi));
    
            ActionListener listener = new ActionListener() {
                Random rand = new Random();
                @Override
                public void actionPerformed(ActionEvent ae) {
                    Graphics2D g2 = bi.createGraphics();
                    int x1 = rand.nextInt(x);
                    int x2 = rand.nextInt(x);
                    int y1 = rand.nextInt(y);
                    int y2 = rand.nextInt(y);
                    int r = rand.nextInt(255);
                    int g = rand.nextInt(255);
                    int b = rand.nextInt(255);
                    g2.setColor(new Color(r,g,b));
                    g2.drawLine(x1,y1,x2,y2);
    
                    g2.dispose();
                    label.repaint();
                }
            };
    
            Timer t = new Timer(5,listener);
    
            JScrollPane scroll = new JScrollPane(label);
            JFrame f = new JFrame("Big Scroll");
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            f.add(scroll);
            f.pack();
            f.setSize(800, 600);
    
            f.setLocationByPlatform(true);
            f.setVisible(true);
            t.start();
        }
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    new BigScrollImage();
                }
            });
        }
    }
    

    It tries to draw 200 hundred lines per second, and seems to scroll smoothly here.

提交回复
热议问题