Re: [問題] 如何控制放在JLabel裡面的ImageIcon大小?

看板java作者 (!H45)時間16年前 (2009/04/08 16:34), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串2/2 (看更多)
/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 上面這段是為了合法散佈原始碼.... 下面才是正文 ※ 引述《zeat (Lucifer)》之銘言: : 各位好: : 如同標題, 我在JLabel裡面使用ImageIcon顯示圖片. : 但使用者如果選太大張的圖, : 整個swing視窗就會充滿著圖片... : 有什麼可以解決的辦法? 在 JavaTM Tutorials - How to Use Icons http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html 有一個附件 IconDemoApp.java : http://java.sun.com/docs/books/tutorial/uiswing/examples/components/IconDemoProject/src/components/IconDemoApp.java 內有放大縮小圖片的程式碼: /** * Resizes an image using a Graphics2D object backed by a BufferedImage. * @param srcImg - source image to scale * @param w - desired width * @param h - desired height * @return - the new resized image */ private Image getScaledImage(Image srcImg, int w, int h){ BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg, 0, 0, w, h, null); g2.dispose(); return resizedImg; } 你要不要考慮直接拿去用 (大誤 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.247.13

04/08 16:44, , 1F
註:大誤是因為直接拿去用馬上就觸犯使用條款 XDDD
04/08 16:44, 1F

04/08 19:57, , 2F
謝謝你提供方向m(_ _)m
04/08 19:57, 2F
文章代碼(AID): #19t66e0s (java)
文章代碼(AID): #19t66e0s (java)