Java Jpanel中的放大和缩小功能
我必须将Java Jpanel中的放大和缩小功能,java,swing,applet,Java,Swing,Applet,我必须将放大和缩小功能添加到JPanel,其中包含JLabel和ImageIcon等组件 我想适当地缩放JPanel及其组件 我尝试使用以下代码段,但它不能正常工作 JPanel具有null布局,并且自身放置在Applet中。 我不确定它不工作的原因是因为Applet或其他原因 cPanel是我的JPanel,它包含JLabel 下面是缩放按钮上的代码片段单击, 它显示闪烁屏幕上的按钮点击后,这是原来的 Graphics g = cPanel.getGraphics(); Graphics2D
放大和缩小功能添加到JPanel
,其中包含JLabel
和ImageIcon
等组件
我想适当地缩放JPanel及其组件
我尝试使用以下代码段,但它不能正常工作
JPanel
具有null
布局,并且自身放置在Applet
中。
我不确定它不工作的原因是因为Applet
或其他原因
cPanel
是我的JPanel
,它包含JLabel
下面是缩放按钮上的代码片段
单击,
它显示闪烁屏幕上的按钮点击后,这是原来的
Graphics g = cPanel.getGraphics();
Graphics2D g2d = (Graphics2D) g;
AffineTransform savedXForm = g2d.getTransform();
g2d.scale(1.0, 1.0);
g2d.setColor(Color.red);
super.paint(g);
g2d.setTransform(savedXForm);
cPanel.validate();
cPanel.repaint();
您可以查看此链接:
要参考的源代码(此处已使用MouseWheelListener完成):
“我想放大放大”我想让问题易于阅读。请停止“咕哝”,在句首添加一个大写字母。我会看看你的布局问题,然后看看JXLayer
Seepost@ExtremeCoders+1表示建议,可惜链接断了…;)请任何人告诉我我做错了什么,有没有其他解决方案可以做到这一点,或者它不可能在applet中,因为我设置为jpanel的空布局
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import com.mortennobel.imagescaling.ResampleOp;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageZoom {
private JFrame frmImageZoomIn;
private static final String inputImage = "C:\\my-pfl-pic.jpg"; // give image path here
private JLabel label = null;
private double zoom = 1.0; // zoom factor
private BufferedImage image = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ImageZoom window = new ImageZoom();
window.frmImageZoomIn.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
* @throws IOException
*/
public ImageZoom() throws IOException {
initialize();
}
/**
* Initialize the contents of the frame.
* @throws IOException
*/
private void initialize() throws IOException {
frmImageZoomIn = new JFrame();
frmImageZoomIn.setTitle("Image Zoom In and Zoom Out");
frmImageZoomIn.setBounds(100, 100, 450, 300);
frmImageZoomIn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
frmImageZoomIn.getContentPane().add(scrollPane, BorderLayout.CENTER);
image = ImageIO.read(new File(inputImage));
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
// display image as icon
Icon imageIcon = new ImageIcon(inputImage);
label = new JLabel( imageIcon );
panel.add(label, BorderLayout.CENTER);
panel.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = e.getWheelRotation();
double temp = zoom - (notches * 0.2);
// minimum zoom factor is 1.0
temp = Math.max(temp, 1.0);
if (temp != zoom) {
zoom = temp;
resizeImage();
}
}
});
scrollPane.setViewportView(panel);
}
public void resizeImage() {
System.out.println(zoom);
ResampleOp resampleOp = new ResampleOp((int)(image.getWidth()*zoom), (int)(image.getHeight()*zoom));
BufferedImage resizedIcon = resampleOp.filter(image, null);
Icon imageIcon = new ImageIcon(resizedIcon);
label.setIcon(imageIcon);
}
}