List of usage examples for java.lang Math max
@HotSpotIntrinsicCandidate public static double max(double a, double b)
From source file:org.eclipse.swt.snippets.Snippet230.java
public static void main(String[] args) { Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); Shell shell = new Shell(display); shell.setText("Images on the right side of the TableItem"); shell.setLayout(new FillLayout()); Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION); table.setHeaderVisible(true);/*w ww . java 2 s. co m*/ table.setLinesVisible(true); int columnCount = 3; for (int i = 0; i < columnCount; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("Column " + i); } int itemCount = 8; for (int i = 0; i < itemCount; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "item " + i + " a", "item " + i + " b", "item " + i + " c" }); } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be * as efficient as possible. */ Listener paintListener = event -> { switch (event.type) { case SWT.MeasureItem: { Rectangle rect1 = image.getBounds(); event.width += rect1.width; event.height = Math.max(event.height, rect1.height + 2); break; } case SWT.PaintItem: { int x = event.x + event.width; Rectangle rect2 = image.getBounds(); int offset = Math.max(0, (event.height - rect2.height) / 2); event.gc.drawImage(image, x, event.y + offset); break; } } }; table.addListener(SWT.MeasureItem, paintListener); table.addListener(SWT.PaintItem, paintListener); for (int i = 0; i < columnCount; i++) { table.getColumn(i).pack(); } shell.setSize(500, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (image != null) image.dispose(); display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet231.java
public static void main(String[] args) { final int COLUMN_COUNT = 4; final int ITEM_COUNT = 8; final int TEXT_MARGIN = 3; Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 231"); final Table table = new Table(shell, SWT.FULL_SELECTION); table.setHeaderVisible(true);//from w w w. j a v a2 s . c o m table.setLinesVisible(true); for (int i = 0; i < COLUMN_COUNT; i++) { new TableColumn(table, SWT.NONE); } for (int i = 0; i < ITEM_COUNT; i++) { TableItem item = new TableItem(table, SWT.NONE); for (int j = 0; j < COLUMN_COUNT; j++) { String string = "item " + i + " col " + j; if ((i + j) % 3 == 1) { string += "\nnew line1"; } if ((i + j) % 3 == 2) { string += "\nnew line1\nnew line2"; } item.setText(j, string); } } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be * as efficient as possible. */ table.addListener(SWT.MeasureItem, event -> { TableItem item = (TableItem) event.item; String text = item.getText(event.index); Point size = event.gc.textExtent(text); event.width = size.x + 2 * TEXT_MARGIN; event.height = Math.max(event.height, size.y + TEXT_MARGIN); }); table.addListener(SWT.EraseItem, event -> event.detail &= ~SWT.FOREGROUND); table.addListener(SWT.PaintItem, event -> { TableItem item = (TableItem) event.item; String text = item.getText(event.index); /* center column 1 vertically */ int yOffset = 0; if (event.index == 1) { Point size = event.gc.textExtent(text); yOffset = Math.max(0, (event.height - size.y) / 2); } event.gc.drawText(text, event.x + TEXT_MARGIN, event.y + yOffset, true); }); for (int i = 0; i < COLUMN_COUNT; i++) { table.getColumn(i).pack(); } table.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet239.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Text spans two columns in a TableItem"); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION); table.setHeaderVisible(true);/*w ww. j a v a2 s.c om*/ int columnCount = 4; for (int i = 0; i < columnCount; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("Column " + i); } int itemCount = 8; for (int i = 0; i < itemCount; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(0, "item " + i + " a"); item.setText(3, "item " + i + " d"); } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be * as efficient as possible. */ final String string = "text that spans two columns"; GC gc = new GC(table); final Point extent = gc.stringExtent(string); gc.dispose(); final Color red = display.getSystemColor(SWT.COLOR_RED); Listener paintListener = event -> { switch (event.type) { case SWT.MeasureItem: { if (event.index == 1 || event.index == 2) { event.width = extent.x / 2; event.height = Math.max(event.height, extent.y + 2); } break; } case SWT.PaintItem: { if (event.index == 1 || event.index == 2) { int offset = 0; if (event.index == 2) { TableColumn column1 = table.getColumn(1); offset = column1.getWidth(); } event.gc.setForeground(red); int y = event.y + (event.height - extent.y) / 2; event.gc.drawString(string, event.x - offset, y, true); } break; } } }; table.addListener(SWT.MeasureItem, paintListener); table.addListener(SWT.PaintItem, paintListener); for (int i = 0; i < columnCount; i++) { table.getColumn(i).pack(); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:TableCellEnlarge.java
public static void main(String[] args) { Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); Shell shell = new Shell(display); shell.setText("Images on the right side of the TableItem"); shell.setLayout(new FillLayout()); Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION); table.setHeaderVisible(true);//from w w w . ja v a2s . c o m table.setLinesVisible(true); int columnCount = 3; for (int i = 0; i < columnCount; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("Column " + i); } int itemCount = 8; for (int i = 0; i < itemCount; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "item " + i + " a", "item " + i + " b", "item " + i + " c" }); } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be as * efficient as possible. */ Listener paintListener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.MeasureItem: { Rectangle rect = image.getBounds(); event.width += rect.width; event.height = Math.max(event.height, rect.height + 2); break; } case SWT.PaintItem: { int x = event.x + event.width; Rectangle rect = image.getBounds(); int offset = Math.max(0, (event.height - rect.height) / 2); event.gc.drawImage(image, x, event.y + offset); break; } } } }; table.addListener(SWT.MeasureItem, paintListener); table.addListener(SWT.PaintItem, paintListener); for (int i = 0; i < columnCount; i++) { table.getColumn(i).pack(); } shell.setSize(500, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (image != null) image.dispose(); display.dispose(); }
From source file:StyledTextGradientBackground.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); styledText.setText(text);//from ww w .jav a 2 s.co m FontData data = display.getSystemFont().getFontData()[0]; Font font = new Font(display, data.getName(), 16, SWT.BOLD); styledText.setFont(font); styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); styledText.addListener(SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle rect = styledText.getClientArea(); Image newImage = new Image(display, 1, Math.max(1, rect.height)); GC gc = new GC(newImage); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true); gc.dispose(); styledText.setBackgroundImage(newImage); if (oldImage != null) oldImage.dispose(); oldImage = newImage; } }); shell.setSize(700, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (oldImage != null) oldImage.dispose(); font.dispose(); display.dispose(); }
From source file:ScrollWidgetViewFocusIn.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Button b1 = new Button(shell, SWT.PUSH); b1.setText("top"); b1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); Composite c = new Composite(sc, SWT.NONE); c.setLayout(new GridLayout(10, true)); for (int i = 0; i < 300; i++) { Button b = new Button(c, SWT.PUSH); b.setText("Button " + i); }/*from w w w .j a v a 2s . c o m*/ Button b2 = new Button(shell, SWT.PUSH); b2.setText("bottom"); b2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); sc.setContent(c); sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT)); Listener listener = new Listener() { public void handleEvent(Event e) { Control child = (Control) e.widget; Rectangle bounds = child.getBounds(); Rectangle area = sc.getClientArea(); Point origin = sc.getOrigin(); if (origin.x > bounds.x) origin.x = Math.max(0, bounds.x); if (origin.y > bounds.y) origin.y = Math.max(0, bounds.y); if (origin.x + area.width < bounds.x + bounds.width) origin.x = Math.max(0, bounds.x + bounds.width - area.width); if (origin.y + area.height < bounds.y + bounds.height) origin.y = Math.max(0, bounds.y + bounds.height - area.height); sc.setOrigin(origin); } }; Control[] controls = c.getChildren(); for (int i = 0; i < controls.length; i++) { controls[i].addListener(SWT.Activate, listener); } shell.setSize(300, 500); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:TableEventMeasureItemPaintItemEraseItem.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Multiple lines in a TableItem"); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION); table.setHeaderVisible(true);/* ww w .j a v a 2 s .co m*/ table.setLinesVisible(true); int columnCount = 4; for (int i = 0; i < columnCount; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("Column " + i); } int itemCount = 8; for (int i = 0; i < itemCount; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "item " + i + " a", "item " + i + " b", "item " + i + " c", "item " + i + " d" }); } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be as * efficient as possible. */ Listener paintListener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.MeasureItem: { TableItem item = (TableItem) event.item; String text = getText(item, event.index); Point size = event.gc.textExtent(text); event.width = size.x; event.height = Math.max(event.height, size.y); break; } case SWT.PaintItem: { TableItem item = (TableItem) event.item; String text = getText(item, event.index); Point size = event.gc.textExtent(text); int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0; event.gc.drawText(text, event.x, event.y + offset2, true); break; } case SWT.EraseItem: { event.detail &= ~SWT.FOREGROUND; break; } } } String getText(TableItem item, int column) { String text = item.getText(column); if (column != 0) { int index = table.indexOf(item); if ((index + column) % 3 == 1) { text += "\nnew line"; } if ((index + column) % 3 == 2) { text += "\nnew line\nnew line"; } } return text; } }; table.addListener(SWT.MeasureItem, paintListener); table.addListener(SWT.PaintItem, paintListener); table.addListener(SWT.EraseItem, paintListener); for (int i = 0; i < columnCount; i++) { table.getColumn(i).pack(); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:TextSpanMultipleColumns.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Text spans two columns in a TableItem"); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION); table.setHeaderVisible(true);// w ww. ja v a 2 s. c om int columnCount = 4; for (int i = 0; i < columnCount; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText("Column " + i); } int itemCount = 8; for (int i = 0; i < itemCount; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(0, "item " + i + " a"); item.setText(3, "item " + i + " d"); } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be as * efficient as possible. */ final String string = "text that spans two columns"; GC gc = new GC(table); final Point extent = gc.stringExtent(string); gc.dispose(); final Color red = display.getSystemColor(SWT.COLOR_RED); Listener paintListener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.MeasureItem: { if (event.index == 1 || event.index == 2) { event.width = extent.x / 2; event.height = Math.max(event.height, extent.y + 2); } break; } case SWT.PaintItem: { if (event.index == 1 || event.index == 2) { int offset = 0; if (event.index == 2) { TableColumn column1 = table.getColumn(1); offset = column1.getWidth(); } event.gc.setForeground(red); int y = event.y + (event.height - extent.y) / 2; event.gc.drawString(string, event.x - offset, y); } break; } } } }; table.addListener(SWT.MeasureItem, paintListener); table.addListener(SWT.PaintItem, paintListener); for (int i = 0; i < columnCount; i++) { table.getColumn(i).pack(); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet240.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Text spans two columns in a TreeItem"); shell.setLayout(new FillLayout()); final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION); tree.setHeaderVisible(true);// ww w. j a va2 s . com int columnCount = 4; for (int i = 0; i < columnCount; i++) { TreeColumn column = new TreeColumn(tree, SWT.NONE); column.setText("Column " + i); } int itemCount = 8; for (int i = 0; i < itemCount; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText(0, "item " + i + " a"); item.setText(3, "item " + i + " d"); for (int j = 0; j < 3; j++) { TreeItem subItem = new TreeItem(item, SWT.NONE); subItem.setText(0, "subItem " + i + "-" + j + " a"); subItem.setText(3, "subItem " + i + "-" + j + " d"); } } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be * as efficient as possible. */ final String string = "text that spans two columns"; GC gc = new GC(tree); final Point extent = gc.stringExtent(string); gc.dispose(); final Color red = display.getSystemColor(SWT.COLOR_RED); Listener paintListener = event -> { switch (event.type) { case SWT.MeasureItem: { if (event.index == 1 || event.index == 2) { event.width = extent.x / 2; event.height = Math.max(event.height, extent.y + 2); } break; } case SWT.PaintItem: { if (event.index == 1 || event.index == 2) { int offset = 0; if (event.index == 2) { TreeColumn column1 = tree.getColumn(1); offset = column1.getWidth(); } event.gc.setForeground(red); int y = event.y + (event.height - extent.y) / 2; event.gc.drawString(string, event.x - offset, y, true); } break; } } }; tree.addListener(SWT.MeasureItem, paintListener); tree.addListener(SWT.PaintItem, paintListener); for (int i = 0; i < columnCount; i++) { tree.getColumn(i).pack(); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:edu.usc.squash.Main.java
public static void main(String[] args) { Stack<Module> modulesStack; Module module;//from w w w . j ava2s.c o m if (parseInputs(args) == false) { System.exit(-1); //The input files do not exist } String separator = "----------------------------------------------"; System.out.println("Squash v2.0"); System.out.println(separator); long start = System.currentTimeMillis(); // Parsing the input library Library library = QLib.readLib(libraryPath); library.setCurrentECC(currentECC); HashMap<String, Module> modules = parseQASMHF(library); Module mainModule = modules.get("main"); //Finding max{A_L_i} int childModulesLogicalAncillaReq; int moduleAncillaReq; modulesStack = new Stack<Module>(); modulesStack.add(mainModule); while (!modulesStack.isEmpty()) { module = modulesStack.peek(); if (!module.isVisited() && module.isChildrenVisited()) { //Finding the maximum childModulesLogicalAncillaReq = 0; for (Module child : module.getDFG().getModules()) { childModulesLogicalAncillaReq = Math.max(childModulesLogicalAncillaReq, child.getAncillaReq()); } moduleAncillaReq = module.getAncillaQubitNo() + childModulesLogicalAncillaReq; module.setAncillaReq(moduleAncillaReq); // System.out.println("Module "+module.getName()+" requires "+moduleAncillaReq+" ancilla."); modulesStack.pop(); module.setVisited(); } else if (module.isVisited()) { modulesStack.pop(); } else if (!module.isChildrenVisited()) { modulesStack.addAll(module.getDFG().getModules()); module.setChildrenVisited(); } } int totalLogicalAncilla = mainModule.getAncillaReq(); System.out.println("A_L_i_max: " + totalLogicalAncilla); final int Q_L = mainModule.getDataQubitNo(); /* * In order traversal of modules */ //Making sure all of the modules are unvisited for (Module m : modules.values()) { m.setUnvisited(); } modulesStack = new Stack<Module>(); modulesStack.add(mainModule); while (!modulesStack.isEmpty()) { module = modulesStack.peek(); if (!module.isVisited() && module.isChildrenVisited()) { System.out.println(separator); mapModule(module, k, physicalAncillaBudget, totalLogicalAncilla, Q_L, beta_pmd, alpha_int, gamma_memory, library); modulesStack.pop(); module.setVisited(); } else if (module.isVisited()) { modulesStack.pop(); } else if (!module.isChildrenVisited()) { modulesStack.addAll(module.getDFG().getModules()); module.setChildrenVisited(); } } System.out.println(separator); double runtime = (System.currentTimeMillis() - start) / 1000.0; System.out.println("B_P: " + B_P); System.out.println("Total Runtime:\t" + runtime + " sec"); }