我有一个加载图像滚动窗格。我将这个图像与她的自然大小,如果这个图像太大,我将不会激活滚动条,但这个指令
g.drawImage(immagine, 0, 0, getWidth(), getHeight(), this);
用于放置在滚动窗格中的缩放图像。我能做什么?
类GUI:
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) { } } } }
类映射:
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"); } }
我将这个图像与她的自然大小,如果这个图像太大,我将不会激活滚动条,
使用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);
}
});
}
}
JScrollPane
或者更确切地说,JViewPort
将使用组件(在本例中是“视图”)的首选大小作为确定视图应该有多大的基础。
当视图扩展到滚动窗格的大小之外时,它将显示滚动条。
因此,基本上,您需要重写公共类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
,但这是一个相当高级的主题
我已经在JScrollPane中放置了一个JPanel对象,滚动工作就像预期的那样。通过重写paintComponent(),我尝试在JPanel对象中进行自定义绘制。然而,当JPanel对象放置在JScrollPane中时,JPanel不再正确地绘制(而是只显示其背景颜色)。 因为我的应用程序要求JPanel不断更新,所以构造了一个单独的线程,以在特定的时间间隔重新绘制JPanel。 以下代码摘
我如何渲染纹理让假设宽度=400,高度=800,在这个比例? 这是我的代码:
我有一个pandas数据框,它在一系列中具有以下值 我被指示用Python 3.6在Jupyter笔记本中绘制两个直方图。不用担心,对吧? 我选择了8个垃圾箱,因为这对我来说是最好的。我还被指示用x的对数绘制另一个直方图。 这个直方图看起来很糟糕。我做得不对吗?我试过摆弄这个情节,但我所做的一切似乎都让柱状图看起来更糟。例子: 除了将X的日志绘制为直方图之外,我没有得到任何指示。 我真的很感激任何
主要内容:1 Swing绘制图形的介绍,2 Swing绘制图形的方法,3 Swing绘制图形的案例1 Swing绘制图形的介绍 java.awt.Graphics类提供了许多用于图形编程的方法。 2 Swing绘制图形的方法 方法 描述 public abstract void drawString(String str, int x, int y) 用于绘制指定的字符串。 public void drawRect(int x, int y, int width, int height) 绘制
问题内容: 我在程序开始时,根据数据库中的某些内容,以编程方式在JScrollPane中添加了许多组件(JPanels,JLabels等)。 似乎对于GUI(?)而言,此过程太快了,因此JScrollPane并不总是正确更新,即,即使内部JPanel大于可见区域,滚动条也不可见。 调整窗口大小(JFrame)可以解决此问题,因为我认为Java在调整组件大小时会重新打印它们。 作为测试,我添加了一个
我有一张桌子,比如: 生成虚拟数据: 我想用垫线 (v1.4) 在 Python 3 中完成一个任务: 绘制的直方图 按 两个小时,未能获得所需的直方图 我阅读了matplotlib的示例和用户指南。令人惊讶的是,我没有找到关于如何从颜色图中指定颜色的教程 我在谷歌上搜索过,但没有找到一个简洁的例子 我想一个人可以用,不导入一系列模块,例如,