Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 格式化JPanel以包含ArrayList<;JComponent>;并根据单击的视图模式拆分视图_Java_Image_Swing - Fatal编程技术网

Java 格式化JPanel以包含ArrayList<;JComponent>;并根据单击的视图模式拆分视图

Java 格式化JPanel以包含ArrayList<;JComponent>;并根据单击的视图模式拆分视图,java,image,swing,Java,Image,Swing,我正试图通过JavaSwing创建这个基本的照片编辑应用程序。我有我的代码工作时,有些图片导入;我有3个视图-照片视图(仅显示照片)、缩略图视图(应显示图片的缩略图)和拆分视图(应为BorderLayout.CENTER中照片和BorderLayout.SOUTH中缩略图的组合)。我已经在图片和代码摘录中解释了为什么它不能按应有的方式工作。我不能上传任何图片,但希望摘录能提供更多细节 拆分视图相关摘录: public void changeMode(boolean p, boolean

我正试图通过JavaSwing创建这个基本的照片编辑应用程序。我有我的代码工作时,有些图片导入;我有3个视图-照片视图(仅显示照片)、缩略图视图(应显示图片的缩略图)和拆分视图(应为BorderLayout.CENTER中照片和BorderLayout.SOUTH中缩略图的组合)。我已经在图片和代码摘录中解释了为什么它不能按应有的方式工作。我不能上传任何图片,但希望摘录能提供更多细节

拆分视图相关摘录:

    public void changeMode(boolean p, boolean b, boolean s){
        /*
         * Photo View will display a single PhotoComponent2 in a large area. 
         */
        isPhoto = p;
        if (isPhoto){
            //have a child JPanel set as CENTER component of BorderLayout of the JScrollPane (scroll) 
            childPhoto = new JPanel();
            childPhoto.add(displayPhotos.get(getCurrentPhoto()), BorderLayout.CENTER);
            System.out.println("in lc photo view class");
        }

        /*
         * Browser View will hold all the images.
         */
        isBrowse = b;
        if(isBrowse){
            //have a child JPanel set as CENTER component of BorderLayout to hold grid of thumbnails within
            // JScrollPanel (scroll)
            this.removeAll();
            childBrowse = new JPanel();
            tc2 = new ThumbnailComponent(displayPhotos.get(getCurrentPhoto()));
            childBrowse.setLayout(new WrapLayout());
            childBrowse.add(tc2);
//          for(int i = 0; i < displayThumbs.size(); i++){
//              childBrowse.add(displayThumbs.get(i));
//              System.out.println(displayThumbs.get(i));
//          }

            System.out.println("in lc browser view class");
        }

        /*
         * Split View is a combination of Photo and Browser View in that the top half
         * is Photo View and the bottom half is Browser View.
         */
        isSplit = s;
        if(isSplit){
            //have a child JPanel in CENTER to hold PhotoComponent plus a child JPanel in SOUTH to hold thumbnails
            containsAll = new JPanel();
            containsAll.setLayout(new BorderLayout());
            containsAll.add(childPhoto, BorderLayout.CENTER);
            containsAll.add(childBrowse, BorderLayout.SOUTH);
            System.out.println("in lc split view class");
        }
    }
光电元件:

public PhotoComponent2(boolean f, Image img){
        isFlip = f;
        init = img;
        x = init.getWidth(null);
        y = init.getHeight(null);
        setPreferredSize(new Dimension (x,y));
        bi = new BufferedImage(init.getWidth(null),init.getHeight(null),BufferedImage.TYPE_INT_ARGB);
        newIcon = new ImageIcon(bi);
        img1 = new JLabel("", newIcon, JLabel.CENTER);
        image = img1;
        this.add(img1);
        this.addKeyListener(this);
        this.setFocusable(true);
        this.requestFocus(true);
        //System.out.println("In constructor");
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(init, 0, 0, null);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        //System.out.println("In paintComponent");
缩略图:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JComponent;


public class ThumbnailComponent extends JComponent{
    /**
    * ThumbnailComponent class is a way to create smaller versions of each photo passed in. 
    * 
    * @author Puja Sheth
    * @version 1.0 10/16/2014
    */
    private static final long serialVersionUID = 1L;

    PhotoComponent2 pc;
    Image img;
    double x;
    double y;
    int newX;
    int newY;

    public ThumbnailComponent(PhotoComponent2 input){
        pc = input;
        img = pc.init;
        x = (img.getWidth(null))/(.5);
        y = (img.getHeight(null))/(.5);
        newX = (int)x;
        newY = (int)y;
        setPreferredSize(new Dimension (newX,newY));
//      add(input);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics gCopy = g.create();
        Graphics2D g2d = (Graphics2D)gCopy;
        g2d.scale(.5,.5);
        pc.paintComponent(g2d);
    }
}

任何帮助都将不胜感激

不会解决您的问题,但对您的代码有一些一般性的评论:

scroll.add(lc.childPhoto);
scroll.setViewportView(lc.childPhoto);
scroll.revalidate();
scroll.repaint();
您不应该将组件添加到滚动窗格中。您需要的唯一代码行是:

scroll.setViewportView(lc.childPhoto);
当视口视图更改时,滚动窗格将自动重新验证()并重新绘制()

super.paintComponent(g);
g.drawImage(init, 0, 0, null);
this.addMouseListener(this);
this.addMouseMotionListener(this);

从不将侦听器添加到组件是一种绘制方法。绘画方法仅适用于绘画。听众与绘画无关。此外,每当Swing确定组件需要重新绘制时,都会调用绘制方法,以便向组件添加多个侦听器

是的,我明白我不应该把监听器放在一个paintComponent方法中,但我也在做这件事,我可以“翻转”照片,画线,文字等。让监听器在绘画方法中是我能让这个功能工作的唯一方法。但是,您给我的关于向jscrollpane添加组件的建议非常好!!少写几行代码哈哈。
让听众使用绘画方法是我让该功能发挥作用的唯一方法。
不!这是100%的错误!!!这不是解决问题的办法。您只需在类的构造函数中添加侦听器。
super.paintComponent(g);
g.drawImage(init, 0, 0, null);
this.addMouseListener(this);
this.addMouseMotionListener(this);