Java after adding background, why program will be slower?

Daxi Song :

So if I add a background picture, then I click the button lots of times, my program will be slower and slower. But if I don't add that background picture, it won't get slower. Can anyone tell me why? Thanks! here is my JFrame class:

public class test extends JFrame implements ActionListener {
    private Container contentPane;

    public test() throws FileNotFoundException {
        // set the position of the GUI related with screen size
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
        this.setBounds((int) width / 4, (int) height / 4, (int) width / 2, (int) height / 2);

        //set background
        ImageIcon img = new ImageIcon("background.jpeg");

        // get the container
        contentPane = this.getContentPane();
        //contentPane.setLayout(new GridLayout(3, 1));

        /*
         *background panel
         */
        JPanel backgroundPanel = new JPanel(){
            {
                this.setOpaque(false);
            }
            public void paintComponent(Graphics g) {
                img.setImage(img.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_AREA_AVERAGING));
                g.drawImage(img.getImage(), 0, 0, this);
                super.paintComponent(g);

            }
        };
        contentPane.add(backgroundPanel);
        backgroundPanel.setLayout(new GridLayout(3, 1));


        /*
         * second area
         */
        JPanel secondPanel = new JPanel();
        secondPanel.setOpaque(false);
        // display button
        makeButton(secondPanel, "Display", this);
        backgroundPanel.add(secondPanel);
    }

    private void makeButton(JPanel p, String name, ActionListener target) {
        JButton b = new JButton(name);
        b.setFont(new Font(null, Font.PLAIN, 20));
        // add it to the specified JPanel and button group and make the JPanel listen
        p.add(b);
        b.addActionListener(target);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String command = e.getActionCommand();
        // Load button
        // Display button
        if (command.equals("Display")) {
            // check is there any document has been loaded
                // pop-up window
            JOptionPane.showMessageDialog(null, "Please load a document first!", "Error",JOptionPane.PLAIN_MESSAGE);
        }
        // show states button
    }
} 

here is the main method:

public class DocumentViewer {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        JFrame frm = new test();
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }

}
Hovercraft Full Of Eels :

Here:

public void paintComponent(Graphics g) {
    img.setImage(img.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_AREA_AVERAGING));
    g.drawImage(img.getImage(), 0, 0, this);
    super.paintComponent(g);
}

You're doing resource-hungry calculations within a painting method, a method that contributes greatly to user-perceived program responsiveness. Don't do this. Scale the image once, and store it in a variable, and then only draw the image within the paintComponent method.

If you need the image to resize with the GUI, then do the resizing within a ComponentListener, and again not within a painting method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356512&siteId=1