我试图制作一个允许我打开,放大和缩小图像的小程序。我有一个正在运行的小程序,但我在缩放方面遇到了麻烦。关于从哪里前往的任何想法?
这是我到目前为止所拥有的
ImageZoom
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class ImageZoom extends JApplet implements ActionListener
{
JPanel jpanel, pane2;
JLabel image;
JButton open_file, zoom_in, zoom_out;
Image img;
JFileChooser fc;
Zoom zP;
@Override
public void init()
{
setSize(450,450);
buttonLayout();
filechooser();
imgLayout();
zP = new Zoom();
}
public void imgLayout()
{
pane2 = new JPanel();
pane2.setLayout(new BorderLayout());
image = new JLabel("");
pane2.add(image,BorderLayout.CENTER);
}
public void buttonLayout()
{
jpanel = new JPanel(new FlowLayout());
open_file = new JButton("Open file");
open_file.addActionListener(this);
jpanel.add(open_file);
zoom_in = new JButton("Zoom In");
zoom_in.addActionListener(this);
jpanel.add(zoom_in);
zoom_out = new JButton("Zoom Out");
zoom_out.addActionListener(this);
jpanel.add(zoom_out);
add( jpanel, BorderLayout.NORTH );
}
public void filechooser()
{
fc = new JFileChooser();
fc.setCurrentDirectory(new File(
System.getProperty("user.home")));
}
@Override
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == open_file)
{
int result = fc.showOpenDialog(null );
if(result == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
String sname = file.getAbsolutePath();
image = new JLabel("", new ImageIcon(sname), JLabel.CENTER);
jpanel.add(image, BorderLayout.CENTER);
jpanel.revalidate();
jpanel.repaint();
}
}
else if(ae.getSource() == zoom_in)
{
zP.increaseSize();
}
else if(ae.getSource() == zoom_out)
{
zP.decreaseSize();
}
}
}
缩放
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class Zoom extends Panel
{
MediaTracker tracker;
Image img;
Dimension imgSize,iniSize;
public Zoom()
{
setSize(400,275);
img =Toolkit.getDefaultToolkit().getImage("image"); // here u give ur own image name
tracker=new MediaTracker(this);
tracker.addImage(img,1);
try{
tracker.waitForAll();
}
catch(InterruptedException ie){
System.out.println("Error in loading image");
}
imgSize=iniSize=new Dimension(img.getWidth(this),img.getHeight(this));
}
@Override
public Dimension getPreferredSize(){
return new Dimension(imgSize);
}
public void decreaseSize()
{
int x=10*imgSize.width/100;
int y=10*imgSize.height/100;
imgSize=new Dimension(imgSize.width-x,imgSize.height-y);
if(getWidth()>iniSize.width)
{
setSize(imgSize);
getParent().doLayout();
}
repaint();
}
public void increaseSize()
{
int x=10*imgSize.width/100;
int y=10*imgSize.height/100;
imgSize = new Dimension(imgSize.width+x,imgSize.height+y);
if(imgSize.width>iniSize.width)
{
setSize(imgSize);
getParent().doLayout();
}
repaint();
}
} // class Zoom
答案 0 :(得分:3)
您可以继续使用JLabel
并缩放图片并重置JLabel
s icon
属性,也可以创建专用于该作业的自定义组件......
首先,我们需要显示图像......
public class ZoomPane extends JPanel {
private BufferedImage master;
private Image scaled;
private float scale = 1f;
public ZoomPane() {
try {
master = ImageIO.read(new File("..."));
scaled = master;
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return scaled != null ? new Dimension(scaled.getWidth(this), scaled.getHeight(this)) : new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (scaled != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - scaled.getWidth(this)) / 2;
int y = (getHeight() - scaled.getHeight(this)) / 2;
g2d.drawImage(scaled, x, y, this);
g2d.dispose();
}
}
}
现在,您可以使用MouseWheelListener
缩放图片...
addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() < 0) {
scale -= 0.1;
} else {
scale += 0.1;
}
scaleBy(scale);
}
});
因为您可能想让用户使用几种不同的方法来缩放图像,这是一种不错的便捷方法......
public void scaleBy(float amount) {
scale += amount;
scale = Math.min(Math.max(scale, 0.1f), 200);
scaled = master.getScaledInstance(
Math.round(master.getWidth() * scale),
Math.round(master.getHeight() * scale),
Image.SCALE_SMOOTH);
revalidate();
repaint();
}
现在,允许用户使用 + 和 - 键可能会很好,为此,我们想要一些Action
...
public class ZoomAction extends AbstractAction {
private ZoomPane zoomPane;
private float zoomAmount;
public ZoomAction(ZoomPane zoomPane, String name, float zoomAmount) {
this.zoomAmount = zoomAmount;
this.zoomPane = zoomPane;
putValue(NAME, name);
}
@Override
public void actionPerformed(ActionEvent e) {
zoomPane.scaleBy(zoomAmount);
System.out.println("...");
}
}
这允许我们绑定键盘动作,按钮和菜单,并重复使用相同的基本代码来完成...
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "zoom-in");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0), "zoom-out");
am.put("zoom-in", new ZoomAction(this, "Zoom in", 0.1f));
am.put("zoom-out", new ZoomAction(this, "Zoom out", -0.1f));
此示例适用于小键盘 + 和 - 键。
所有这些功能都应直接添加到ZoomPane
您可以重复使用菜单中的ZoomAction
...
JMenuBar mb = new JMenuBar();
JMenu pictureMenu = new JMenu("Picture");
pictureMenu.add(new ZoomAction(zoomPane, "Zoom In", 0.1f));
pictureMenu.add(new ZoomAction(zoomPane, "Zoom Out", -0.1f));
mb.add(pictureMenu);
甚至在你的按钮......
JPanel buttons = new JPanel();
buttons.add(new JButton(new ZoomAction(zoomPane, "Zoom In", 0.1f)));
buttons.add(new JButton(new ZoomAction(zoomPane, "Zoom Out", -0.1f)));
frame.add(buttons, BorderLayout.SOUTH);
缩放非常粗糙,准备就绪,您应该考虑一下:
...了解更多详情
最后,将它们作为一个可运行的例子一起黑客攻击......
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
ZoomPane zoomPane = new ZoomPane();
JMenuBar mb = new JMenuBar();
JMenu pictureMenu = new JMenu("Picture");
pictureMenu.add(new ZoomAction(zoomPane, "Zoom In", 0.1f));
pictureMenu.add(new ZoomAction(zoomPane, "Zoom Out", -0.1f));
mb.add(pictureMenu);
JFrame frame = new JFrame("Testing");
frame.setJMenuBar(mb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(zoomPane);
JPanel buttons = new JPanel();
buttons.add(new JButton(new ZoomAction(zoomPane, "Zoom In", 0.1f)));
buttons.add(new JButton(new ZoomAction(zoomPane, "Zoom Out", -0.1f)));
frame.add(buttons, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ZoomPane extends JPanel {
private BufferedImage master;
private Image scaled;
private float scale = 1f;
public ZoomPane() {
try {
master = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\MegaTokyo\\megatokyo_omnibus_1_3_cover_by_fredrin-d4oupef.jpg"));
scaled = master;
addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() < 0) {
scale -= 0.1;
} else {
scale += 0.1;
}
scaleBy(scale);
}
});
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "zoom-in");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0), "zoom-out");
am.put("zoom-in", new ZoomAction(this, "Zoom in", 0.1f));
am.put("zoom-out", new ZoomAction(this, "Zoom out", -0.1f));
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void scaleBy(float amount) {
scale += amount;
scale = Math.min(Math.max(scale, 0.1f), 200);
scaled = master.getScaledInstance(
Math.round(master.getWidth() * scale),
Math.round(master.getHeight() * scale),
Image.SCALE_SMOOTH);
revalidate();
repaint();
}
@Override
public Dimension getPreferredSize() {
return scaled != null ? new Dimension(scaled.getWidth(this), scaled.getHeight(this)) : new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (scaled != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - scaled.getWidth(this)) / 2;
int y = (getHeight() - scaled.getHeight(this)) / 2;
g2d.drawImage(scaled, x, y, this);
g2d.dispose();
}
}
}
public class ZoomAction extends AbstractAction {
private ZoomPane zoomPane;
private float zoomAmount;
public ZoomAction(ZoomPane zoomPane, String name, float zoomAmount) {
this.zoomAmount = zoomAmount;
this.zoomPane = zoomPane;
putValue(NAME, name);
}
@Override
public void actionPerformed(ActionEvent e) {
zoomPane.scaleBy(zoomAmount);
System.out.println("...");
}
}
}