문제
저는 앱을 작성하고 디자이너는 몇몇 복합재에 대한 일부 배경에 그라디언트를 사용하려고합니다.
다음 코드를 썼습니다.
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까지 예제를 업로드했습니다 여기.
감사,
Brian Gianforcaro
해결책
사용 composite.setbackgroundmode (swt.inherit_default), 그러나 복합재를 직접 페인트하지 마십시오 - 이미지를 페인트하고 사용하는 배경 이미지로 설정하십시오. composite.setbackgroundimage (이미지). 속임수가 없으면 복합재가 크기가 커지면 이미지를 재생해야한다는 의미입니다.
내가 의미하는 바와 같이이 코드를 삭감 할 수 있어야합니다.
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);
또한, 당신이 그들과 함께 작성한 후에 만든 색상 인스턴스를 처분하도록주의하십시오. 그렇지 않으면 시스템 리소스를 누출하고 결국 다 떨어집니다.