gpt4 book ai didi

java - 不从 List 重新绘制的图像

转载 作者:行者123 更新时间:2023-11-29 05:39:25 25 4
gpt4 key购买 nike

我正在尝试从指定路径加载图像,文件列表存储在 List<> 中。第一次当我初始化它显示的图像但是当我试图显示来自包含文件列表的列表实例的下一张图像时,它不会重新绘制图像。出了什么问题是我第一次在构造函数中初始化图像 overwrite新图像,现在第一次在构造函数之外初始化图像的位置我不知道。

我的代码:

 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);

类代码和构造函数:

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();
}
}

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

any suggestion ?

最佳答案

我想我有适合您的完美解决方案!我为您编写了一个小程序,这样更容易理解。

首先我有一个方法可以让你检查文件是否是图片:

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;
}

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

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

最后,我将每个文件压入堆栈。记住将堆栈填充到一个变量中,不要多次读取文件,因为你会遇到和以前一样的问题:

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

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

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 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 方法执行您的程序!

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

总结

基本上你需要一些东西来迭代,因为像整数这样的值没有按照你喜欢的方式保存。在我的示例中,我使用了 Stack 并在开始时将所有图片保存在其中。重要的是,如果您使用或完成了图片,则必须将其删除(对堆栈使用 stack.pop())。我没有找到一种方法来检查文件是否是图片(如果是 ImageIO 捕获它是错误的)。我为此编写了一个方法,如果您愿意,可以使用它。

关于java - 不从 List<path> 重新绘制的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com