怎么把Canvas的背景设置为透明
1个回答
展开全部
java swing中将组件的背景设置成透明的示例如下:
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
public class Talpha {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
//使用paintlistener,保证每次均重新绘制。
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
GC gc = e.gc;
//读图像
ImageData imageData = new ImageData("1.jpg");
//这里是建立从左到右的渐进Alpha。
byte[] alphaValues = new byte[imageData.height * imageData.width];
for (int j = 0; j < imageData.height; j++) {
for (int i = 0; i < imageData.width; i++) {
alphaValues[j * imageData.width + i] = (byte) (255 - 255
* i / imageData.width);
}
}
imageData.alphaData = alphaValues;
Image image = new Image(display, imageData);
//绘制
gc.drawImage(image,0,0);//关键点是这里设置背景颜色
}
});
FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;
shell.setLayout(fillLayout);
shell.setSize(200, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
public class Talpha {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
//使用paintlistener,保证每次均重新绘制。
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
GC gc = e.gc;
//读图像
ImageData imageData = new ImageData("1.jpg");
//这里是建立从左到右的渐进Alpha。
byte[] alphaValues = new byte[imageData.height * imageData.width];
for (int j = 0; j < imageData.height; j++) {
for (int i = 0; i < imageData.width; i++) {
alphaValues[j * imageData.width + i] = (byte) (255 - 255
* i / imageData.width);
}
}
imageData.alphaData = alphaValues;
Image image = new Image(display, imageData);
//绘制
gc.drawImage(image,0,0);//关键点是这里设置背景颜色
}
});
FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;
shell.setLayout(fillLayout);
shell.setSize(200, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询