调整大小和翻转函数应该在左侧面板中生成结果图像。但是,调整大小功能只生成相同的图像,翻转功能会产生黑屏图像。我该如何解决这个问题?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ImgViewTester extends JFrame implements ActionListener
{
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open Image...");
JLabel imgLabel = new JLabel();
JLabel newImgLabel = new JLabel();
JPanel lblPanel = new JPanel();
JButton resizeBtn = new JButton("Resize");
JButton flipBtn = new JButton("Flip");
JPanel imagePanel;
JPanel btnPanel;
JPanel resizePanel;
JTextField widthTxt = new JTextField(10);
JTextField heightTxt = new JTextField(10);
JFileChooser fc = new JFileChooser();
File imgFile;
String imgFileName;
String imgTitle;
Image img;
//BufferedImage buffImg;
Graphics2D g2d;
Graphics gb;
public ImgViewTester()
{
initialize();
}
public void initialize()
{
this.setTitle("Image Editor");
this.setSize(700, 700);
this.setLayout(new BorderLayout());
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
setJMenuBar(menuBar);
menuBar.add(fileMenu);
fileMenu.add(openItem);
openItem.addActionListener(this);
imgLabel.setPreferredSize(new Dimension(500, 500));
newImgLabel.setPreferredSize(new Dimension(500, 500));
imagePanel = new JPanel();
imagePanel.add(imgLabel);
imagePanel.add(newImgLabel);
btnPanel = new JPanel();
btnPanel.add(resizeBtn);
btnPanel.add(flipBtn);
resizeBtn.addActionListener(this);
flipBtn.addActionListener(this);
resizePanel = new JPanel();
resizePanel.add(widthTxt);
resizePanel.add(heightTxt);
this.add(imagePanel, BorderLayout.CENTER);
this.add(btnPanel, BorderLayout.SOUTH);
this.add(resizePanel, BorderLayout.NORTH);
this.pack();
}
public static void main(String[] args){
new ImgViewTester();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(openItem))
{
JFileChooser imgFileChooser = new JFileChooser();
FileNameExtensionFilter imgFilter =
new FileNameExtensionFilter("GIF, PNG, JPEG or JPG files",
"gif", "png", "jpg", "jpeg");
imgFileChooser.setFileFilter(imgFilter);
int returnVal = imgFileChooser.showOpenDialog(null);
imgFile = imgFileChooser.getSelectedFile();
//variables for getting name of image
imgFileName = imgFile.getName();
imgTitle = imgFileName.substring(imgFileName.lastIndexOf("/") + 1);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
img = ImageIO.read(imgFile);
img = img.getScaledInstance(500, -1, Image.SCALE_DEFAULT);
imgLabel.setIcon(new ImageIcon(img));
}
catch(IOException ioe)
{
System.out.println("Oops! Image I/O exception occurred!");
ioe.printStackTrace();
}
}
else if(returnVal == JFileChooser.CANCEL_OPTION)
{
System.out.println("Operation Cancelled!");
}
}
else if(e.getSource().equals(resizeBtn))
{
int newWidth= Integer.parseInt(widthTxt.getText());
int newHeight = Integer.parseInt(heightTxt.getText());
img.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
newImgLabel.setIcon(new ImageIcon(img));
System.out.println(imgFileName);
}
else if(e.getSource().equals(flipBtn))
{
ImageIcon currIcon = (ImageIcon)imgLabel.getIcon();
BufferedImage buffImg = new BufferedImage(currIcon.getIconWidth(), currIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-buffImg.getWidth(null), 0);
AffineTransformOp op = new AffineTransformOp(tx,AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
buffImg = op.filter(buffImg, null);
newImgLabel.setIcon(new ImageIcon(buffImg));
System.out.println(newImgLabel);
}
}
}//end of class
答案 0 :(得分:6)
img.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
imgLabel.setIcon(new ImageIcon(img));
应该是:
Image scaled = img.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
imgLabel.setIcon(new ImageIcon(scaled));
每当我在缩放之前完成翻译之前我已经完成了翻转:
import java.awt.*;
import javax.swing.*;
class MirrorImageIcon extends ImageIcon
{
public MirrorImageIcon(String filename)
{
super(filename);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
Graphics2D g2 = (Graphics2D)g.create();
g2.translate(0, getIconHeight());
g2.scale(1, -1);
super.paintIcon(c, g2, x, y);
g2.dispose();
}
private static void createAndShowUI()
{
ImageIcon io = new MirrorImageIcon("dukewavered.gif");
JLabel label = new JLabel(io);
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label, BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}