在SWT中用这种方式重画的动态图片,为什么设定不了图片的背景颜色呢?

问题描述

在SWT中用这种方式重画的动态图片,为什么设定不了图片的背景颜色呢?

在SWT中插入的动态等待图片,设定的dg背景色,画的时候无效,这是为什么呢?求高手相助~**

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class ImageViewers extends Canvas {

protected Point origin = new Point(0, 0);
protected Image image;
protected ImageData[] imageDatas;
protected Image[] images;
protected int current;

private int repeatCount;
private Runnable animationTimer;
private Color bg;
private Display display;

public ImageViewers(Composite parent) {
    super(parent, SWT.NO_BACKGROUND);

    bg = new Color(null, 248, 85, 40);
    display = getDisplay();
    addListeners();
}

/**
 * @param repeatCount
 *            0 forever
 */
public void setImages(ImageData[] imageDatas, int repeatCount) {
    checkWidget();

    this.image = null;
    this.imageDatas = imageDatas;
    this.repeatCount = repeatCount;
    convertImageDatasToImages();
    startAnimationTimer();
    redraw();
}

@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
    checkWidget();

    Image image = getCurrentImage();
    if (image != null) {
        Rectangle rect = image.getBounds();
        Rectangle trim = computeTrim(0, 0, rect.width, rect.height);
        return new Point(trim.width, trim.height);
    }

    return new Point(wHint, hHint);
}

@Override
public void dispose() {
    if (image != null)
        image.dispose();

    if (images != null)
        for (int i = 0; i < images.length; i++)
            images[i].dispose();

    super.dispose();
}

protected void paint(Event e) {
    Image image = getCurrentImage();
    if (image == null)
        return;

    GC gc = e.gc;
    gc.drawImage(image, origin.x, origin.y);

    gc.setBackground(bg);
    Rectangle rect = image.getBounds();
    Rectangle client = getClientArea();
    int marginWidth = client.width - rect.width;
    if (marginWidth > 0) {
        gc.fillRectangle(rect.width, 0, marginWidth, client.height);
    }
    int marginHeight = client.height - rect.height;
    if (marginHeight > 0) {
        gc.fillRectangle(0, rect.height, client.width, marginHeight);
    }
}

void addListeners() {
    addListener(SWT.Paint, new Listener() {
        public void handleEvent(Event e) {
            paint(e);
        }
    });
}

void convertImageDatasToImages() {
    images = new Image[imageDatas.length];

    // Step 1: Determine the size of the resulting images.
    int width = imageDatas[0].width;
    int height = imageDatas[0].height;

    // Step 2: Construct each image.
    int transition = SWT.DM_FILL_BACKGROUND;
    for (int i = 0; i < imageDatas.length; i++) {
        ImageData id = imageDatas[i];
        images[i] = new Image(display, width, height);
        GC gc = new GC(images[i]);

        // Do the transition from the previous image.
        switch (transition) {
        case SWT.DM_FILL_NONE:
        case SWT.DM_UNSPECIFIED:
            // Start from last image.
            gc.drawImage(images[i - 1], 0, 0);
            break;
        case SWT.DM_FILL_PREVIOUS:
            // Start from second last image.
            gc.drawImage(images[i - 2], 0, 0);
            break;
        default:
            // DM_FILL_BACKGROUND or anything else,
            // just fill with default background.
            gc.setBackground(bg);
            gc.fillRectangle(0, 0, width, height);
            break;
        }

        // Draw the current image and clean up.
        Image img = new Image(display, id);
        gc.drawImage(img, 0, 0, id.width, id.height, id.x, id.y, id.width,
                id.height);
        img.dispose();
        gc.dispose();

        // Compute the next transition.
        // Special case: Can't do DM_FILL_PREVIOUS on the
        // second image since there is no "second last"
        // image to use.
        transition = id.disposalMethod;
        if (i == 0 && transition == SWT.DM_FILL_PREVIOUS)
            transition = SWT.DM_FILL_NONE;
    }
}

Image getCurrentImage() {
    if (image != null)
        return image;

    if (images == null)
        return null;

    return images[current];
}

void startAnimationTimer() {
    if (images == null || images.length < 2)
        return;

    final int delay = imageDatas[current].delayTime * 10;
    display.timerExec(delay, animationTimer = new Runnable() {
        public void run() {
            if (isDisposed())
                return;

            current = (current + 1) % images.length;
            redraw();

            if (current + 1 == images.length && repeatCount != 0
                    && --repeatCount <= 0)
                return;
            display.timerExec(delay, this);
        }
    });
}

