【问题标题】:How to make an image gallery with java如何用java制作图片库
【发布时间】:2012-04-23 04:52:45
【问题描述】:

对于课程,我正在开发我的第一个 GUI 应用程序。它只是一个简单的图像查看器,有四个按钮:上一个、下一个、停止、播放。上一个和下一个工作正常,但老实说,我什至不知道如何开始处理幻灯片部分(播放和停止)。我知道有一个计时器类可能很方便控制图像变化时的速度……但我不确定通常使用哪种逻辑来循环浏览图像。谁能指出我正确的方向,我的大脑在这一点上有点炸:0

我在下面包含了我的代码。我是新手,所以希望人们不会对我的技术太挑剔。如果重要的话,我正在 eclipse 中工作。

这是我目前的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;


public class ImageGallery extends JFrame
{
    private ImageIcon myImage1 = new ImageIcon ("Chrysanthemum.jpg");
    private ImageIcon myImage2 = new ImageIcon ("Desert.jpg");
    private ImageIcon myImage3 = new ImageIcon ("Jellyfish.jpg");
    private ImageIcon myImage4 = new ImageIcon ("Penguins.jpg");
    JPanel ImageGallery = new JPanel();
    private ImageIcon[] myImages = new ImageIcon[4];
    private int curImageIndex=0;

    public ImageGallery ()
        {   
            ImageGallery.add(new JLabel (myImage1));
            myImages[0]=myImage1;
            myImages[1]=myImage2;
            myImages[2]=myImage3;
            myImages[3]=myImage4;

            add(ImageGallery, BorderLayout.NORTH);

            JButton PREVIOUS = new JButton ("Previous");
            JButton PLAY = new JButton ("Play");
            JButton STOP = new JButton ("Stop");
            JButton NEXT = new JButton ("Next");

            JPanel Menu = new JPanel();
            Menu.setLayout(new GridLayout(1,4));
            Menu.add(PREVIOUS);
            Menu.add(PLAY);
            Menu.add(STOP);
            Menu.add(NEXT);

            add(Menu, BorderLayout.SOUTH);

            //register listener
            PreviousButtonListener PreviousButton = new PreviousButtonListener ();
            PlayButtonListener PlayButton = new PlayButtonListener ();
            StopButtonListener StopButton = new StopButtonListener ();
            NextButtonListener NextButton = new NextButtonListener ();

            //add listeners to corresponding componenets 
            PREVIOUS.addActionListener(PreviousButton);
            PLAY.addActionListener(PlayButton);
            STOP.addActionListener(StopButton);
            NEXT.addActionListener(NextButton);

        }

    public static void main (String [] args)
        {
            ImageGallery frame = new ImageGallery();

            frame.setSize(490,430);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
        }



    class PreviousButtonListener implements ActionListener 
    {

        public void actionPerformed(ActionEvent e)
            {
                if(curImageIndex>0 && curImageIndex <= 3)
                    {   ImageGallery.remove(0);
                        curImageIndex=curImageIndex-1;
                        ImageIcon TheImage= myImages[curImageIndex];
                        ImageGallery.add(new JLabel (TheImage));
                        ImageGallery.validate();
                        ImageGallery.repaint(); 
                    }
                else 
                    {   
                        ImageGallery.remove(0);
                        ImageGallery.add(new JLabel (myImage1));
                        curImageIndex=0;
                        ImageGallery.validate();
                        ImageGallery.repaint();
                    }
            }
    }

    class PlayButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
            {
                        // *need help here*//

            }
    }

    class StopButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
            {
                        // *need help here*//
            }
    }

    class NextButtonListener implements ActionListener 
    {


        public void actionPerformed(ActionEvent e)
        {

            if(curImageIndex>=0 && curImageIndex < 3)
                {   ImageGallery.remove(0);
                    curImageIndex = curImageIndex + 1;
                    ImageIcon TheImage= myImages[curImageIndex];
                    ImageGallery.add(new JLabel (TheImage));
                    ImageGallery.validate();
                    ImageGallery.repaint(); 
                }
            else 
                {   
                    ImageGallery.remove(0);
                    ImageGallery.add(new JLabel (myImage4));
                    curImageIndex=3;
                    ImageGallery.validate();
                    ImageGallery.repaint();
                }

        }
    }
}

【问题讨论】:

  • 永远不要在 Swing 中使用 java.util.Timer,尽可能避免使用它,因为通过它完成的 Swing 更新将成为您的一大难题,因为您必须在 EDT 上进行这些更新 -事件调度程序线程。试试看javax.swing.Timer,这里有个小程序供大家帮忙SlideShow

标签: java swing image-gallery


【解决方案1】:

为什么要把简单的事情复杂化,

【讨论】:

  • 不确定CardLayout 是否是这里的最佳选择。这意味着您将所有图像都保存在内存中,并且根据您要查看的图像的大小/数量,可能会消耗大量内存。在这种情况下,我会选择 BorderLayout 并始终替换图像
  • @Robin 有 1) 已加载图像的内存 2) Graphics2D 内存 3) 我错过的东西,第二个结论。点 AFAIK 仅当前可见,无论渲染 Graphics2D,
  • 同意在他的 SSCCE 中他已经为图像分配了内存。一般来说,我更倾向于让图像查看器循环浏览整个图像集,CardLayout 不是 IMO 的最佳选择
  • hmmm 很好的业务概念,问题,然后在这里发布关于 SwingWorker 和 java.util.Queue for Icons 的答案,
【解决方案2】:

这个example 显示了一个控制javax.swing.Timer 的开始/停止按钮。而不是每次替换标签,只需更新标签的Icon,正如@mKorbel 所建议并显示here

【讨论】:

    【解决方案3】:

    您需要为幻灯片使用线程。您可以在 run 方法中使用一个标志来继续显示或在此标志更改时停止,例如布尔变量。您可以在http://java.sun.com/developer/technicalArticles/Threads/applet/ 中看到一个示例。

    【讨论】:

      【解决方案4】:

      以下是一些可以帮助您入门的指南:

      首先,您需要一个单独的线程来控制不断变化的图像。我建议你写一个实现TimerTask 的类。覆盖此类中的 run() 方法。在此运行方法中,您应该将功能用于更改当前显示的图像(类似于您在下一个和上一个功能中所做的)。

      在播放按钮的 actionPerformed() 方法中,您需要创建 Timer 类的实例并使用 scheduleAtFixedRate(TimerTask task, long delay, long period) 方法启动计时器(此类中的其他方法可能是虽然也使用了 scheduleAtFixedRate() 似乎更合适)。

      对于停止,您需要添加足够的功能来使用 Timer 类中的 cancel() 方法停止正在运行的计时器

      【讨论】:

      • 对于 Swing 相关的操作,最好使用 Swing 计时器。因为所有与 Swing 相关的操作都应该在 EDT 上发生,而 Swing 计时器会为您处理这些事情
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      相关资源
      最近更新 更多