我在下面的代码中执行了缩放操作。窗体中的两个滑块是缩放仿射变换的 X 和 Y 参数。我在这里的问题是,当我更改滑块时,图像正在缩放,如何动态调整绘制此图像的 jpanel 的大小。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
*
* @author ABC
*/
public class Swon extends javax.swing.JFrame {
final BufferedImage img;
/**
* Creates new form Swon
*/
public Swon() throws IOException{
BufferedImage im=ImageIO.read(new File("C:/Users/ABC/Desktop/I-RIX2012_Final_logo_out.jpg"));
this.img=new BufferedImage(im.getWidth(),im.getWidth(),BufferedImage.TYPE_BYTE_GRAY);
this.img.getGraphics().drawImage(im, 0, 0, null);
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel(){
// @Override
// public void paint(Graphics g)
// {
// super.paint(g);
// g.drawImage(img.getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_FAST),0,0,null);
// }
};
jSlider1 = new javax.swing.JSlider();
jSlider2 = new javax.swing.JSlider();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
jPanel1.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
jPanel1ComponentResized(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 268, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 209, Short.MAX_VALUE)
);
jSlider1.setMaximum(5);
jSlider1.setValue(1);
jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
jSlider2.setMaximum(5);
jSlider2.setValue(1);
jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider2StateChanged(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(242, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(33, 33, 33)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jPanel1ComponentResized(java.awt.event.ComponentEvent evt) {
// TODO add your handling code here:
// AffineTransform f=new AffineTransform();
}
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
AffineTransform scale = AffineTransform.getScaleInstance(jSlider1.getValue(),jSlider2.getValue());
AffineTransformOp op3 = new AffineTransformOp(scale, AffineTransformOp.TYPE_BILINEAR);
BufferedImage scaling = new BufferedImage(this.img.getHeight(), this.img.getWidth(), this.img.getType());
BufferedImage img1=op3.filter(this.img, scaling);
jPanel1.getGraphics().drawImage(img1.getScaledInstance(jPanel1.getWidth(), jPanel1.getHeight(), Image.SCALE_FAST), 0, 0, null);
}
private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
AffineTransform scale = AffineTransform.getScaleInstance(jSlider1.getValue(),jSlider2.getValue());
AffineTransformOp op3 = new AffineTransformOp(scale, AffineTransformOp.TYPE_BILINEAR);
BufferedImage scaling = new BufferedImage(this.img.getHeight(), this.img.getWidth(), this.img.getType());
BufferedImage img1=op3.filter(this.img, scaling);
jPanel1.getGraphics().drawImage(img1.getScaledInstance(jPanel1.getWidth(), jPanel1.getHeight(), Image.SCALE_FAST), 0, 0, null);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Swon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Swon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Swon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Swon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Swon().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Swon.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
public javax.swing.JPanel jPanel1;
private javax.swing.JSlider jSlider1;
private javax.swing.JSlider jSlider2;
// End of variables declaration
}
好吧,首先,不要这样做.img.getGraphics().drawImage(im
, 0, 0, null),当面板被重新绘制时,图像不会更新。您需要重写 JComponent
的 paintComponent
方法(首选 JPanel
)并以适当的比例重新绘制图像。
其次,您需要将图像面板放置在能够实际关心面板大小的布局上(例如 GridBagLayout
)。为此,请重写 getPreferredSize
方法并返回缩放图像的大小...
public class ResizableImagePane {
public static void main(String[] args) {
new ResizableImagePane();
}
public ResizableImagePane() {
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 ScalerPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class ScalerPane extends JPanel {
private JSlider slider;
private ImagePane imagePane;
public ScalerPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
imagePane = new ImagePane();
add(imagePane, gbc);
gbc.gridy = 1;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
slider = new JSlider();
slider.setMinimum(1);
slider.setMaximum(100);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(slider, gbc);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent ce) {
imagePane.setZoom(slider.getValue() / 100f);
}
});
slider.setValue(100);
}
}
protected class ImagePane extends JPanel {
private BufferedImage background;
private Image scaled;
private float zoom;
public ImagePane() {
try {
background = ImageIO.read(new File("path/to/your/image"));
} catch (IOException ex) {
ex.printStackTrace();
}
setZoom(1f);
setBorder(new LineBorder(Color.RED));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(scaled.getWidth(this), scaled.getHeight(this));
}
public void setZoom(float zoom) {
this.zoom = zoom;
int width = Math.round(background.getWidth() * zoom);
scaled = background.getScaledInstance(width, -1, Image.SCALE_SMOOTH);
invalidate();
revalidate();
repaint();
}
public float getZoom() {
return zoom;
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
int x = (getWidth() - scaled.getWidth(this)) / 2;
int y = (getHeight() - scaled.getHeight(this)) / 2;
grphcs.drawImage(scaled, x, y, this);
}
}
}
起初,我只有文本栏和状态栏,如下例所示(我不需要父容器): 这是应该的。但是,请记住状态栏的高度。现在,如果您将StatusBar类更改为下面的类,那么高度几乎会增加一倍。为什么会这样? 这种设置并不需要更多的垂直空间: 我可以让这成为一个新问题,但我觉得不值得...
问题内容: 一旦我在代码中使用了渐变,在调整大小时就不会完成重新绘制,而在调整大小时 会得到类似的信息(已调整大小的黑色矩形,请参见下面的链接中的图像)。当我停止调整大小时,一切都会 再次绘制,但只有那时。 如果我不使用,;我会快速重画 http://gui-builder.com/C41142775162.rar 我尝试调整大小,尝试拖动鼠标时却 什么也没有。 这是一些SSCCE(对不起,我之前
我有一个JScroll面板和一个JGroup添加到它。我想绘制到JGroup并使JScrollPane的滚动条在绘图超过面板大小时出现,并且能够垂直和水平滚动绘图。 我尝试过咨询各种论坛和官方文档,并尝试了一些事情(设置边框、首选尺寸等),但似乎都没有产生预期的效果。 我有一个JFrame(附带GridBagLayout,btw.): 相关组件包括: JPanel公司: JScrollPane:
问题内容: 我是Java的新手,开始学习挥杆技巧,并且在JFrame中调整JPanel的大小时遇到问题。我正在关注本教程: http://vincentramdhanie.blogspot.com/2009/11/cardlayout-with- netbeans.html 因为最终我将学习每种不同的布局,并且现在已经进入CardLayout。在上面,有一个JPanel用于状态面板。那也是我想
我是Java新手,开始学习swing,在JFrame中调整JPanel的大小时遇到问题。我将学习本教程: http://vincentramdhanie.blogspot.com/2009/11/cardlayout-with-netbeans.html 因为最终我正在学习每一种不同的布局,现在我来到了CardLayout。在上面,有一个JPanel用于状态面板。这也是我想要做的,但是当我将一个J
我制作了一个包含三个jpanel的jframe,这三个panel将jframe分成三个相等的部分。每一个面板都包含一个图像。我想调整图像和面板的大小,当帧的大小从高到低的时候。有人能告诉我如何做吗。
这里是简单的Java问题。我制作了一个自定义的JSlider,它在用户移动滑块的最后一个位置绘制方块。我们重写paintcomponent并绘制相对于滑块大小的方块。当你正确地使用滑块时,一切工作都很好。但是,当你调整框架的大小时,问题就会发生。滑块变大,滑块调节器随之移动,但是我们放置的方块相对于滑块不移动。当我们调整框架大小时,我们应该从JSlider中使用哪个函数来重新绘制? 我试图将JSl