void stopAnimationTimer() {
    if (animationTimer != null)
        display.timerExec(-1, animationTimer);
}

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    ImageViewers ic = new ImageViewers(shell);

    shell.setSize(500, 300);

    ImageLoader loader = new ImageLoader();
    ImageData[] imageDatas = loader.load("E:/image/11.gif");
    ic.setImages(imageDatas, loader.repeatCount);

    ic.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

}

解决方案

http://blog.csdn.net/cuiran/article/details/7339936

解决方案二:

有大神帮忙吗~求助!!!

时间: 2024-08-03 10:30:56

在SWT中用这种方式重画的动态图片,为什么设定不了图片的背景颜色呢?的相关文章

vb实现强制重画窗口某一区域

这个例子演示了如何强制重画窗口某一区域.有时这是很需要的,例如当你使用LockWindowUpdate API 函数来加快某一控件的数据加载速度时. 新建一个工程,添加一个模块,代码如下: Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type POINTAPI X As Long Y As Long End Type Private Declare Functio

矢量图 重画 选中-跪求矢量图的重画问题

问题描述 跪求矢量图的重画问题 由于存在着大量的图形区域,每选中一个图形或取消选中或移动或变形时,都需要重画,使得程序执行效率低,跪求大神的解决思路!! 解决方案 一般这种情况都是局部更新,及只重画窗口范围的图形,并且使用双缓冲. 解决方案二: 一般这种情况都是局部更新,及只重画窗口范围的图形,并且使用双缓冲.

c++-关于MFC窗口尺寸变化重画问题

问题描述 关于MFC窗口尺寸变化重画问题 问题的链接在这里,谢谢http://zhidao.baidu.com/question/1604266407975887067.html?quesup2&oldq=1

如何把在panel中用DrawLine()方法画的直线变成图片格式

问题描述 各为朋友:我现在想在panel中用DrawLine()方法画两条直线,然后用什么办法能把刚才画的直线变成图片格式,方便以后调用.希望各位能给我点思路.谢谢 解决方案 解决方案二:直接在panel上放个bmp,画在bmp上面,然后保存解决方案三:panel.CreateGraghics解决方案四:引用1楼q309079549的回复: 直接在panel上放个bmp,画在bmp上面,然后保存 附代码:Bitmapbm=newBitmap(this.Width,this.Height);Pen

调用repaint后有打印,但是却没有重画

问题描述 menu和panel是两个JPanel,添加在Main_menu里.importjava.awt.Color;importjava.awt.Font;importjava.awt.FontMetrics;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.RenderingHints;importjava.awt.Sh

动态将彩色图片动画过渡到黑白图片的BlackAndWhiteView

动态将彩色图片动画过渡到黑白图片的BlackAndWhiteView 效果如下:  BlackAndWhiteView.h 与 BlackAndWhiteView.m // // BlackAndWhiteView.h // BlackAndWhiteView // // Created by YouXianMing on 14-10-4. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIK

动态获取当前屏幕中光标所在位置的颜色

动态 ////////////////////////////////////////////////////////////////////////// /// 程序:屏幕取色 /// 功能:动态获取当前屏幕中光标所在位置的颜色 /// 作者:黎波 /// 网名:upto(阿球) /// 邮箱:itfun@163.com /// 日期:2004年3月31日 /////////////////////////////////////////////////////////////////////

dll动态库-如何用函数实现动态库里的对话框的背景颜色,背景图片,求大神们帮帮忙!!!

问题描述 如何用函数实现动态库里的对话框的背景颜色,背景图片,求大神们帮帮忙!!! MFC里或者VC++6.0一个动态库里含有一个对话框,我想用几个函数实现①动态库里的对话框的背景颜色和背景图片,②还有就是隐藏背景图片,③隐藏对话框,④在对话框相应的位置显示文字,⑤并能设置文字的字体和颜色.

asp.net中动态生成word文档,怎么给导出的word中标题栏的加个背景颜色。

问题描述 asp.net中动态生成word文档,怎么给导出的word中标题栏的加个背景颜色. asp.net中动态生成word文档,怎么给 导出 的word中标题栏的加个背景颜色.