栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在不移动中心的情况下放大JPanel:数学还是Swing?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在不移动中心的情况下放大JPanel:数学还是Swing?

我只是做了一个非常简单的示例,该示例基本上试图将滚动窗格保持在所提供图像中间的中心

public class TestZooming {    public static void main(String[] args) {        new TestZooming();    }    public TestZooming() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException classNotFoundException) {     } catch (InstantiationException instantiationException) {     } catch (IllegalAccessException illegalAccessException) {     } catch (UnsupportedLookAndFeelException unsupportedLookAndFeelException) {     }     Jframe frame = new Jframe();     frame = new Jframe();     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.setSize(400, 400);     frame.setLocationRelativeTo(null);     frame.setLayout(new BorderLayout());     final ZoomPane pane = new ZoomPane();     frame.add(new JScrollPane(pane));     frame.setVisible(true);     SwingUtilities.invokeLater(new Runnable() {         @Override         public void run() {  pane.centerInViewport();         }     }); }        });    }    protected class ZoomPane extends JPanel {        private Image background;        private Image scaled;        private float zoom = 1f;        private Dimension scaledSize;        private JViewport con;        public ZoomPane() { try {     background = ImageIO.read(new File("..."));     scaled = background;     scaledSize = new Dimension(background.getWidth(this), background.getHeight(this)); } catch (IOException ex) {     ex.printStackTrace(); } InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW); ActionMap am = getActionMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0), "plus"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, InputEvent.SHIFT_DOWN_MASK), "plus"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0), "minus"); am.put("plus", new AbstractAction() {     @Override     public void actionPerformed(ActionEvent e) {         setZoom(getZoom() + 0.1f);     } }); am.put("minus", new AbstractAction() {     @Override     public void actionPerformed(ActionEvent e) {         setZoom(getZoom() - 0.1f);     } }); setFocusable(true); requestFocusInWindow();        }        @Override        public void addNotify() { super.addNotify();        }        public float getZoom() { return zoom;        }        public void setZoom(float value) { if (zoom != value) {     zoom = value;     if (zoom < 0) {         zoom = 0f;     }     int width = (int) Math.floor(background.getWidth(this) * zoom);     int height = (int) Math.floor(background.getHeight(this) * zoom);     scaled = background.getScaledInstance(width, height, Image.SCALE_SMOOTH);     scaledSize = new Dimension(width, height);     if (getParent() instanceof JViewport) {         int centerX = width / 2;         int centerY = height / 2;         JViewport parent = (JViewport) getParent();         Rectangle viewRect = parent.getViewRect();         viewRect.x = centerX - (viewRect.width / 2);         viewRect.y = centerY - (viewRect.height / 2);         scrollRectToVisible(viewRect);     }     invalidate();     repaint(); }        }        @Override        public Dimension getPreferredSize() { return scaledSize;        }        @Override        protected void paintComponent(Graphics g) { super.paintComponent(g); if (scaled != null) {     g.drawImage(scaled, 0, 0, this); }        }        protected void centerInViewport() { Container container = getParent(); if (container instanceof JViewport) {     JViewport port = (JViewport) container;     Rectangle viewRect = port.getViewRect();     int width = getWidth();     int height = getHeight();     viewRect.x = (width - viewRect.width) / 2;     viewRect.y = (height - viewRect.height) / 2;     scrollRectToVisible(viewRect); }        }    }}

至于你的为什么不起作用,我不能说,我不能运行这个例子,但是也许这至少会给你一些想法…



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号