在将缓存图像从1920 x 224缩放到8777 x 1024并以30 fps绘制时,我遇到了cpu占用完整处理器的问题。
我尝试使用。
仅显示jpanel的高度和宽度drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer)
但仍然是cpu的主要飙升。
我的测试代码如下:
package test;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
public class GamePanel extends JPanel{
int frame = 0;
BufferedImage srcImg;
BufferedImage backgroundImg;
Timer timer = new Timer();
public GamePanel(){
try {
srcImg = ImageIO.read(new URL("http://i.stack.imgur.com/6TJPy.png"));
} catch (Exception e) {
e.printStackTrace();
}
backgroundImg = new BufferedImage(8777, 1024, BufferedImage.TRANSLUCENT);
Graphics2D g2 = backgroundImg.createGraphics();
g2.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.drawImage(srcImg, 0, 0,8777, 1024, null);
g2.dispose();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
repaint();
}
}, 1000/30, 1000/30);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
frame++;
//g.drawImage(backgroundImg, frame%backgroundImg.getWidth(),0, this);
g.drawImage(backgroundImg, 0, 0, 600, 1024, frame, 0, frame+this.getWidth(), backgroundImg.getHeight(),null);
g.setColor(Color.WHITE);
g.drawString("" + frame, 20, 50);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600,1024);
}
public static void main(String[] args){
GamePanel panel = new GamePanel();
JFrame f = new JFrame();
//f.setBounds(0,0,400,400);
f.getContentPane().add(panel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
编辑:我发现这种方式减少了cpu但只减少了大约20%,我希望将其降低到仅使用0 - 10%cpu,就像我没有显示如此大的图像一样
我的问题是绘制缓冲图像的最有效的方法是什么。
答案 0 :(得分:2)
尝试从代码中形成的这个SSCCE,看看它是否表现得像你期望的那样。这似乎很快:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
public class GamePanel extends JPanel implements Runnable{
int frame = 0;
BufferedImage srcImg;
BufferedImage backgroundImg;
public GamePanel(){
try {
srcImg = ImageIO.read(new URL("http://i.stack.imgur.com/6TJPy.png"));
} catch (Exception e) {
e.printStackTrace();
}
backgroundImg = new BufferedImage(8777, 1024, BufferedImage.TRANSLUCENT);
Graphics2D g2 = backgroundImg.createGraphics();
g2.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.drawImage(srcImg, 0, 0,8777, 1024, null);
g2.dispose();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
frame--;
g.drawImage(backgroundImg, frame%backgroundImg.getWidth(),0, this);
g.setColor(Color.WHITE);
g.drawString("" + frame, 20, 50);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600,1024);
}
@Override
public void run() {
while(true){
repaint();
try {
Thread.sleep(1000/30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
GamePanel panel = new GamePanel();
Thread thread = new Thread(panel);
JFrame f = new JFrame();
//f.setBounds(0,0,400,400);
f.getContentPane().add(panel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
thread.start();
}
}
以下是使用基于Swing的Timer
的等效项。好吧,不是更好,但仍然是更好的练习。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
public class GamePanel extends JPanel {
int frame = 0;
BufferedImage srcImg;
BufferedImage backgroundImg;
public GamePanel(){
try {
srcImg = ImageIO.read(new URL("http://i.stack.imgur.com/6TJPy.png"));
} catch (Exception e) {
e.printStackTrace();
}
backgroundImg = new BufferedImage(8777, 1024, BufferedImage.TRANSLUCENT);
Graphics2D g2 = backgroundImg.createGraphics();
g2.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.drawImage(srcImg, 0, 0,8777, 1024, null);
g2.dispose();
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
repaint();
}
};
Timer timer = new Timer(1000/30, al);
timer.start();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
frame--;
g.drawImage(backgroundImg, frame%backgroundImg.getWidth(),0, this);
g.setColor(Color.WHITE);
g.drawString("" + frame, 20, 50);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600,1024);
}
public static void main(String[] args){
GamePanel panel = new GamePanel();
JFrame f = new JFrame();
f.getContentPane().add(panel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}