List of usage examples for org.eclipse.swt.graphics FontData getName
public String getName()
From source file:TextLayoutSubscriptSuperscript.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED); shell.setText("Modify Rise"); FontData data = display.getSystemFont().getFontData()[0]; Font font = new Font(display, data.getName(), 24, SWT.NORMAL); Font smallFont = new Font(display, data.getName(), 8, SWT.NORMAL); GC gc = new GC(shell); gc.setFont(smallFont);//from w w w . j a v a 2 s . c o m FontMetrics smallMetrics = gc.getFontMetrics(); final int smallBaseline = smallMetrics.getAscent() + smallMetrics.getLeading(); gc.setFont(font); FontMetrics metrics = gc.getFontMetrics(); final int baseline = metrics.getAscent() + metrics.getLeading(); gc.dispose(); final TextLayout layout0 = new TextLayout(display); layout0.setText("SubscriptScriptSuperscript"); layout0.setFont(font); TextStyle subscript0 = new TextStyle(smallFont, null, null); TextStyle superscript0 = new TextStyle(smallFont, null, null); superscript0.rise = baseline - smallBaseline; layout0.setStyle(subscript0, 0, 8); layout0.setStyle(superscript0, 15, 25); final TextLayout layout1 = new TextLayout(display); layout1.setText("SubscriptScriptSuperscript"); layout1.setFont(font); TextStyle subscript1 = new TextStyle(smallFont, null, null); subscript1.rise = -smallBaseline; TextStyle superscript1 = new TextStyle(smallFont, null, null); superscript1.rise = baseline; layout1.setStyle(subscript1, 0, 8); layout1.setStyle(superscript1, 15, 25); shell.addListener(SWT.Paint, new Listener() { public void handleEvent(Event event) { Display display = event.display; GC gc = event.gc; Rectangle rect0 = layout0.getBounds(); rect0.x += 10; rect0.y += 10; gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); gc.fillRectangle(rect0); layout0.draw(gc, rect0.x, rect0.y); gc.setForeground(display.getSystemColor(SWT.COLOR_MAGENTA)); gc.drawLine(rect0.x, rect0.y, rect0.x + rect0.width, rect0.y); gc.drawLine(rect0.x, rect0.y + baseline, rect0.x + rect0.width, rect0.y + baseline); gc.drawLine(rect0.x + rect0.width / 2, rect0.y, rect0.x + rect0.width / 2, rect0.y + rect0.height); Rectangle rect1 = layout1.getBounds(); rect1.x += 10; rect1.y += 20 + rect0.height; gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); gc.fillRectangle(rect1); layout1.draw(gc, rect1.x, rect1.y); gc.setForeground(display.getSystemColor(SWT.COLOR_MAGENTA)); gc.drawLine(rect1.x, rect1.y + smallBaseline, rect1.x + rect1.width, rect1.y + smallBaseline); gc.drawLine(rect1.x, rect1.y + baseline + smallBaseline, rect1.x + rect1.width, rect1.y + baseline + smallBaseline); gc.drawLine(rect1.x + rect1.width / 2, rect1.y, rect1.x + rect1.width / 2, rect1.y + rect1.height); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } layout0.dispose(); layout1.dispose(); smallFont.dispose(); font.dispose(); display.dispose(); }
From source file:SWTUtils.java
/** * Create an awt font by converting as much information * as possible from the provided swt <code>FontData</code>. * <p>Generally speaking, given a font size, an swt font will * display differently on the screen than the corresponding awt * one. Because the SWT toolkit use native graphical ressources whenever * it is possible, this fact is platform dependent. To address * this issue, it is possible to enforce the method to return * an awt font with the same height as the swt one. * * @param device The swt device being drawn on (display or gc device). * @param fontData The swt font to convert. * @param ensureSameSize A boolean used to enforce the same size * (in pixels) between the swt font and the newly created awt font. * @return An awt font converted from the provided swt font. *///from w w w. j av a 2 s .c o m public static java.awt.Font toAwtFont(Device device, FontData fontData, boolean ensureSameSize) { int height = (int) Math.round(fontData.getHeight() * device.getDPI().y / 72.0); // hack to ensure the newly created awt fonts will be rendered with the // same height as the swt one if (ensureSameSize) { GC tmpGC = new GC(device); Font tmpFont = new Font(device, fontData); tmpGC.setFont(tmpFont); JPanel DUMMY_PANEL = new JPanel(); java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height); if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { height--; tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height); } } else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { height++; tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height); } } tmpFont.dispose(); tmpGC.dispose(); } return new java.awt.Font(fontData.getName(), fontData.getStyle(), height); }
From source file:org.eclipse.swt.examples.graphics.AdvancedGraphics.java
public Shell open(final Display display) { final Shell shell = new Shell(display); shell.setText(RESOURCE_BUNDLE.getString("AdvancedGraphics")); //$NON-NLS-1$ try {/*from www. ja v a2 s . c o m*/ Path path = new Path(display); path.dispose(); } catch (SWTException e) { MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK); dialog.setText(RESOURCE_BUNDLE.getString("Warning")); //$NON-NLS-1$ dialog.setMessage(RESOURCE_BUNDLE.getString("LibNotFound")); //$NON-NLS-1$ dialog.open(); shell.dispose(); return null; } FontData fd = shell.getFont().getFontData()[0]; final Font font = new Font(display, fd.getName(), 96, SWT.BOLD | SWT.ITALIC); final Image image = loadImage(display, AdvancedGraphics.class, "irmaos.jpg"); final Rectangle rect = image.getBounds(); shell.addListener(SWT.Paint, event -> { GC gc = event.gc; Transform tr = new Transform(display); tr.translate(rect.width / 4, rect.height / 2); tr.rotate(-30); if (image != null) { gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, rect.width, rect.height); } gc.setAlpha(100); gc.setTransform(tr); Path path = new Path(display); path.addString("SWT", 0, 0, font); gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); gc.fillPath(path); gc.drawPath(path); tr.dispose(); path.dispose(); }); shell.setSize(shell.computeSize(rect.width, rect.height)); shell.open(); shell.addListener(SWT.Dispose, event -> { if (image != null) image.dispose(); font.dispose(); }); return shell; }
From source file:org.eclipse.swt.examples.graphics.IntroTab.java
@Override public void paint(GC gc, int width, int height) { if (!example.checkAdvancedGraphics()) return;//from w w w . ja v a 2 s. com Device device = gc.getDevice(); if (image == null) { image = example.loadImage(device, "irmaos.jpg"); Rectangle rect = image.getBounds(); FontData fd = device.getSystemFont().getFontData()[0]; font = new Font(device, fd.getName(), rect.height / 4, SWT.BOLD); gc.setFont(font); Point size = gc.stringExtent(text); textWidth = size.x; textHeight = size.y; } Path path = new Path(device); path.addString(text, x, y, font); gc.setClipping(path); Rectangle rect = image.getBounds(); gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, width, height); gc.setClipping((Rectangle) null); gc.setForeground(device.getSystemColor(SWT.COLOR_BLUE)); gc.drawPath(path); path.dispose(); }
From source file:org.eclipse.swt.examples.graphics.CustomFontTab.java
public CustomFontTab(GraphicsExample example) { super(example); // create list of fonts for this platform FontData[] fontData = Display.getCurrent().getFontList(null, true); fontNames = new ArrayList<>(); for (FontData element : fontData) { // remove duplicates and sort String nextName = element.getName(); if (!fontNames.contains(nextName)) { int j = 0; while (j < fontNames.size() && nextName.compareTo(fontNames.get(j)) > 0) { j++;// ww w. jav a2 s . c om } fontNames.add(j, nextName); } } fontStyles = new String[] { GraphicsExample.getResourceString("Regular"), //$NON-NLS-1$ GraphicsExample.getResourceString("Italic"), //$NON-NLS-1$ GraphicsExample.getResourceString("Bold"), //$NON-NLS-1$ GraphicsExample.getResourceString("BoldItalic") //$NON-NLS-1$ }; styleValues = new int[] { SWT.NORMAL, SWT.ITALIC, SWT.BOLD, SWT.BOLD | SWT.ITALIC }; }
From source file:PrintingExample.java
void print(Printer printer) { if (printer.startJob("Text")) { // the string is the job name - shows up in the printer's job list Rectangle clientArea = printer.getClientArea(); Rectangle trim = printer.computeTrim(0, 0, 0, 0); Point dpi = printer.getDPI(); leftMargin = dpi.x + trim.x; // one inch from left side of paper rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch from right side of paper topMargin = dpi.y + trim.y; // one inch from top edge of paper bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch from bottom edge of paper /* Create a buffer for computing tab width. */ int tabSize = 4; // is tab width a user setting in your UI? StringBuffer tabBuffer = new StringBuffer(tabSize); for (int i = 0; i < tabSize; i++) tabBuffer.append(' '); tabs = tabBuffer.toString();/*from w ww. ja v a 2s . c o m*/ /* Create printer GC, and create and set the printer font & foreground color. */ gc = new GC(printer); FontData fontData = font.getFontData()[0]; printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle()); gc.setFont(printerFont); tabWidth = gc.stringExtent(tabs).x; lineHeight = gc.getFontMetrics().getHeight(); RGB rgb = foregroundColor.getRGB(); printerForegroundColor = new Color(printer, rgb); gc.setForeground(printerForegroundColor); rgb = backgroundColor.getRGB(); printerBackgroundColor = new Color(printer, rgb); gc.setBackground(printerBackgroundColor); /* Print text to current gc using word wrap */ printText(); printer.endJob(); /* Cleanup graphics resources used in printing */ printerFont.dispose(); printerForegroundColor.dispose(); printerBackgroundColor.dispose(); gc.dispose(); } }
From source file:Snippet133.java
void print(Printer printer) { if (printer.startJob("Text")) { // the string is the job name - shows up // in the printer's job list Rectangle clientArea = printer.getClientArea(); Rectangle trim = printer.computeTrim(0, 0, 0, 0); Point dpi = printer.getDPI(); leftMargin = dpi.x + trim.x; // one inch from left side of paper rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one // inch//from w w w .j a v a 2 s . c o m // from // right // side // of // paper topMargin = dpi.y + trim.y; // one inch from top edge of paper bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one // inch // from // bottom // edge // of // paper /* Create a buffer for computing tab width. */ int tabSize = 4; // is tab width a user setting in your UI? StringBuffer tabBuffer = new StringBuffer(tabSize); for (int i = 0; i < tabSize; i++) tabBuffer.append(' '); tabs = tabBuffer.toString(); /* * Create printer GC, and create and set the printer font & * foreground color. */ gc = new GC(printer); FontData fontData = font.getFontData()[0]; printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle()); gc.setFont(printerFont); tabWidth = gc.stringExtent(tabs).x; lineHeight = gc.getFontMetrics().getHeight(); RGB rgb = foregroundColor.getRGB(); printerForegroundColor = new Color(printer, rgb); gc.setForeground(printerForegroundColor); rgb = backgroundColor.getRGB(); printerBackgroundColor = new Color(printer, rgb); gc.setBackground(printerBackgroundColor); /* Print text to current gc using word wrap */ printText(); printer.endJob(); /* Cleanup graphics resources used in printing */ printerFont.dispose(); printerForegroundColor.dispose(); printerBackgroundColor.dispose(); gc.dispose(); } }
From source file:org.gumtree.vis.core.internal.SWTChartComposite.java
protected void print(Printer printer) { org.eclipse.swt.graphics.Rectangle clientArea = printer.getClientArea(); org.eclipse.swt.graphics.Rectangle trim = printer.computeTrim(0, 0, 0, 0); Point dpi = printer.getDPI(); int leftMargin = dpi.x + trim.x; // one inch from left side of paper int rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch from right side of paper int topMargin = dpi.y + trim.y; // one inch from top edge of paper int bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch from bottom edge of paper // printer.startJob("Plot"); GC gc = new GC(printer); Font font = new Font(getDisplay(), "Courier", 10, SWT.NORMAL); FontData fontData = font.getFontData()[0]; Font printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle()); gc.setFont(printerFont);//from w ww . j a va2 s . c o m // printer.startPage(); try { paint(gc, leftMargin, topMargin, rightMargin, bottomMargin); } catch (Exception e) { e.printStackTrace(); MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.ICON_ERROR); messageBox.setMessage(e.getMessage()); messageBox.open(); } finally { gc.dispose(); font.dispose(); printerFont.dispose(); System.out.println("printer job finished"); printer.endPage(); printer.endJob(); printer.dispose(); } }
From source file:org.locationtech.udig.processingtoolbox.tools.BoxPlotDialog.java
private void updateChart(SimpleFeatureCollection features, String[] fields) { // Setup Box plot int fontStyle = java.awt.Font.BOLD; FontData fontData = getShell().getDisplay().getSystemFont().getFontData()[0]; CategoryAxis xPlotAxis = new CategoryAxis(EMPTY); // Type xPlotAxis.setLabelFont(new Font(fontData.getName(), fontStyle, 12)); xPlotAxis.setTickLabelFont(new Font(fontData.getName(), fontStyle, 12)); NumberAxis yPlotAxis = new NumberAxis("Value"); // Value //$NON-NLS-1$ yPlotAxis.setLabelFont(new Font(fontData.getName(), fontStyle, 12)); yPlotAxis.setTickLabelFont(new Font(fontData.getName(), fontStyle, 10)); yPlotAxis.setAutoRangeIncludesZero(false); BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer(); renderer.setMedianVisible(true);//from ww w .j a v a 2 s .com renderer.setMeanVisible(false); renderer.setFillBox(true); renderer.setSeriesFillPaint(0, java.awt.Color.CYAN); renderer.setBaseFillPaint(java.awt.Color.CYAN); renderer.setBaseToolTipGenerator(new BoxAndWhiskerToolTipGenerator()); // Set the scatter data, renderer, and axis into plot CategoryDataset dataset = getDataset(features, fields); CategoryPlot plot = new CategoryPlot(dataset, xPlotAxis, yPlotAxis, renderer); plot.setOrientation(PlotOrientation.VERTICAL); plot.setBackgroundPaint(java.awt.Color.WHITE); plot.setRangePannable(false); plot.setDomainCrosshairVisible(false); plot.setRangeCrosshairVisible(false); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setForegroundAlpha(0.85f); // Map the scatter to the first Domain and first Range plot.mapDatasetToDomainAxis(0, 0); plot.mapDatasetToRangeAxis(0, 0); // 3. Setup Selection /***************************************************************************************** * CategoryAxis xSelectionAxis = new CategoryAxis(EMPTY); * xSelectionAxis.setTickMarksVisible(false); xSelectionAxis.setTickLabelsVisible(false); * * NumberAxis ySelectionAxis = new NumberAxis(EMPTY); * ySelectionAxis.setTickMarksVisible(false); ySelectionAxis.setTickLabelsVisible(false); * * BoxAndWhiskerRenderer selectionRenderer = new BoxAndWhiskerRenderer(); * selectionRenderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 6, 6)); * selectionRenderer.setSeriesPaint(0, java.awt.Color.RED); // dot * * plot.setDataset(2, new DefaultBoxAndWhiskerCategoryDataset()); plot.setRenderer(2, * selectionRenderer); plot.setDomainAxis(2, xSelectionAxis); plot.setRangeAxis(2, * ySelectionAxis); * * // Map the scatter to the second Domain and second Range plot.mapDatasetToDomainAxis(2, * 0); plot.mapDatasetToRangeAxis(2, 0); *****************************************************************************************/ // 5. Finally, Create the chart with the plot and a legend java.awt.Font titleFont = new Font(fontData.getName(), fontStyle, 20); JFreeChart chart = new JFreeChart(EMPTY, titleFont, plot, false); chart.setBackgroundPaint(java.awt.Color.WHITE); chart.setBorderVisible(false); chartComposite.setChart(chart); chartComposite.forceRedraw(); }
From source file:org.locationtech.udig.processingtoolbox.tools.HistogramDialog.java
private void updateChart(SimpleFeatureCollection features, String field) { int bin = spinner.getSelection(); double[] values = getValues(features, field); HistogramDataset dataset = new HistogramDataset(); dataset.addSeries(field, values, bin, minMaxVisitor.getMinX(), minMaxVisitor.getMaxX()); dataset.setType(histogramType);//w w w . ja v a 2 s. co m JFreeChart chart = ChartFactory.createHistogram(EMPTY, null, null, dataset, PlotOrientation.VERTICAL, false, false, false); // 1. Create a single plot containing both the scatter and line chart.setBackgroundPaint(java.awt.Color.WHITE); chart.setBorderVisible(false); XYPlot plot = (XYPlot) chart.getPlot(); plot.setForegroundAlpha(0.85F); plot.setBackgroundPaint(java.awt.Color.WHITE); plot.setOrientation(PlotOrientation.VERTICAL); plot.setDomainGridlinePaint(java.awt.Color.LIGHT_GRAY); plot.setRangeGridlinePaint(java.awt.Color.LIGHT_GRAY); int fontStyle = java.awt.Font.BOLD; FontData fontData = getShell().getDisplay().getSystemFont().getFontData()[0]; NumberAxis valueAxis = new NumberAxis(cboField.getText()); valueAxis.setLabelFont(new Font(fontData.getName(), fontStyle, 12)); valueAxis.setTickLabelFont(new Font(fontData.getName(), fontStyle, 10)); valueAxis.setAutoRange(false); valueAxis.setRange(minMaxVisitor.getMinX(), minMaxVisitor.getMaxX()); String rangeAxisLabel = histogramType == HistogramType.FREQUENCY ? "Frequency" : "Ratio"; //$NON-NLS-1$ //$NON-NLS-2$ NumberAxis rangeAxis = new NumberAxis(rangeAxisLabel); rangeAxis.setLabelFont(new Font(fontData.getName(), fontStyle, 12)); rangeAxis.setTickLabelFont(new Font(fontData.getName(), fontStyle, 10)); if (histogramType == HistogramType.FREQUENCY) { rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer(); renderer.setShadowVisible(false); CustomXYBarPainter.selectedColumn = -1; // init renderer.setBarPainter(new CustomXYBarPainter()); renderer.setAutoPopulateSeriesFillPaint(true); renderer.setAutoPopulateSeriesPaint(true); renderer.setShadowXOffset(3); renderer.setMargin(0.01); renderer.setBaseItemLabelsVisible(true); ItemLabelPosition pos = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.TOP_CENTER); renderer.setBasePositiveItemLabelPosition(pos); XYToolTipGenerator plotToolTip = new StandardXYToolTipGenerator(); renderer.setBaseToolTipGenerator(plotToolTip); // color GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, java.awt.Color.GRAY, 0.0f, 0.0f, java.awt.Color.LIGHT_GRAY); renderer.setSeriesPaint(0, gp0); plot.setDomainAxis(0, valueAxis); plot.setRangeAxis(0, rangeAxis); // 3. Setup line // Create the line data, renderer, and axis XYItemRenderer lineRenderer = new XYLineAndShapeRenderer(true, false); // Lines only lineRenderer.setSeriesPaint(0, java.awt.Color.RED); lineRenderer.setSeriesStroke(0, new BasicStroke(2f)); // Set the line data, renderer, and axis into plot NumberAxis xLineAxis = new NumberAxis(EMPTY); xLineAxis.setTickMarksVisible(false); xLineAxis.setTickLabelsVisible(false); xLineAxis.setAutoRange(false); NumberAxis yLineAxis = new NumberAxis(EMPTY); yLineAxis.setTickMarksVisible(false); yLineAxis.setTickLabelsVisible(false); yLineAxis.setAutoRange(false); double maxYValue = Double.MIN_VALUE; for (int i = 0; i < dataset.getItemCount(0); i++) { maxYValue = Math.max(maxYValue, dataset.getYValue(0, i)); } XYSeriesCollection lineDatset = new XYSeriesCollection(); // Vertical Average XYSeries vertical = new XYSeries("Average"); //$NON-NLS-1$ vertical.add(minMaxVisitor.getAverageX(), 0); vertical.add(minMaxVisitor.getAverageX(), maxYValue); lineDatset.addSeries(vertical); plot.setDataset(1, lineDatset); plot.setRenderer(1, lineRenderer); plot.setDomainAxis(1, xLineAxis); plot.setRangeAxis(1, yLineAxis); // Map the line to the second Domain and second Range plot.mapDatasetToDomainAxis(1, 0); plot.mapDatasetToRangeAxis(1, 0); chartComposite.setChart(chart); chartComposite.forceRedraw(); }