Java源码示例:org.eclipse.draw2d.SWTGraphics

示例1
@Override
public void run() {
	IFigure contents = railroadView.getContents();
	if (contents != null) {
		FileDialog fileDialog = new FileDialog(this.railroadView.getSite().getShell(), SWT.SAVE);
		fileDialog.setFilterExtensions(new String[] { "*.png" });
		fileDialog.setText("Choose diagram file");
		String fileName = fileDialog.open();
		if (fileName == null) {
			return;
		}
		Dimension preferredSize = contents.getPreferredSize();
		Image image = new Image(Display.getDefault(), preferredSize.width + 2 * PADDING, preferredSize.height + 2
				* PADDING);
		GC gc = new GC(image);
		SWTGraphics graphics = new SWTGraphics(gc);
		graphics.translate(PADDING, PADDING);
		graphics.translate(contents.getBounds().getLocation().getNegated());
		contents.paint(graphics);
		ImageData imageData = image.getImageData();
		ImageLoader imageLoader = new ImageLoader();
		imageLoader.data = new ImageData[] { imageData };
		imageLoader.save(fileName, SWT.IMAGE_PNG);
	}
}
 
示例2
@Override
public void run() {
	if (file == null)
		return;
	log.trace("export product graph as image: {}", file);
	ScalableRootEditPart editPart = (ScalableRootEditPart) editor.getGraphicalViewer().getRootEditPart();
	IFigure rootFigure = editPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
	Rectangle bounds = rootFigure.getBounds();
	Image img = new Image(null, bounds.width, bounds.height);
	GC imageGC = new GC(img);
	Graphics graphics = new SWTGraphics(imageGC);
	rootFigure.paint(graphics);
	ImageLoader imgLoader = new ImageLoader();
	imgLoader.data = new ImageData[] { img.getImageData() };
	imgLoader.save(file.getAbsolutePath(), SWT.IMAGE_PNG);
}
 
示例3
@Override
protected Image getInternalXYGraphSnapShot(IXYGraph xyGraph) {
	Rectangle bounds = xyGraph.getBounds();
	Image image = new Image(null, bounds.width + 6, bounds.height + 6);
	GC gc = GraphicsUtil.createGC(image);
	SWTGraphics graphics = new SWTGraphics(gc);
	// Needed because clipping is not set in GTK2
	graphics.setClip(new Rectangle(0, 0, image.getBounds().width, image.getBounds().height));
	graphics.translate(-bounds.x + 3, -bounds.y + 3);
	graphics.setForegroundColor(xyGraph.getForegroundColor());
	graphics.setBackgroundColor(xyGraph.getBackgroundColor());
	xyGraph.paint(graphics);
	gc.dispose();
	return image;
}
 
示例4
private Image createImage() {
	try {
		ScalableRootEditPart root = (ScalableRootEditPart) sankeyDiagram.getGraphicalViewer().getRootEditPart();
		IFigure figure = root.getLayer(LayerConstants.PRINTABLE_LAYERS);
		Rectangle bounds = figure.getBounds();
		Image img = new Image(null, bounds.width, bounds.height);
		GC imageGC = new GC(img);
		Graphics graphics = new SWTGraphics(imageGC);
		figure.paint(graphics);
		return img;
	} catch (Exception e) {
		log.error("Could not create image", e);
		return null;
	}
}
 
示例5
/**
 * Given a GEF GraphicalViewer, this method creates a save file dialog and
 * will save the contents of the viewer as an image file. Supported
 * extensions include PNG, JPG, and BMP.
 * 
 * @param viewer
 *            The GraphicalViewer to save as an image.
 */
protected static void saveViewerImage(GraphicalViewer viewer) {

	// Store the allowed extensions in a HashMap.
	HashMap<String, Integer> extensions = new HashMap<String, Integer>(4);
	extensions.put("png", SWT.IMAGE_PNG);
	extensions.put("jpg", SWT.IMAGE_JPEG);
	extensions.put("bmp", SWT.IMAGE_BMP);

	// Make the array of strings needed to pass to the file dialog.
	String[] extensionStrings = new String[extensions.keySet().size()];
	int i = 0;
	for (String extension : extensions.keySet()) {
		extensionStrings[i++] = "*." + extension;
	}

	// Create the file save dialog.
	FileDialog fileDialog = new FileDialog(viewer.getControl().getShell(),
			SWT.SAVE);
	fileDialog.setFilterExtensions(extensionStrings);
	fileDialog.setOverwrite(true);

	// Get the path of the new/overwritten image file.
	String path = fileDialog.open();

	// Return if the user cancelled.
	if (path == null) {
		return;
	}

	// Get the image type to save from the path's extension.
	String[] splitPath = path.split("\\.");
	int extensionSWT = extensions.get(splitPath[splitPath.length - 1]);

	// Get the root EditPart and its draw2d Figure.
	SimpleRootEditPart rootEditPart = (SimpleRootEditPart) viewer
			.getRootEditPart();
	IFigure figure = rootEditPart.getFigure();

	// Get the image from the Figure.
	org.eclipse.draw2d.geometry.Rectangle bounds = figure.getBounds();
	Image image = new Image(null, bounds.width, bounds.height);
	GC gc = new GC(image);
	SWTGraphics g = new SWTGraphics(gc);
	figure.paint(g);
	g.dispose();
	gc.dispose();

	// Save the file.
	ImageLoader loader = new ImageLoader();
	loader.data = new ImageData[] { image.getImageData() };
	loader.save(path, extensionSWT);
	image.dispose();

	return;
}
 
示例6
/**
 * Creates a new PrinterGraphics with Graphics g, using Printer p ;
 * 
 * @param g
 *            Graphics object to draw with
 * @param p
 *            Printer to print to
 */
public CompositePrinterGraphics( SWTGraphics g, Device p )
{
	super( g );
	printer = p;
}