001: /*******************************************************************************
002: * Copyright (c) 2007-2008 Kirill Grouchnikov and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *******************************************************************************/package test;
008:
009: import java.awt.*;
010: import java.awt.Color;
011: import java.awt.image.*;
012: import java.io.IOException;
013:
014: import javax.swing.*;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.graphics.*;
018: import org.eclipse.swt.graphics.Image;
019: import org.eclipse.swt.widgets.Display;
020:
021: public class MainSimplified extends JFrame {
022: private BufferedImage bi;
023:
024: protected class SwtPanel extends JPanel {
025: @Override
026: protected void paintComponent(Graphics g) {
027: // the goal is to have red fill and then show the SWT-rendered text
028: // on top of it.
029: g.setColor(Color.red);
030: g.fillRect(0, 0, getWidth(), getHeight());
031: g.drawImage(bi, 0, 0, null);
032: }
033: }
034:
035: public MainSimplified() throws IOException {
036: this .setLayout(new BorderLayout());
037: SwtPanel panel = new SwtPanel();
038: panel.setOpaque(true);
039: this .add(panel, BorderLayout.CENTER);
040:
041: int width = 400;
042: int height = 150;
043:
044: this .setSize(width, height);
045: this .setLocationRelativeTo(null);
046: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
047:
048: Display display = Display.getDefault();
049:
050: PaletteData PALETTE_DATA = new PaletteData(0xFF0000, 0xFF00,
051: 0xFF);
052: ImageData swtImageData = new ImageData(width, height, 24,
053: PALETTE_DATA);
054: // the following doesn't have any effect when the SWT background is set
055: swtImageData.alpha = 0;
056:
057: // Create SWT image and render some text in it
058: Image swtImage = new Image(display, swtImageData);
059: GC gc = new GC(swtImage);
060: // if the following two lines are removed, the image is all black
061: gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
062: gc.fillRectangle(0, 0, width, height);
063: gc.setTextAntialias(SWT.DEFAULT);
064: gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
065:
066: // Pass "true" as the last parameter to preserve the original background
067: // pixels
068: gc.drawText(
069: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
070: 10, 10, true);
071:
072: ImageData imageData = swtImage.getImageData();
073: // copy bits from SWT image to a AWT image
074: this .bi = new BufferedImage(width, height,
075: BufferedImage.TYPE_INT_ARGB);
076:
077: int redMask = imageData.palette.redMask;
078: int redShift = imageData.palette.redShift;
079: int greenMask = imageData.palette.greenMask;
080: int greenShift = imageData.palette.greenShift;
081: int blueShift = imageData.palette.blueShift;
082: int blueMask = imageData.palette.blueMask;
083:
084: int[] lineData = new int[width];
085:
086: // Get the raw data buffer and write directly to it.
087: // Compared to BufferedImage.setRGB this speeds up the
088: // image creation by the factor of three.
089: WritableRaster srcRaster = this .bi.getRaster();
090: DataBufferInt dataBuffer = (DataBufferInt) srcRaster
091: .getDataBuffer();
092: int[] rawData = dataBuffer.getData();
093:
094: for (int y = 0; y < height; y++) {
095: imageData.getPixels(0, y, width, lineData, 0);
096: // Analyze each pixel value in the line
097: for (int x = 0; x < lineData.length; x++) {
098: // Extract the red, green and blue component -
099: // see org/eclipse/swt/internal/image/PngEncoder.java
100: int pixelValue = lineData[x];
101: int alpha = imageData.getAlpha(x, y);
102:
103: int r = pixelValue & redMask;
104: r = (redShift < 0) ? r >>> -redShift : r << redShift;
105: int g = pixelValue & greenMask;
106: g = (greenShift < 0) ? g >>> -greenShift
107: : g << greenShift;
108: int b = pixelValue & blueMask;
109: b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
110:
111: rawData[x + y * width] = alpha << 24 | r << 16 | g << 8
112: | b;
113: }
114: swtImage.dispose();
115: gc.dispose();
116: }
117: }
118:
119: public static void main(String[] args) {
120: SwingUtilities.invokeLater(new Runnable() {
121: public void run() {
122: try {
123: new MainSimplified().setVisible(true);
124: } catch (IOException ioe) {
125: ioe.printStackTrace();
126: }
127: }
128: });
129: }
130: }
|