[英]Java after adding background, why program will be slower?
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:
这是我的 JFrame 类:
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);
}
}
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.
将图像缩放一次,并将其存储在变量中,然后仅在paintComponent 方法中绘制图像。
If you need the image to resize with the GUI, then do the resizing within a ComponentListener, and again not within a painting method.如果您需要使用 GUI 调整图像大小,请在 ComponentListener 中调整大小,而不是在绘制方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.