質問

私はアプリを書いていますが、デザイナーはいくつかのコンポジットの背景の一部にグラデーションを使用したいと考えています。

次のコードを書きました:

composite.addListener (SWT.Paint, new Listener () {
    public void handleEvent (Event e) {
            GC gc = e.gc;
            Rectangle rect = composite.getClientArea ();
            Color color1 = new Color (display, 0, 0, 0);
            Color color2 = new Color (display, 255, 255, 255);
            gc.setForeground(color1);
            gc.setBackground(color2);
            gc.fillGradientRectangle (rect.x, rect.y, rect.width, rect.height , true);

        }
    });

これにより、コンポジット上にグラデーションが細かく描画されますが、Label / CLabels、Canvas、およびLinksがコンポジットの上にあります。

これらの領域では、背景は空のキャンバスを描くときに得られる単なる灰色です。

次のように、ラベルに背景を強制的に継承させようとしました:

label.setBackgroundMode(SWT.INHERIT_DEFAULT) //SWT.INHERIT_FORCE Doesn't work either

ただし、これにより、同じデフォルトのグレーが表示され、Compositeの上にあるコンポーネントの後ろにグラデーションがなくなります。

グラデーションを各要素の背景にするための提案

画像を指定してgcにグラデーションを描画し、その画像に背景を設定することに反対しません。ただし、そのメソッドはまったく機能していません。コンポジットまたはその要素のいずれかです。

また、自分の知識に応じてグラデーションを個別に設定することはできません。複合材料全体を1つの均一なフローグラデーションにする必要があります。

[編集] twitpic こちらまで例をアップロードしました。

ありがとう、

ブライアン・ジャンフォルカロ

役に立ちましたか?

解決

composite.setBackgroundMode(SWT.INHERIT_DEFAULT)を使用するが、コンポジットを直接ペイントしない-イメージをペイントし、 composite.setBackgroundImage(Image)。トリックを逃さない限り、これはコンポジットのサイズが変更されたときにのみ画像を再生成する必要があることを意味します。

このコードをそのままカットアンドペーストして、私が言っていることを確認できるはずです:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * SWT composite with transparent label
 * 
 * @author McDowell
 */
public class Sweet {

    private Image imageGradient;
    private Label label;
    private Composite composite;

    private void createComponents(Shell parent) {
        composite = new Composite(parent, SWT.NONE);
        composite.addListener(SWT.Resize, new Listener() {
            public void handleEvent(Event e) {
                changeImage();
            }
        });
        composite.setLayout(new FormLayout());
        composite.setBackgroundMode(SWT.INHERIT_DEFAULT);

        label = new Label(composite, SWT.None);
        label.setText("Hello, World!");
    }

    private void changeImage() {
        Image oldImage = imageGradient;

        Display display = composite.getDisplay();
        Rectangle rect = composite.getClientArea();
        imageGradient = new Image(display, rect.width, rect.height);
        GC gc = new GC(imageGradient);
        try {
            Color color1 = new Color(display, 200, 200, 255);
            try {
                Color color2 = new Color(display, 255, 255, 255);
                try {
                    gc.setForeground(color1);
                    gc.setBackground(color2);
                    gc.fillGradientRectangle(rect.x, rect.y, rect.width,
                            rect.height, true);
                } finally {
                    color2.dispose();
                }
            } finally {
                color1.dispose();
            }
        } finally {
            gc.dispose();
        }
        composite.setBackgroundImage(imageGradient);

        if (oldImage != null) {
            oldImage.dispose();
        }
    }

    private void openShell(Display display) {
        Shell shell = new Shell(display);
        try {
            shell.setSize(200, 100);
            shell.setLayout(new FillLayout());
            createComponents(shell);
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } finally {
            if (!shell.isDisposed()) {
                shell.dispose();
            }
        }
    }

    public void run() {
        Display display = Display.getDefault();
        try {
            openShell(display);
        } finally {
            display.dispose();
        }
    }

    public void dispose() {
        if (imageGradient != null) {
            imageGradient.dispose();
        }
    }

    public static void main(String[] args) {
        Sweet sweet = new Sweet();
        try {
            sweet.run();
        } finally {
            sweet.dispose();
        }
    }

}

他のヒント

最初に試すことは、ウィジェットから画像をキャプチャし、子ウィジェットが配置されている画像の部分を直接子ウィジェットにペイントします。

うまくいかない場合は、両方のウィジェットに同じペイントリスナーを使用してみてください。ただし、GCを変換してから座標をラベルの座標空間に変換した後、GCの変換を設定する前に

Listener listener = new Listener () {
  public void handleEvent (Event e) {
    GC gc = e.gc;
    Rectangle rect = composite.getClientArea ();
    Point offset = ((Control)e.widget).toControl(composite.toDisplay(0, 0));
    Color color1 = new Color (display, 0, 0, 0);
    Color color2 = new Color (display, 255, 255, 255);
    gc.setForeground(color1);
    gc.setBackground(color2);
    gc.fillGradientRectangle (rect.x + offset.x, rect.y + offset.y,
        rect.width, rect.height , true);
  }
}
composite.addListener (SWT.Paint, listener);
label.addListener(SWT.Paint, listener);

また、作成後は、作成したColorインスタンスを破棄するよう注意してください。そうしないと、システムリソースがリークし、最終的に使い果たしてしまいます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top