0

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

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

    }
}
4

1 回答 1

0

以下是您需要采取的一些步骤才能开始:

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

完成该帖子后,如果您仍然卡住,则发布另一个更具体的问题。您目前距离最终目标还很遥远,所以现在只需专注于响应用户事件(单击您的按钮)即可。

请参阅开始。

于 2011-05-01T18:24:06.250 回答