在缩放范围内在JScrollPane中绘制图像
我有一个滚动窗格加载图像。 我用她自然的大小来处理这个图像,如果这个图像太大,我不会激活滚动条,但是这个指令
g.drawImage(immagine, 0, 0, getWidth(), getHeight(), this);
缩放图像放置在滚动窗格中。 我能做什么?
桂贵班:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Gui implements ActionListener {
private JFrame frmEditor;
private Mappa content;
private JMenuItem mntmSfondo;
private JScrollPane scrollabile;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui window = new Gui();
window.frmEditor.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Gui() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmEditor = new JFrame();
frmEditor.setFont(UIManager.getFont("TextArea.font"));
frmEditor.setBounds(50, 50, 1024, 768);
frmEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmEditor.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panelTile = new JPanel();
panelTile.setLayout(new BorderLayout(0, 0));
content = new Mappa(null);
content.setMinimumSize(new Dimension(150, 150));
scrollabile = new JScrollPane(content);
frmEditor.getContentPane().add(scrollabile, BorderLayout.CENTER);
inizializzaMenu();
}
/**
* Initialize the menu.
*/
private void inizializzaMenu() {
JMenuBar menuBar = new JMenuBar();
frmEditor.setJMenuBar(menuBar);
JMenu mnAltro = new JMenu("Modify");
menuBar.add(mnAltro);
mntmSfondo = new JMenuItem("Load Background");
mntmSfondo.addActionListener(this);
mnAltro.add(mntmSfondo);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == mntmSfondo) {
JFileChooser fc = new JFileChooser("tuttiSfondi");
int result = fc.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
content.setImage(file);
//content = new Mappa(file);
//scrollabile.setViewportView(content);
} catch (Exception ex) {
}
}
if (result == JFileChooser.CANCEL_OPTION) {
}
}
}
}
班级Mappa:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Mappa extends JPanel {
BufferedImage immagine;
public Mappa(File fileImmagine) {
if (fileImmagine != null ) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(fileImmagine.getPath()));
} catch (IOException e) {
e.printStackTrace();
}
this.immagine = img;
}
repaint();
}
public void setImage(File file) throws IOException {
this.immagine = ImageIO.read(file);
String name = file.getPath();
System.out.println(name);
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, 4000, 4000);
g.drawImage(this.immagine, 0, 0, getWidth(), getHeight(), this);
System.out.println("Called Repaint() on Mappa");
}
}
JScrollPane或更多JViewport将使用该组件(或者在本例中为“视图”)的首选大小作为确定视图应该有多大的基础。
当视图展开超出滚动窗格的大小时,它将显示滚动条。
所以基本上,您需要重写public class Mappa extends JPanel {面板,例如,的getPreferredSize
public class Mappa extends JPanel {
//...
public Dimension getPreferredSize() {
return immagine == null ? new Dimension(200, 200) : new Dimension(immagine.getWidth(), immagine.getHeight());
}
//...
}
这将鼓励JViewport始终与图像大小相同。
另外,有两件事...
首先,例如,你不应该依赖神奇的数字
g.clearRect(0, 0, 4000, 4000);
应该更像...
g.clearRect(0, 0, getWidth(), getHeight());
其次,
super.paintComponent(g);
将以任何方式做到这一点,所以调用clearRect是毫无意义的...
您可能还想看看Scrollable ,但这是一个相当先进的话题
我用她的自然大小不会这个图像,如果这个图像太大,我不会激活滚动条,
使用JLabel来包含图像并将其包装在JScrollPane应该很容易实现你想要的。 从以下示例中获取提示:
class AFrame extends JFrame
{
public AFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Image view Demo with JScrollPane");
ImageIcon image = new ImageIcon("myImage.png"); // pass the file location of an image
JLabel label = new JLabel(image);
JScrollPane scrollPane = new JScrollPane(label);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane, BorderLayout.CENTER);
pack();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new AFrame().setVisible(true);
}
});
}
}
链接地址: http://www.djcxy.com/p/34937.html
