Java:我怎样才能更快地绘制这些图像?

Java:我怎样才能更快地绘制这些图像?,java,image,graphics,Java,Image,Graphics,好的,我正在制作一个游戏,在游戏中,用户将一个图像(在我的代码中称为“地图”)移动到角色的后面,这样看起来就像角色在移动一样 当程序运行时,会有适度的性能下降,背景图像(“贴图”)会滞后,并且不总是重新绘制 下面是我的主类调用的一个类,用于绘制图片: public graphics(int z,int s, int f) { x = z; y = s; d = f; try { map = ImageIO.read(new File("background

好的,我正在制作一个游戏,在游戏中,用户将一个图像(在我的代码中称为“地图”)移动到角色的后面,这样看起来就像角色在移动一样

当程序运行时,会有适度的性能下降,背景图像(“贴图”)会滞后,并且不总是重新绘制

下面是我的主类调用的一个类,用于绘制图片:

public graphics(int z,int s, int f)
{
    x = z;
    y = s;
    d = f;
    try {
    map = ImageIO.read(new File("background.png"));
    //br = ImageIO.read(new File("blackrectangle.jpg"));
    s0 = ImageIO.read(new File("spriteshoot0.png"));
    s10 = ImageIO.read(new File("spriteshoot10.png"));
    s20 = ImageIO.read(new File("spriteshoot20.png"));
    s30 = ImageIO.read(new File("spriteshoot30.png"));
    s40 = ImageIO.read(new File("spriteshoot40.png"));
    s50 = ImageIO.read(new File("spriteshoot50.png"));
    s60 = ImageIO.read(new File("spriteshoot60.png"));
    s70 = ImageIO.read(new File("spriteshoot70.png"));
    s80 = ImageIO.read(new File("spriteshoot80.png"));
    s90 = ImageIO.read(new File("spriteshoot90.png"));
    s100 = ImageIO.read(new File("spriteshoot100.png"));
    s110 = ImageIO.read(new File("spriteshoot110.png"));
    s120 = ImageIO.read(new File("spriteshoot120.png"));
    s130 = ImageIO.read(new File("spriteshoot130.png"));
} catch (IOException e) {}
}
public void moveZ(int a,int b)
{
    xz+=a;
    yz+=b;
}

public void move(int a,int b)
{
    x+=a;
    y+=b;
}

public void angle(int t)
{
    q=t;

}



public void paintComponent(Graphics g) {

        g.drawImage(map,x,y,this);

        if (q==1)
        g.drawImage(s0,599,340,null);

        if (q==2)
        g.drawImage(s10,599,340,null);

        if (q==3)
        g.drawImage(s20,599,340,null);

        if (q==4)
        g.drawImage(s30,599,340,null);

        if (q==5)
        g.drawImage(s40,599,340,null);

        if (q==6)
        g.drawImage(s50,599,340,null);

        if (q==7)
        g.drawImage(s60,599,340,null);

        if (q==8)
        g.drawImage(s70,599,340,null);

        if (q==9)
        g.drawImage(s80,599,340,null);

        if (q==10)
        g.drawImage(s90,599,340,null);

        if (q==11)
        g.drawImage(s100,599,340,null);

        if (q==12)
        g.drawImage(s110,599,340,null);

        if (q==13)
        g.drawImage(s120,599,340,null);

        if (q==14)
        g.drawImage(s130,599,340,null);
   }}
因此,我想知道如何才能更快地显示图像“地图”,或者找到解决此性能问题的替代路线

注意**这段代码是作为我的主类中的一个对象创建的,并不是每次执行时都从文件中读取缓冲区映像(据我所知)。还有,重新绘制();方法在我的程序中每16毫秒调用一次(是的,我已经尝试增加这一时间,但问题仍然存在)。以下是用于重新绘制帧的计时器片段:

TimerTask任务=新的TimerTask(){


非常感谢您提供的任何帮助。

请考虑将重新绘制(…)限制在需要重新绘制的GUI的小区域内。同样,不要在paint或paintComponent方法中处理JVM传递给您的图形对象。只处理您自己创建的图形对象

编辑1

如果图形的性能是关键,您最好离开Swing,转到一个为游戏、动画和图形优化的库。

考虑限制重新绘制(…)到GUI中需要重新绘制的小区域。同样,不要在paint或paintComponent方法中处理JVM传递给您的图形对象。只处理您自己创建的图形对象

编辑1

如果图形性能是关键,您最好离开Swing,搬到一个为游戏、动画和图形优化的库中。

您可以通过ResourceBundle缓存常用的图像。以下代码提供了对存储在jar中的图像的缓存访问,或以绝对路径作为图像图标的图像的缓存访问将图像导入哈希表。对同一图像的后续调用将是查找。此类必须在“默认”包下设置:


您可以通过ResourceBundle缓存常用图像。以下代码提供对存储在jar中或绝对路径中作为ImageIcons的图像的缓存访问。首次使用将图像存储到哈希表中。对同一图像的后续调用将是查找。此类必须在“default”包下设置:


我在容器中的交互式移动JComponent出现性能问题,发现在父组件上而不是在移动组件上调用repaint()会大大提高速度。我不知道为什么-有人知道吗?

我在容器中的交互式移动JComponent出现性能问题,并发现调用repaint()会导致速度大幅提高在父代上,而不是在移动组件上,速度有了巨大的提高。我不知道为什么-有人知道吗?

如果你在编写游戏,你应该使用游戏引擎。编写高性能的游戏是很困难的,暴力重画屏幕上的所有内容都不会减少速度。其他聪明人已经解决了这些问题,你可以利用他们累积的工作,而不是试图重新发明轮子


我不是游戏引擎方面的专家,但我是google上第一个热门的“java游戏引擎”是的。也许你应该试试。

如果你在写游戏,你应该使用游戏引擎。写高性能的游戏很难,用蛮力重画屏幕上的一切都不会解决问题。其他聪明人已经解决了这些问题,你可以利用他们累积的工作,而不是试图重新发明轮子


我不是游戏引擎方面的专家,但谷歌上“java游戏引擎”的第一个亮点是。也许你应该试试。

好的,我已经删除了dispose();方法,但是背景图像显示在整个JPanel中,每次移动都需要重绘,所以我不能限制重绘()方法设置为某些参数,因为整个JPanel需要更新。是的,我将尝试一个游戏库以获得高速性能,谢谢。好的,我已经删除了dispose();方法,但是背景图像显示在整个JPanel中,需要在每次移动时重新绘制,因此我无法限制重新绘制()方法设置为某些参数,因为整个JPanel需要更新。是的,我将尝试一个游戏库以获得高速性能,谢谢。我尝试过这个方法,但在重新绘制容器和重新绘制框架的性能上没有看到任何差异。我尝试过这个方法,但在重新绘制的性能上没有看到任何差异容器并重新绘制框架。好的,我在代码中实现了这一点,但没有性能改进。这可能是因为这种想法读取图像的速度更快,而不是绘制/重新绘制图像的速度更快,这是最大的性能问题。通过安装自己的重新绘制,使用javax.swing.RepainManager如何t manager?好的,我在我的代码中实现了这一点,但没有性能改进。这可能是因为这种想法读取图像的速度更快,而不是更快地绘制/重新绘制图像,这是最大的性能问题。通过安装自己的重绘管理器来使用javax.swing.RepaitManager如何?您尝试过Doub吗缓冲?不,我以为摆动会自动加倍Buffers@user1030690:它会自动双缓冲。永远不要在paint或paintComponent方法中处理JVM传递给您的图形对象。只处理您自己创建的图形对象。您尝试过双缓冲吗?不,我以为swing会自动双缓冲Buffers@user1030690: 它会自动将缓冲区加倍。永远不要在paint或paintComponent方法中处理JVM传递给您的图形对象。只处理您自己创建的图形对象。
    public void run()       {

        if (onU)
            g.move(0,3);

        if (onL)
            g.move(3,0);

        if (onD)
            g.move(0,-3);

        if (onR)
            g.move(-3,0);
        //System.out.println(ze.getX(650,400,650,0,xZ,yZ));
        //System.out.println(ze.getY(650,400,650,0,xZ,yZ));
        if(onR||onL||onD||onU)
        gameSurface.repaint();
    }};
import java.util.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.*;

import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * ImageBundle is a ResourceBundle that retrieves the content of an image from an image file with supported extension.
 */
public class ImageBundle extends ResourceBundle {
    private static final String RESOURCE = "MyApp";
    private String __fileSuffix;
    /**
     * @uml.property  name="__KEYS"
     */
    private static final Vector<String> __KEYS;
    private static Hashtable<String, ImageIcon> __TABLE;

    static {
        __KEYS  = new Vector<String>();
        __KEYS.addElement(RESOURCE);
        __TABLE = new Hashtable<String, ImageIcon>();
    }

    /**
     * Load image file stored in a JAR classpath
     * @param imageName the filename of the image
     * @return the ImageIcon of the image file
     */
    private ImageIcon __loadImageFromJar(String imageName) {
        String path = ""; //define the file path of the image file in imageName itself

        String imagePath = path + imageName + __fileSuffix;
        ImageIcon icon;
        URL url;

        icon = (ImageIcon)__TABLE.get(imageName);

        if(icon != null)
            return icon;

        url = ImageBundle.class.getResource(imagePath);

        icon = new ImageIcon(url);
        __TABLE.put(imageName, icon);

        return icon;
    }

    /**
     * Load image file stored in a given path
     * @param imageName the filename of the image
     * @return the ImageIcon of the image file
     */
    @SuppressWarnings("unused")
    private ImageIcon __loadImageFromExternalSource(String imageName) {
        String path = System.getenv("MY_APP_HOME_PATH");

        String imagePath = ((path != null) ? (path + imageName + __fileSuffix) : (imageName + __fileSuffix));
        ImageIcon value;

        value = (ImageIcon)__TABLE.get(imageName);

        if(value != null){
            //Outils.debugMessage("Cached " + imagePath + "> " + imageName + ": " + value);
            return value;
        }
        else {
            //Outils.debugMessage("New " + imagePath + "> " + imageName + ": " + value);
        }

        ImageIcon property = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(imagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        property = new ImageIcon(image);

        value = property;
        __TABLE.put(imageName, value);

        return value;
    }

    protected ImageBundle(String suffix) {
        __fileSuffix = suffix;
    }

    public ImageBundle() {
        this("");
    }

    /**
     * @return
     * @uml.property  name="__KEYS"
     */
    public Enumeration<String> getKeys() {
        return __KEYS.elements();
    }

    protected final Object handleGetObject(String key) {
        return __loadImageFromJar(key);
    }
}
ResourceBundle rsc = ResourceBundle.getBundle("ImageBundle"); //call rsc only once
...
ImageIcon icon = (ImageIcon) rsc.getObject("picture/plus.png");