gpt4 book ai didi

java - 根据单击的 View 模式格式化 JPanel 以包含 ArrayList 和分割 View

转载 作者:行者123 更新时间:2023-12-02 05:21:07 28 4
gpt4 key购买 nike

我正在尝试通过 java swing 创建这个基本的照片编辑应用程序。当导入一张图片时,我的代码可以在某种程度上起作用;我有 3 个 View - 仅显示照片的照片 View 、应显示图片缩略图的缩略图 View 和应该是 BorderLayout.CENTER 中的照片和 BorderLayout.SOUTH 中的缩略图的组合的分割 View 。我已经添加了图像和代码摘录来解释为什么这不能按应有的方式工作。我无法上传任何图像,但希望摘录提供更多详细信息。

Split View相关摘录:

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

这基本上是我现在的所有问题。目前正在创建的缩略图基于当前图像。如果我进入照片 View 并更改使用前进/后退按钮显示的图像,然后返回浏览器 View ,我会得到相应图像的缩略图。但是,我希望能够为导入的所有图像创建缩略图,然后显示它。我尝试了 for 循环(我已将其注释掉),但这也没有帮助。与此相关的其他代码是我的主类,它创建 JFrame 和按钮等,photoComponent 具有我在缩略图和 lightComponent 类中使用的 PaintComponent 方法。

我的照片3:

view = new JMenu("View");
mbar.add(view);
pv = new JRadioButton("Photo Viewer");
pv.addActionListener(new ActionListener(){ //change status
public void actionPerformed(ActionEvent e){
sbar.setText("Status: By clicking this, you'll be able to view your photos one at a time");
boolean p = true;
boolean b = false;
boolean s = false;
lc.changeMode(p,b,s);
scroll.add(lc.childPhoto);
scroll.setViewportView(lc.childPhoto);
scroll.getViewport().setBackground(Color.BLUE);
scroll.revalidate();
scroll.repaint();

}
});

b = new JRadioButton("Browser");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //change status
sbar.setText("Status: By clicking this, you'll be able to view all your photos as thumbnails");
boolean p = false;
boolean b = true;
boolean s = false;
lc.changeMode(p,b,s);
scroll.setViewportView(lc.childBrowse);
scroll.getViewport().setBackground(Color.BLUE);
scroll.revalidate();
scroll.repaint();
}
});

sm = new JRadioButton("Split Mode");
sm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //change status
sbar.setText("Status: By clicking this, you'll be able to view a single photo with a film strip dimensional view");
boolean p = false;
boolean b = false;
boolean s = true;
lc.changeMode(p,b,s);
scroll.setViewportView(lc.containsAll);
scroll.getViewport().setBackground(Color.BLUE);
scroll.revalidate();
scroll.repaint();
}
});

照片组件:

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

您永远不应该将组件添加到滚动 Pane 。您需要的唯一代码行是:

scroll.setViewportView(lc.childPhoto);

当视口(viewport) View 更改时,滚动 Pane 将自动 revalidate() 和 repaint() 本身。

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

永远不要向组件添加监听器是一种绘画方法。绘画方法仅用于绘画。听众与绘画无关。此外,每当 Swing 确定需要重新绘制组件时,都会调用绘制方法,因此您将向该组件添加多个监听器。

关于java - 根据单击的 View 模式格式化 JPanel 以包含 ArrayList<JComponent> 和分割 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26510382/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com