Java - paintComponent()在重新绘制高分辨率图像时大大减慢了程序的速度

时间:2017-03-29 20:23:11

标签: java swing paintcomponent

最近我一直在做一个小项目,直到遇到一些令人烦恼的事情。

程序当前所做的是读取程序生成的某些文件夹,其中显示文件扩展名为“.jpg”和“.png”的文件(为此,用户必须手动插入这些文件夹中的图像。)

该部分有效,直到我们到达我创建的动画部分。这是一个具有某个RGBA值的矩形,一旦鼠标悬停在它上面并且退出它就会改变其alpha值。使用低分辨率图像,此动画可以流畅地运行,但是对于高分辨率图像,它非常滞后,动画看起来不那么平滑。

mainFrame类

package seriesorganiser;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Luca Muscat
 */
public class mainFrame {

    private static boolean check;
    private static final String DOCUMENTS_LOCATION = System.getProperty("user.home") + "\\Documents";

    public static void main(String[] args) throws IOException {
        check = new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser").exists();
        if (!check) {
            new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser").mkdir();
            new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser\\Shows").mkdir();
            new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser\\Movies").mkdir();
        }
        JFrame frame = new JFrame("Series Organiser");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        //Layout Handler
        JPanel container = new JPanel();
        //Movies=0, Shows=1.
        JPanel moviesPanel = new moviesShowsPanel(0);
        JPanel showsPanel = new moviesShowsPanel(1);

        container.setLayout(new GridLayout(1, 2));
        container.add(moviesPanel);
        container.add(showsPanel);

        frame.add(container);
        frame.pack();
    }
}

moviesShowsPanel

package seriesorganiser;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author Luca Muscat
 */
public class moviesShowsPanel extends JPanel {

    int alpha = 255 / 2;
    boolean isMouseOver = false;
    boolean isMouseHover = false;
    public final static int SHOWS_GENRE = 1;
    public final static int MOVIES_GENRE = 0;
    private int genreHolder;
    public moviesShowsPanel(int genre) {
        genreHolder=genre;
        setPreferredSize(new Dimension(600, 600));
        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.LIGHT_GRAY);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent me) {
                if(!isMouseOver)repaint();
                isMouseOver = true;
            }

            @Override
            public void mouseExited(MouseEvent me) {
                if(isMouseOver)repaint();
                isMouseOver = false;
            }
        });

    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        scanForImage findImage = new scanForImage();
        if (isMouseOver && alpha>10) {
            alpha-=10;      
        }
        if(!isMouseOver && alpha<145){
            alpha+=10;
        }
        try {
            if(genreHolder==MOVIES_GENRE){
                g.drawImage(findImage.findMoviesImage(), 0, 0, getWidth(), getHeight(), null);
            }
            if(genreHolder==SHOWS_GENRE){
                g.drawImage(findImage.findShowsImage(),0,0,getWidth(),getHeight(),null);
            }
        } catch (IOException ex) {
            Logger.getLogger(moviesShowsPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        g.setColor(new Color(214, 229, 255, alpha));
        g.fillRect(0, 0, getWidth(), getHeight());
        repaint();
    }
}

scanForImage

package seriesorganiser;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * 
 * @author Luca Muscat
 */
public class scanForImage {
    private static final String DOCUMENTS_LOCATION = System.getProperty("user.home")+"\\Documents";
    BufferedImage findMoviesImage() throws IOException{
        BufferedImage img = null;
        File folderMovies = new File(DOCUMENTS_LOCATION+"\\SeriesOrganiser\\Movies");
        img=DRY(folderMovies);
        return img;
    }
    BufferedImage findShowsImage()throws IOException{
       BufferedImage img=null;
       File folderShows = new File(DOCUMENTS_LOCATION+"\\SeriesOrganiser\\Shows");
       img = DRY(folderShows);
       return img;
    }
    private BufferedImage DRY(File f)throws IOException{
        BufferedImage img=null;
        String filepath=String.valueOf(f);
        File[] listOfFiles=f.listFiles();
        for(File c:listOfFiles){
           if(c.isFile()){
               if(c.getName().contains(".jpg") || c.getName().contains(".png")){
                   img=ImageIO.read(new File(filepath+"\\"+c.getName()));
                   break;
               }
           }
       }
        return img;
    }
}

要测试它,您需要做的就是在“Documents”文件夹库中的一个名为SeriesOrganiser的文件夹中的电影和显示文件中放置一个图像。 (Windows 10)。

不会介意对代码的一些批评,因为我知道我有很多需要改进的地方。

1 个答案:

答案 0 :(得分:3)

paintComponent()方法的一些注释:

scanForImage findImage = new scanForImage();

不要在绘画方法中进行I / O.绘画方法仅用于绘画

repaint();

不要调用repaint()。这将导致绘制请求不断添加到事件队列中。

如果您需要动画,请使用Swing Timer来安排动画。

g.dispose();

不要丢弃传递给方法的Graphics对象。框架上的其他组件可能使用此图形。

您只能在dispose()方法中创建的Graphic对象上使用paintComponent()方法。