我设法增加了JLabel的图像(其中存储了imageIcon)。当我按下增大尺寸按钮时,面板上的图像原始尺寸会增加,这正是我想要的。但是,当我点击我的缩小尺寸按钮(我想把它除以刻度可能会修复它)时,标签会减少,但实际的图像外观(我猜的尺寸)会发生变化。它没有减小尺寸,就像我的增加按钮增加尺寸一样。我花了几个小时试图找出为什么通过乘以它,我能够增加标签的大小和其中的图像(这意味着不仅标签正在增加,实际图像也是如此),而是减少(我划分而不是乘法)它不起作用。这是我的增加和减少的听众。
public class IncreaseSizeListener implements ActionListener {
static JLabel increasedLabel;
@Override
public void actionPerformed(ActionEvent e) {
increasedLabel = CardLabelListener.selectedLabel;
Icon icon = CardLabelListener.selectedLabel.getIcon();
int scale =2;
System.out.println("Increased size fired");
//I can now resize images, based on my needs
BufferedImage bi = new BufferedImage(
scale*icon.getIconWidth(),
scale*icon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.scale(scale,scale);
icon.paintIcon(null,g,0,0);
g.dispose();
JLabel temp = new JLabel(new ImageIcon(bi));
//to ensure proper size is kept for the enlarged image
CardLabelListener.selectedLabel.setSize(icon.getIconWidth()*scale, icon.getIconHeight()*(scale));
CardLabelListener.selectedLabel.setIcon(temp.getIcon());
CardLabelListener.selectedLabel.updateUI();
}
}
public class DecreaseSizeListener implements ActionListener {
static JLabel increasedLabel;
@Override
public void actionPerformed(ActionEvent e) {
increasedLabel = CardLabelListener.selectedLabel;
Icon icon = CardLabelListener.selectedLabel.getIcon();
int scale =2;
//I can now resize images, based on my needs
BufferedImage bi = new BufferedImage(
icon.getIconWidth()/scale,
icon.getIconHeight()/scale,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.scale(scale,scale);
icon.paintIcon(null,g,0,0);
g.dispose();
JLabel temp = new JLabel(new ImageIcon(bi));
//to ensure proper size is kept for the enlarged image
CardLabelListener.selectedLabel.setSize( (icon.getIconWidth()/scale), (icon.getIconHeight()/(scale)));
CardLabelListener.selectedLabel.setIcon(temp.getIcon());
CardLabelListener.selectedLabel.updateUI();
}
}
答案 0 :(得分:0)
在减少操作侦听器中将g.scale(scale,scale);
更改为g.scale(0.5d,0.5d);
或者你可以这样做......
int scale = 0.5;
//I can now resize images, based on my needs
BufferedImage bi = new BufferedImage(
icon.getIconWidth() * scale,
icon.getIconHeight() * scale,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.scale(scale,scale);
icon.paintIcon(null,g,0,0);
g.dispose();
// This really isn't required...
//JLabel temp = new JLabel(new ImageIcon(bi));
//to ensure proper size is kept for the enlarged image
// There is a better way...
//CardLabelListener.selectedLabel.setSize( (icon.getIconWidth()/scale), (icon.getIconHeight()/(scale)));
// This isn't required
//CardLabelListener.selectedLabel.setIcon(temp.getIcon());
// This doesn't do what you think it does...
//CardLabelListener.selectedLabel.updateUI();
CardLabelListener.selectedLabel.setIcon(new ImageIcon(bi));
CardLabelListener.selectedLabel.setSize(CardLabelListener.selectedLabel.getPreferredSize());
现在增加和减少算法都差不多(除了因子),你应该能够使用单一方法;)
这几乎是我最终的代码...
public class ScaleMyIcon {
public static void main(String[] args) {
new ScaleMyIcon();
}
public ScaleMyIcon() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ScaleMyIconPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class ScaleMyIconPane extends JPanel {
public ScaleMyIconPane() {
setLayout(new BorderLayout());
ImageIcon image = null;
try {
image = new ImageIcon(ImageIO.read(getClass().getResource("/stormtrooper-tie.jpg")));
} catch (IOException ex) {
ex.printStackTrace();
}
JLabel label = new JLabel(image);
add(label);
JPanel buttons = new JPanel();
JButton increase = new JButton("+");
JButton decrease = new JButton("-");
buttons.add(increase);
buttons.add(decrease);
increase.addActionListener(new IncreaseSizeListener(label));
decrease.addActionListener(new DecreaseSizeListener(label));
add(buttons, BorderLayout.SOUTH);
}
}
public class Scaler {
public Icon getScaledInstance(Icon original, double scale) {
BufferedImage bi = new BufferedImage(
(int)Math.round(scale * original.getIconWidth()),
(int)Math.round(scale * original.getIconHeight()),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.scale(scale, scale);
original.paintIcon(null, g, 0, 0);
g.dispose();
return new ImageIcon(bi);
}
}
public class IncreaseSizeListener extends Scaler implements ActionListener {
private JLabel increasedLabel;
private IncreaseSizeListener(JLabel label) {
increasedLabel = label;
}
@Override
public void actionPerformed(ActionEvent e) {
Icon icon = increasedLabel.getIcon();
int scale = 2;
increasedLabel.setIcon(getScaledInstance(icon, scale));
}
}
public class DecreaseSizeListener extends Scaler implements ActionListener {
private JLabel decreasedLabel;
private DecreaseSizeListener(JLabel label) {
decreasedLabel = label;
}
@Override
public void actionPerformed(ActionEvent e) {
Icon icon = decreasedLabel.getIcon();
decreasedLabel.setIcon(getScaledInstance(icon, 0.5d));
}
}
}
使用不同方法更新
当我正在捣乱时,我发现了两个问题。在上下尺度之间没有联盟,你从来没有使用原始图像来缩放,你总是缩放脏图像。尝试缩小图像并重新备份。
这是我对如何克服这些问题的看法
public class ScaleMyIcon {
public static void main(String[] args) {
new ScaleMyIcon();
}
public ScaleMyIcon() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ScaleMyIconPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class ScaleMyIconPane extends JPanel {
public ScaleMyIconPane() {
setLayout(new BorderLayout());
ImageIcon image = null;
try {
image = new ImageIcon(ImageIO.read(getClass().getResource("/stormtrooper-tie.jpg")));
} catch (IOException ex) {
ex.printStackTrace();
}
JLabel label = new JLabel(image);
add(label);
JPanel buttons = new JPanel();
JButton increase = new JButton("+");
JButton decrease = new JButton("-");
buttons.add(increase);
buttons.add(decrease);
increase.addActionListener(new IncreaseSizeListener(label));
decrease.addActionListener(new DecreaseSizeListener(label));
add(buttons, BorderLayout.SOUTH);
}
}
public static class Scalable {
private JLabel label;
private Icon original;
private static double scale = 1;
private Scalable(JLabel label) {
this.label = label;
original = label.getIcon();
}
public JLabel getLabel() {
return label;
}
public double getScale() {
return scale;
}
public void setScale(double scale) {
this.scale = scale;
}
public void incrementScale(double factor) {
setScale(getScale() + factor);
}
public Icon getScaledInstance() {
BufferedImage bi = new BufferedImage(
(int) Math.round(scale * original.getIconWidth()),
(int) Math.round(scale * original.getIconHeight()),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.scale(scale, scale);
original.paintIcon(null, g, 0, 0);
g.dispose();
return new ImageIcon(bi);
}
}
public class IncreaseSizeListener extends Scalable implements ActionListener {
public IncreaseSizeListener(JLabel label) {
super(label);
}
@Override
public void actionPerformed(ActionEvent e) {
incrementScale(0.05);
getLabel().setIcon(getScaledInstance());
}
}
public class DecreaseSizeListener extends Scalable implements ActionListener {
private DecreaseSizeListener(JLabel label) {
super(label);
}
@Override
public void actionPerformed(ActionEvent e) {
incrementScale(-0.05);
getLabel().setIcon(getScaledInstance());
}
}
}