图片未从List< path>重新绘制 [英] Image in not repainting from List<path>

查看:146
本文介绍了图片未从List< path>重新绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从指定路径加载图像,文件列表存储在List<>中。第一次初始化图像时显示
但是当我尝试显示包含文件列表的列表实例的下一张图像时,不会重新绘制图像
出错的是我第一次在构造函数中初始化图像覆盖
新图像,现在 首次在构造函数外部初始化图像 我不知道。

i am trying to load image from specified path, and list of files are stored inside List<>. at first time when i initialize image it display but when i am trying to display next image from List instance which contain list of files, it doesn't repaint the image. what's wrong going is i am initializing image in constructor first time that overwrite the new image, now where to initialize image first time outside constructor i don't know.

我的代码:

 public void nextImage(int cnt)
    {                   
        System.out.println(cnt);

        if (cnt < imageFiles.size()) 
        {
            System.out.println(imageFiles.size());
            try 
            {                   
                bg = ImageIO.read(new File((imageFiles.get(cnt)).toString()));

                scaled = getScaledInstanceToFit(bg, new Dimension(600, 600));
                setBackground(Color.BLACK);

            } 
            catch(Exception e) 
            {
                e.printStackTrace();
            }
        }

        MouseHandler handler = new MouseHandler();
        addMouseListener(handler);
        addMouseMotionListener(handler);           
        System.out.println(cnt);            
        System.out.println(imageFiles.get(cnt).toString());         
    }

菜单项点击代码:

JMenuItem mntmRestoreImage = new JMenuItem("Next Image");

        final ImagePane st = new ImagePane();           

        mntmRestoreImage.addActionListener(new ActionListener() 
        {                       
        @Override
            public void actionPerformed(ActionEvent arg0) 
            {               
               st.nextImage(k);
               k++;     
            }
        });

        mnEdit.add(mntmRestoreImage);

班级代码&构造函数:

class code & constructor :

private BufferedImage bg;
        private BufferedImage scaled;
java.util.List<Path> imageFiles= getFilesFromDirectory(FileSystems.getDefault().getPath("D:\\New folder")); 


 public ImagePane() 
        {
try 
            {
                bg = ImageIO.read(getClass().getResource("/images/src11.jpg"));
                scaled = getScaledInstanceToFit(bg, new Dimension(600, 600));
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}




计数器也递增,现在ImagePane()构造函数
中的代码覆盖了nextImage()函数的图像,所以想想这个代码中发生了什么?b $ b

counter also increments, now the code inside ImagePane() constructor overwrites the image of nextImage() function, so idea what happen out in this code ??

任何建议?


推荐答案

我想我有完美的解决方案!我为你写了一个小程序,所以它更容易理解。

I think I have the perfect solution for you! I wrote a little program for you so it is easier to understand.

首先,我有一种方法可以检查文件是否是图片:

First I have a method for you to check if the file is a picture:

public Stack<File> getFilesInFolder(String startPath) {
    File startFolder = new File(startPath);
    Stack<File> picturestack = new Stack<File>();

    String extension;
    int dotindex;

    // Go through the folder
    for (File file : startFolder.listFiles()) {
        extension = "";
        dotindex = file.getName().lastIndexOf('.'); // Get the index of the dot in the filename

        if (dotindex > 0) {
            extension = file.getName().substring(dotindex + 1);

            // Iterate all valid file types and check it
            for (String filetype : validpicturetypes) {
                if (extension.equals(filetype)) {
                    picturestack.add(file);
                }
            }
        }
    }
    return picturestack;
}

非常简单!拿起文件夹并迭代他的文件。获取文件的扩展名并检查它是否是有效的文件类型。在代码开头定义数组中的文件类型。

Very easy! Take the folder and iterate his files. Take the extension of the file and check if it is a valid file type. Define the file types in a array at the begining of your code.

String[] validpicturetypes = {"png", "jpg", "jpeg", "gif"};

最后,我将每个文件推入堆栈。请记住将堆栈填充到变量中,不要多次读取文件,因为您遇到的问题与以前相同:

At the end I push every file into a stack. Remember to fill the stack into a variable, do not read the files more than once because than you get the same problem as before:

Stack<File> pictures = getFilesInFolder("C:\\Users\\Admin\\Desktop");

之后为你的JMenuItem使用一个Action!在我的例子中,我没有太多,你必须把你的方法放进去!

After that use a Action for your JMenuItem! In my example I do not have much, you have to put your methods in!

Action nextpictureaction = new AbstractAction("Next Picture") {
    private static final long serialVersionUID = 2421742449531785343L;

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!pictures.isEmpty()) {
            System.out.println(pictures.pop().getName());
        }
    }
};

在JMenu中添加Action并设置Frame的属性。

Add the Action at your JMenu and set the properties of your Frame.

/*
 * Add Components to Frame
 */
setJMenuBar(menubar);
menubar.add(toolsmenu);
toolsmenu.add(nextpictureaction);

/*
 * Frame Properties
 */
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setSize(1000, 700);
setTitle("PictureEditor");
setVisible(true);

最后用invokeLater方法执行你的程序!

At the end execute your program with the invokeLater method!

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new PictureEditor();
        }
    });
}



摘要



基本上你需要一个迭代的东西,因为像整数这样的值不会以你喜欢的方式保存。在我的例子中,我使用了一个堆栈并在其中保存所有图片。重要的是,如果您使用或完成了图片,则必须将其删除(对堆栈使用stack.pop())。我没有找到一种方法,你检查文件是否是一张图片(如果它是ImageIO捕获它是坏的)。如果你想要你可以使用它,我写了一个方法。

这篇关于图片未从List&lt; path&gt;重新绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