[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform.swt] Re: Antialiased rendering on a transparent image
|
- From: kirillcool@xxxxxxxxx (Kirill Grouchnikov)
- Date: Wed, 21 Nov 2007 07:02:56 +0000 (UTC)
- Newsgroups: eclipse.platform.swt
- Organization: Eclipse
- User-agent: NewsPortal/0.36 (http://florian-amrhein.de/newsportal)
Here is a small test app that does the following:
1. Creates a Swing frame
2. Creates an SWT image and renders some text in it
3. Converts the SWT image into an AWT image
4. Uses the AWT image to paint on the Swing frame
So, i would like to have Swing control the frame background (red in this
example) and SWT paint the text (black in this example). How do i make the
SWT image have completely transparent pixels wherever there is no text,
completely black pixels when there is full "value" for text and partially
translucent pixels on the "outliers"?
Thanks
Kirill
import java.awt.*;
import java.awt.Color;
import java.awt.image.*;
import java.io.IOException;
import javax.swing.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
public class MainSimplified extends JFrame {
private BufferedImage bi;
protected class SwtPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
// the goal is to have red fill and then show the SWT-rendered text
// on top of it.
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(bi, 0, 0, null);
}
}
public MainSimplified() throws IOException {
this.setLayout(new BorderLayout());
SwtPanel panel = new SwtPanel();
panel.setOpaque(true);
this.add(panel, BorderLayout.CENTER);
int width = 400;
int height = 150;
this.setSize(width, height);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Display display = Display.getDefault();
PaletteData PALETTE_DATA = new PaletteData(0xFF0000, 0xFF00, 0xFF);
ImageData swtImageData = new ImageData(width, height, 24, PALETTE_DATA);
// the following doesn't have any effect when the SWT background is set
swtImageData.alpha = 0;
// Create SWT image and render some text in it
Image swtImage = new Image(display, swtImageData);
GC gc = new GC(swtImage);
// if the following two lines are removed, the image is all black
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
gc.fillRectangle(0, 0, width, height);
gc.setTextAntialias(SWT.DEFAULT);
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
// Pass "true" as the last parameter to preserve the original background
// pixels
gc.drawText("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 10,
10, true);
ImageData imageData = swtImage.getImageData();
// copy bits from SWT image to a AWT image
this.bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int redMask = imageData.palette.redMask;
int redShift = imageData.palette.redShift;
int greenMask = imageData.palette.greenMask;
int greenShift = imageData.palette.greenShift;
int blueShift = imageData.palette.blueShift;
int blueMask = imageData.palette.blueMask;
int[] lineData = new int[width];
// Get the raw data buffer and write directly to it.
// Compared to BufferedImage.setRGB this speeds up the
// image creation by the factor of three.
WritableRaster srcRaster = this.bi.getRaster();
DataBufferInt dataBuffer = (DataBufferInt) srcRaster.getDataBuffer();
int[] rawData = dataBuffer.getData();
for (int y = 0; y < height; y++) {
imageData.getPixels(0, y, width, lineData, 0);
// Analyze each pixel value in the line
for (int x = 0; x < lineData.length; x++) {
// Extract the red, green and blue component -
// see org/eclipse/swt/internal/image/PngEncoder.java
int pixelValue = lineData[x];
int alpha = imageData.getAlpha(x, y);
int r = pixelValue & redMask;
r = (redShift < 0) ? r >>> -redShift : r << redShift;
int g = pixelValue & greenMask;
g = (greenShift < 0) ? g >>> -greenShift : g << greenShift;
int b = pixelValue & blueMask;
b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
rawData[x + y * width] = alpha << 24 | r << 16 | g << 8 | b;
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new MainSimplified().setVisible(true);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
}
}