Java Code Examples for org.eclipse.swt.widgets.Display#getDPI()

The following examples show how to use org.eclipse.swt.widgets.Display#getDPI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageCaptureExample.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Demonstrate capturing the pages of a print to in-memory images.
 * 
 * @param args
 *            command-line arguments (ignored)
 */
public static void main(String[] args) {
	Display display = new Display();
	Point displayDPI = display.getDPI();
	display.dispose();

	Printer printer = new Printer(new PrinterData());
	Point printerDPI = printer.getDPI();

	try {
		PrintJob job = new PrintJob("ImageCapture.java", createPrint());

		PrintPiece[] pages = PaperClips.getPages(job, printer);

		ImageLoader imageLoader = new ImageLoader();
		for (int i = 0; i < pages.length; i++) {
			PrintPiece page = pages[i];
			Point pageSize = page.getSize();
			pageSize.x = pageSize.x * displayDPI.x / printerDPI.x;
			pageSize.y = pageSize.y * displayDPI.y / printerDPI.y;
			ImageData pageImage = captureImageData(printer, page, pageSize);

			// Do something with the image
			pageImage.scanlinePad = 1;

			imageLoader.data = new ImageData[] { pageImage };
			imageLoader.save(getImageName(i), SWT.IMAGE_JPEG);
		}

	} finally {
		printer.dispose();
	}
}
 
Example 2
Source File: UIUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the DPI info of current display environment.
 * 
 * @return the DPI values in format of {hdpi, vdpi}.
 */
public static int[] getScreenResolution( )
{
	int[] dpi = {
			0, 0
	};

	Display display = Display.getCurrent( );
	if ( display == null )
	{
		display = Display.getDefault( );
	}
	if ( display.getThread( ).equals( Thread.currentThread( ) ) )
	{
		Point p = display.getDPI( );
		dpi[0] = p.x;
		dpi[1] = p.y;

		return dpi;
	}
	final Point[] points = new Point[]{
		new Point( 0, 0 )
	};
	final Display tempDisplay = display;
	display.syncExec( new Runnable( ) {

		public void run( )
		{
			points[0] = tempDisplay.getDPI( );
		}
	} );
	dpi[0] = points[0].x;
	dpi[1] = points[0].y;
	return dpi;
}
 
Example 3
Source File: BonitaRulerGridPropertySection.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 
 * converts fromUnits to toUnits (e.g. inches to pixels)
 * 
 * @param fromUnits
 * @param toUnits
 * @return equivalent number of toUnits for the given fromUnits
 */
private String convertUnits(int fromUnits, int toUnits) {
	String valueStr = textWidget.getText();
	if (fromUnits == toUnits) {
		return valueStr;
	}
	Double value = convertStringToDouble(valueStr);
	double pixelValue = 0;
	Display display = DisplayUtils.getDisplay();
	switch (fromUnits) {
	case INCHES:
		pixelValue = value.doubleValue() * display.getDPI().x;
		break;
	case CENTIMETERS:
		pixelValue = value.doubleValue() * display.getDPI().x / INCH2CM;
		break;
	case PIXELS:
		pixelValue = value.intValue();
	}

	double returnValue = 0;

	switch (toUnits) {
	case INCHES:
		returnValue = pixelValue / display.getDPI().x;
		break;
	case CENTIMETERS:
		returnValue = pixelValue * INCH2CM / display.getDPI().x;
		break;
	case PIXELS:
		returnValue = Math.round(pixelValue);
	}
	NumberFormat numberFormatter = NumberFormat.getInstance();
	return numberFormatter.format(returnValue);

}
 
Example 4
Source File: ShowDiagramGridField.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
private String convertUnits(int fromUnits, int toUnits ) {
	String valueStr = gridSpacing.getStringValue();
	if( fromUnits == toUnits ) {
		return valueStr;
	}
	
	//Double value = Double.valueOf( valueStr );
	NumberFormat numberFormatter = NumberFormat.getInstance();		
	Double value = new Double(0.125);
	try {
		value = forceDouble(numberFormatter.parse(valueStr));
	} catch (ParseException e) {
		// Use the default
	}
	double pixelValue = 0;
	
	Display display = Display.getDefault();

	switch( fromUnits ) {
		case INCHES:
			pixelValue = value.doubleValue() * display.getDPI().x;
			break;
		case CENTIMETERS:
			pixelValue = value.doubleValue() * display.getDPI().x / INCH2CM;
			break;
		case PIXELS:
			pixelValue = value.intValue();
	}
	
	double returnValue = 0;
	
	switch( toUnits ) {
		case INCHES:
			returnValue = pixelValue / display.getDPI().x;
			break;
		case CENTIMETERS:
			returnValue = pixelValue * INCH2CM / display.getDPI().x;
			break;
		case PIXELS:
			returnValue = pixelValue;
	}
	
	return numberFormatter.format(returnValue);		
}