Java图像查看器

时间:2011-05-01 18:02:55

标签: java random random-sample image-viewer

我正在尝试创建一个图像查看器,其想法是查看器将弹出一个图像随机按钮和一个重置按钮,让用户单击该按钮并随机循环浏览不同图像的列表。我可以打开观众但不能让观众旋转图像。这是代码。我将不胜感激任何帮助

import java.awt.*;
import javax.swing.*;


public class CreateImage extends JFrame {
    private JButton jbtRandom = new JButton("Random");
    private JButton jbtReset = new JButton ("Reset");

   public CreateImage() {

        JPanel panel = new JPanel();
        panel.add(jbtRandom);
        panel.add(jbtReset);


        Image image1 = new ImageIcon("kobe.jpg").getImage();
        Image image2 = new ImageIcon("joe.jpg").getImage();
        Image image3 = new ImageIcon("sidney.jpg").getImage();
        Image image4 = new ImageIcon("bugs.gif").getImage();
        Image image5 = new ImageIcon("mac.jpg").getImage();
        Image image6 = new ImageIcon("snooki.jpg").getImage();

        setLayout(new GridLayout(2, 0, 5, 5));
        add(new ImageViewer(image1));


        /*add(new ImageViewer(image2));// <== extra lines form first viewer attempt  
         add(new ImageViewer(image3)); //, <== which showed all images at once.
         add(new ImageViewer(image4));// <== only need one image and to flip 
         add(new ImageViewer(image5));// <== to a random image
         add(new ImageViewer(image6));// <==      */
    }

    public class ImageViewer extends JPanel {
        private java.awt.Image image;
        private boolean stretched = true;
        private int xCoordinate;
        private int yCoordinate;
        public ImageViewer() {

        }
        public ImageViewer(Image image) {
            this.image = image;
       }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image != null)
                if (isStretched())
                    g.drawImage(image, xCoordinate, yCoordinate, getWidth(), getHeight(), this);
                else
                    g.drawImage(image, xCoordinate, yCoordinate, this);
        }
        public java.awt.Image getImage() {
        return image;
        }

        public void setImage(java.awt.Image image) {
            this.image = image;
            repaint();
        }

        public boolean isStretched() {
            return stretched;
        }

        public void setStretched(boolean stretched) {
            this.stretched = stretched;
            repaint();
        }

        public int getXCoordinate() {
            return xCoordinate;
        }

        public void setXCoodinate(int xCoordinate) {
            this.xCoordinate = xCoordinate;
        }

        public int getYCoordinate() {
            return xCoordinate;
        }

        public void setYCoodinate(int yCoordinate) {
            this.yCoordinate = yCoordinate;
            repaint();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new CreateImage();
        frame.setTitle("Random Image-Click The Button");
        frame.add(new JButton("Random"));
        frame.add(new JButton("Reset"));
        frame.setSize(400, 320);
        frame.setLocationRelativeTo(null); //Center Frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

1 个答案:

答案 0 :(得分:0)

以下是您需要采取的一些步骤:

1)而不仅仅是创建和丢弃图像。把它们放在某个地方 - 比如列表或地图。 2)向Random按钮添加事件处理程序。 3)单击该按钮,从列表或地图中选择并显示新图像。

如果您仍然卡住了,那么在您发布这个帖子之后会有另一个更具体的问题。你现在距离最终目标还很远,所以现在只需要专注于响应用户事件(点击你的按钮)就可以了。

请参阅this开始使用。