OmegaT の背景に画像を表示します。
メインウィンドウの背景描画処理を新規の MoeRootPaneUI クラスに移動し、不要になった MoeLayeredPane クラスを削除
| @@ -1,123 +0,0 @@ | ||
| 1 | -/************************************************************************** | |
| 2 | - Moenizer - Allow to set background image for OmegaT. | |
| 3 | - | |
| 4 | - Copyright (C) 2013-2014 Yu Tang | |
| 5 | - Home page: http://sourceforge.jp/users/yu-tang/ | |
| 6 | - Support center: http://sourceforge.jp/users/yu-tang/pf/Moenizer/ | |
| 7 | - | |
| 8 | - This file is part of plugin for OmegaT. | |
| 9 | - http://www.omegat.org/ | |
| 10 | - | |
| 11 | - License: GNU GPL version 3 or (at your option) any later version. | |
| 12 | - | |
| 13 | - You should have received a copy of the GNU General Public License | |
| 14 | - along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 15 | - **************************************************************************/ | |
| 16 | - | |
| 17 | -package jp.sourceforge.users.yutang.omegat.plugin.moenizer; | |
| 18 | - | |
| 19 | -import java.awt.AlphaComposite; | |
| 20 | -import java.awt.Graphics; | |
| 21 | -import java.awt.Graphics2D; | |
| 22 | -import java.awt.image.BufferedImage; | |
| 23 | -import java.awt.image.WritableRaster; | |
| 24 | -import javax.swing.JLayeredPane; | |
| 25 | - | |
| 26 | -/** | |
| 27 | - * 背景をカスタマイズ可能な {@link JLayeredPane} です。 | |
| 28 | - * OmegaT のメインウィンドウ(フレーム)の内部構成は、次のような階層構造に | |
| 29 | - * なっています。 | |
| 30 | - * <pre> | |
| 31 | - * JRootPane | |
| 32 | - * + layeredPane (JLayeredPane) | |
| 33 | - * | + JMenuBar | |
| 34 | - * | + contentPane (Component) | |
| 35 | - * + glassPane (Component) | |
| 36 | - * </pre> | |
| 37 | - * 本クラスは、この中の <code>layeredPane</code> の代替クラスです。本来の <code>layeredPane</code> を | |
| 38 | - * 本クラスのインスタンスで置き換えることにより、背景をカスタマイズします。 | |
| 39 | - * | |
| 40 | - * @author Yu-Tang | |
| 41 | - */ | |
| 42 | -public class MoeLayeredPane extends JLayeredPane { | |
| 43 | - | |
| 44 | - private BufferedImage image = null; | |
| 45 | - private BufferedImage tiledImage = null; | |
| 46 | - private final float DEFAULT_ALPHA = 0.5f; | |
| 47 | - private float alpha = DEFAULT_ALPHA; | |
| 48 | - private int paintMarginTop = 0; | |
| 49 | - | |
| 50 | - public MoeLayeredPane() { | |
| 51 | - super(); | |
| 52 | - } | |
| 53 | - | |
| 54 | - public void setPaintMarginTop(int paintMarginTop) { | |
| 55 | - this.paintMarginTop = paintMarginTop; | |
| 56 | - } | |
| 57 | - | |
| 58 | - public void setBackground(BufferedImage image) { | |
| 59 | - setPicture(image); | |
| 60 | - this.repaint(); | |
| 61 | - } | |
| 62 | - | |
| 63 | - public void setOpacity(float opacity) { | |
| 64 | - this.alpha = opacity; | |
| 65 | - } | |
| 66 | - | |
| 67 | - @Override | |
| 68 | - protected void paintComponent(Graphics g) { | |
| 69 | - if (image != null) { | |
| 70 | - Graphics2D g2 = (Graphics2D) g.create(); | |
| 71 | - g2.setComposite(makeComposite(alpha)); | |
| 72 | - | |
| 73 | - // 当初 TexturePaint を用意して、 | |
| 74 | - // g2.setPaint(paint); | |
| 75 | - // g2.fillRect(0, 0, getWidth(), getHeight()); | |
| 76 | - // などとしていたが、メニューバーなどが不透過に設定された場合に | |
| 77 | - // 背景の描画開始位置を下げる必要があり、テクスチャによるタイリング | |
| 78 | - // では対応できなかった(描画開始位置を下げてもテクスチャの位置が | |
| 79 | - // 変わらないので、テクスチャの途中から描画が始まる感じ)。 | |
| 80 | - // そこで、自前でタイリング画像を作って保持し、その一枚絵を貼る路線 | |
| 81 | - // に変更する。 | |
| 82 | - int width = getWidth(); | |
| 83 | - int height = getHeight(); | |
| 84 | - if (width > tiledImage.getWidth() || height > tiledImage.getHeight()) { | |
| 85 | - tiledImage = createTiledImage(image, width, height); | |
| 86 | - } | |
| 87 | - g2.drawImage(tiledImage, 0, paintMarginTop, this); | |
| 88 | - | |
| 89 | - g2.dispose(); | |
| 90 | - } | |
| 91 | - super.paintComponent(g); | |
| 92 | - } | |
| 93 | - | |
| 94 | - private AlphaComposite makeComposite(float alpha) { | |
| 95 | - int type = AlphaComposite.SRC_OVER; | |
| 96 | - return AlphaComposite.getInstance(type, alpha); | |
| 97 | - } | |
| 98 | - | |
| 99 | - private void setPicture(BufferedImage image) { | |
| 100 | - this.image = image; | |
| 101 | - | |
| 102 | - if (image == null) { | |
| 103 | - this.tiledImage = null; | |
| 104 | - } else { | |
| 105 | - this.tiledImage = createTiledImage(image, getWidth(), getHeight()); | |
| 106 | - } | |
| 107 | - } | |
| 108 | - | |
| 109 | - private BufferedImage createTiledImage(BufferedImage image, int width, int height) { | |
| 110 | - WritableRaster r1 = image.getRaster(); | |
| 111 | - WritableRaster r2 = r1.createCompatibleWritableRaster(width, height); | |
| 112 | - int w = r1.getWidth(); | |
| 113 | - int h = r1.getHeight(); | |
| 114 | - for (int x = 0; x < width; x += w) { | |
| 115 | - for (int y = 0; y < height; y += h) { | |
| 116 | - r2.setRect(x, y, r1); | |
| 117 | - } | |
| 118 | - } | |
| 119 | - BufferedImage ret = new BufferedImage(width, height, image.getType()); | |
| 120 | - ret.setData(r2); | |
| 121 | - return ret; | |
| 122 | - } | |
| 123 | -} |
| @@ -0,0 +1,77 @@ | ||
| 1 | +/************************************************************************** | |
| 2 | + Moenizer - Allow to set background image for OmegaT. | |
| 3 | + | |
| 4 | + Copyright (C) 2014 Yu Tang | |
| 5 | + Home page: http://sourceforge.jp/users/yu-tang/ | |
| 6 | + Support center: http://sourceforge.jp/users/yu-tang/pf/Moenizer/ | |
| 7 | + | |
| 8 | + This file is part of plugin for OmegaT. | |
| 9 | + http://www.omegat.org/ | |
| 10 | + | |
| 11 | + License: GNU GPL version 3 or (at your option) any later version. | |
| 12 | + | |
| 13 | + You should have received a copy of the GNU General Public License | |
| 14 | + along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 15 | + **************************************************************************/ | |
| 16 | + | |
| 17 | +package jp.sourceforge.users.yutang.omegat.plugin.moenizer; | |
| 18 | + | |
| 19 | +import java.awt.Color; | |
| 20 | +import java.awt.Graphics; | |
| 21 | +import java.awt.image.BufferedImage; | |
| 22 | +import javax.swing.JComponent; | |
| 23 | +import javax.swing.plaf.basic.BasicRootPaneUI; | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * 背景をカスタマイズ可能な <code>RootPaneUI</code> です。 | |
| 27 | + * OmegaT のメインウィンドウ(フレーム)の内部構成は、次のような階層構造に | |
| 28 | + * なっています。 | |
| 29 | + * <pre> | |
| 30 | + * JRootPane | |
| 31 | + * + layeredPane (JLayeredPane) | |
| 32 | + * | + JMenuBar | |
| 33 | + * | + contentPane (Component) | |
| 34 | + * + glassPane (Component) | |
| 35 | + * </pre> | |
| 36 | + * 本クラスは、この中の <code>JRootPane</code> 用の UI 代替クラスです。 | |
| 37 | + * 本来の <code>UI</code> を本クラスのインスタンスで置き換えることにより、 | |
| 38 | + * 背景をカスタマイズします。 | |
| 39 | + * | |
| 40 | + * @author Yu-Tang | |
| 41 | + */ | |
| 42 | +public class MoeRootPaneUI extends BasicRootPaneUI implements IMoeUI { | |
| 43 | + | |
| 44 | + private MoeUIDelegator uiDelegator = null; | |
| 45 | + | |
| 46 | + public MoeRootPaneUI() { | |
| 47 | + super(); | |
| 48 | + uiDelegator = new MoeUIDelegator(); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public void installUI(JComponent c) { | |
| 53 | + super.installUI(c); | |
| 54 | + | |
| 55 | + uiDelegator.setComponent(c); | |
| 56 | + } | |
| 57 | + | |
| 58 | + @Override | |
| 59 | + public void setBackground(BufferedImage image) { | |
| 60 | + uiDelegator.setBackground(image); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public void setBackground(Color color) { | |
| 65 | + uiDelegator.setBackground(color); | |
| 66 | + } | |
| 67 | + | |
| 68 | + @Override | |
| 69 | + public void setOpacity(float opacity) { | |
| 70 | + uiDelegator.setOpacity(opacity); | |
| 71 | + } | |
| 72 | + | |
| 73 | + @Override | |
| 74 | + public void paint(Graphics g, JComponent c) { | |
| 75 | + uiDelegator.paintBackground(g); | |
| 76 | + } | |
| 77 | +} |