سؤال

أنا أكتب التطبيق لدينا المصممين تريد استخدام التدرجات بعض الخلفيات على عدد قليل من المواد المركبة.

كتبت البرمجية التالية:

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);

        }
    });

هذا يعتمد التدرج غرامة على المركب ، ولكن علينا تسمية/CLabels, اللوحات و الروابط في أعلى المركب.

في هذه المناطق الخلفية فقط رمادي عادي تحصل عند وضع قماش فارغ.

لقد حاولت إجبار التسميات وراثة الخلفية مثل ذلك:

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

ولكن هذا يترك لي نفس الافتراضي رمادي وليس التدرج وراء المكونات على أعلى المركب.

أي اقتراحات من أجل الحصول على التدرج إلى أن خلفية كل عنصر ؟

لن يكون مقابل رسم التدرج على gc مع صورة الموردة ثم وضع خلفية تلك الصورة.إلا أن هذه الطريقة لم تعمل في جميع مركب أو أي من عناصره.

كما أنه ليس من الممكن بالنسبة لي أن التدرج تعيين بشكل فردي على حد علمي.نحن نريد كل مركب إلى أن تكون واحدة موحدة تتدفق التدرج.

[تحرير] أنا حملت سبيل المثال تصل twitpic هنا.

شكرا

براين Gianforcaro

هل كانت مفيدة؟

المحلول

استخدام مركب.setBackgroundMode(سبحانه وتعالى.INHERIT_DEFAULT), ولكن لا ترسم مركب مباشرة - ترسم صورة و تعيين كصورة خلفية باستخدام مركب.setBackgroundImage(صورة).إلا إذا أنا في عداد المفقودين خدعة ، وهذا يعني لديك فقط لتجديد الصورة عندما المركب حجمها أيضا.

يجب أن تكون قادرة على قطع'n'paste هذا الكود كما هو رؤية ما أعنيه:

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);

أيضا أن يكون حريصا على التخلص من أي لون حالات خلق لكم بعد الانتهاء معهم.وإلا سوف تسرب موارد النظام و تشغيل في نهاية المطاف.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top