我的JFrame中有两张图片。第一个是原版,我希望第二个图像模糊。我有两个图像都反映在帧中,但第二个没有模糊。如何更好地编写此程序以反映模糊图像?
public static void main(String[] args) throws IOException {
String path = "src/logo.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(0,200);
f.setVisible(true);
String path2 = "src/logo.jpg";
File file2 = new File(path2);
BufferedImage image2 = ImageIO.read(file2);
JLabel label2 = new JLabel(new ImageIcon(image2));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label2);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
float[] matrix = new float[400];
for (int i = 0; i < 400; i++)
matrix[i] = 1.0f/400.0f;
BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null );
image2 = op.filter(image, null);
}}
此外,第二张图像的位置关闭,第一张图像也没有。我的setLocation应该并排放置两个图像,中间有空格。
答案 0 :(得分:1)
BorderLayout.WEST
位置,将第二个标签添加到BorderLayout.EAST
位置例如
public static void main(String[] args) throws IOException {
String path = "src/logo.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label, BorderLayout.WEST);
String path2 = "src/logo.jpg";
File file2 = new File(path2);
BufferedImage image2 = ImageIO.read(file2);
float[] matrix = new float[400];
for (int i = 0; i < 400; i++)
matrix[i] = 1.0f/400.0f;
BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, matrix), ConvolveOp.EDGE_NO_OP, null );
image2 = op.filter(image, null);
JLabel label2 = new JLabel(new ImageIcon(image2));
f.getContentPane().add(label, BorderLayout.WEST);
f.pack();
f.setVisible(true);
}