0

私は画像ビューアを作成しようとしています.ビューアは1つの画像、ランダムボタン、リセットボタンをポップアップして、ユーザーがボタンをクリックして異なる画像のリストをランダムに循環できるようにするという考えです. ビューアを開くことはできますが、ビューアで画像を回転させることはできません。これがコードです。どんな助けにも感謝します

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 に答える