OmegaT の背景に画像を表示します。
ペイン単位での背景制御用に MoeTextPaneUI, MoeUIDelegator, IMoeUI の3クラスを追加
| @@ -0,0 +1,116 @@ | ||
| 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.AlphaComposite; | |
| 20 | +import java.awt.Color; | |
| 21 | +import java.awt.Graphics; | |
| 22 | +import java.awt.Graphics2D; | |
| 23 | +import java.awt.image.BufferedImage; | |
| 24 | +import java.awt.image.WritableRaster; | |
| 25 | +import javax.swing.JComponent; | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * | |
| 29 | + * @author Yu-Tang | |
| 30 | + */ | |
| 31 | +public class MoeUIDelegator { | |
| 32 | + | |
| 33 | + private BufferedImage image = null; | |
| 34 | + private BufferedImage tiledImage = null; | |
| 35 | + private Color bgColor = null; | |
| 36 | + private JComponent component = null; | |
| 37 | + private final float DEFAULT_ALPHA = 0.5f; | |
| 38 | + private float alpha = DEFAULT_ALPHA; | |
| 39 | + private AlphaComposite alphaComposite = null; | |
| 40 | + | |
| 41 | + public void setOpacity(float opacity) { | |
| 42 | + this.alpha = opacity; | |
| 43 | + this.alphaComposite = getAlphaComposite(opacity); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public void setBackground(BufferedImage image) { | |
| 47 | + this.image = image; | |
| 48 | + | |
| 49 | + if (component == null) { | |
| 50 | + this.tiledImage = image; | |
| 51 | + } else { | |
| 52 | + this.tiledImage = createTiledImage(image, | |
| 53 | + component.getWidth(), component.getHeight()); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + public void setBackground(Color color) { | |
| 58 | + this.bgColor = color; | |
| 59 | + } | |
| 60 | + | |
| 61 | + public void setComponent(JComponent c) { | |
| 62 | + // change transparent component to opaque | |
| 63 | + if (!c.isOpaque()) { | |
| 64 | + c.setOpaque(true); | |
| 65 | + } | |
| 66 | + this.component = c; | |
| 67 | + } | |
| 68 | + | |
| 69 | + public void paintBackground(Graphics g) { | |
| 70 | + if (image != null) { | |
| 71 | + Graphics2D g2 = (Graphics2D) g.create(); | |
| 72 | + if (alphaComposite != null) { | |
| 73 | + g2.setComposite(alphaComposite); | |
| 74 | + } | |
| 75 | + if (bgColor != null) { | |
| 76 | + g2.setBackground(bgColor); | |
| 77 | + } | |
| 78 | + // 当初 TexturePaint を用意して、 | |
| 79 | + // g2.setPaint(paint); | |
| 80 | + // g2.fillRect(0, 0, getWidth(), getHeight()); | |
| 81 | + // などとしていたが、メニューバーなどが不透過に設定された場合に | |
| 82 | + // 背景の描画開始位置を下げる必要があり、テクスチャによるタイリング | |
| 83 | + // では対応できなかった(描画開始位置を下げてもテクスチャの位置が | |
| 84 | + // 変わらないので、テクスチャの途中から描画が始まる感じ)。 | |
| 85 | + // そこで、自前でタイリング画像を作って保持し、その一枚絵を貼る路線 | |
| 86 | + // に変更する。 | |
| 87 | + int width = component.getWidth(); | |
| 88 | + int height = component.getHeight(); | |
| 89 | + if (width > tiledImage.getWidth() || height > tiledImage.getHeight()) { | |
| 90 | + tiledImage = createTiledImage(image, width, height); | |
| 91 | + } | |
| 92 | + g2.drawImage(tiledImage, 0, 0, component); | |
| 93 | + g2.dispose(); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + | |
| 97 | + private AlphaComposite getAlphaComposite(float alpha) { | |
| 98 | + int type = AlphaComposite.SRC_OVER; | |
| 99 | + return AlphaComposite.getInstance(type, alpha); | |
| 100 | + } | |
| 101 | + | |
| 102 | + private BufferedImage createTiledImage(BufferedImage image, int width, int height) { | |
| 103 | + WritableRaster r1 = image.getRaster(); | |
| 104 | + WritableRaster r2 = r1.createCompatibleWritableRaster(width, height); | |
| 105 | + int w = r1.getWidth(); | |
| 106 | + int h = r1.getHeight(); | |
| 107 | + for (int x = 0; x < width; x += w) { | |
| 108 | + for (int y = 0; y < height; y += h) { | |
| 109 | + r2.setRect(x, y, r1); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + BufferedImage ret = new BufferedImage(width, height, image.getType()); | |
| 113 | + ret.setData(r2); | |
| 114 | + return ret; | |
| 115 | + } | |
| 116 | +} |
| @@ -0,0 +1,33 @@ | ||
| 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.image.BufferedImage; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * | |
| 24 | + * @author Yu-Tang | |
| 25 | + */ | |
| 26 | +public interface IMoeUI { | |
| 27 | + public void setBackground(BufferedImage image); | |
| 28 | + | |
| 29 | + public void setBackground(Color color); | |
| 30 | + | |
| 31 | + public void setOpacity(float opacity); | |
| 32 | + | |
| 33 | +} |
| @@ -0,0 +1,64 @@ | ||
| 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.BasicTextPaneUI; | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * | |
| 27 | + * @author Yu-Tang | |
| 28 | + */ | |
| 29 | +public class MoeTextPaneUI extends BasicTextPaneUI implements IMoeUI { | |
| 30 | + | |
| 31 | + private MoeUIDelegator uiDelegator = null; | |
| 32 | + | |
| 33 | + public MoeTextPaneUI() { | |
| 34 | + super(); | |
| 35 | + uiDelegator = new MoeUIDelegator(); | |
| 36 | + } | |
| 37 | + | |
| 38 | + @Override | |
| 39 | + public void installUI(JComponent c) { | |
| 40 | + super.installUI(c); | |
| 41 | + | |
| 42 | + uiDelegator.setComponent(c); | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + public void setBackground(BufferedImage image) { | |
| 47 | + uiDelegator.setBackground(image); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + public void setBackground(Color color) { | |
| 52 | + uiDelegator.setBackground(color); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public void setOpacity(float opacity) { | |
| 57 | + uiDelegator.setOpacity(opacity); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + protected void paintBackground(Graphics g) { | |
| 62 | + uiDelegator.paintBackground(g); | |
| 63 | + } | |
| 64 | +} |