Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

In this page you can find the example usage for java.lang Math max.

Prototype

@HotSpotIntrinsicCandidate
public static double max(double a, double b) 

Source Link

Document

Returns the greater of two double values.

Usage

From source file:org.eclipse.swt.snippets.Snippet51.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 51");
    shell.setBounds(10, 10, 300, 300);/* www .  j av  a2s. c o m*/
    shell.setLayout(new GridLayout(2, true));
    final Table table = new Table(shell, SWT.NONE);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 2;
    table.setLayoutData(data);
    for (int i = 0; i < 99; i++) {
        new TableItem(table, SWT.NONE).setText("item " + i);
    }
    Button upButton = new Button(shell, SWT.PUSH);
    upButton.setText("Scroll up one page");
    upButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    upButton.addListener(SWT.Selection, event -> {
        int height = table.getClientArea().height;
        int visibleItemCount = height / table.getItemHeight();
        int topIndex = table.getTopIndex();
        int newTopIndex = Math.max(0, topIndex - visibleItemCount);
        if (topIndex != newTopIndex) {
            table.setTopIndex(newTopIndex);
        }
    });
    Button downButton = new Button(shell, SWT.PUSH);
    downButton.setText("Scroll down one page");
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    downButton.addListener(SWT.Selection, event -> {
        int height = table.getClientArea().height;
        int visibleItemCount = height / table.getItemHeight();
        int topIndex = table.getTopIndex();
        int newTopIndex = Math.min(table.getItemCount(), topIndex + visibleItemCount);
        if (topIndex != newTopIndex) {
            table.setTopIndex(newTopIndex);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet214.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 214");
    shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    FillLayout layout1 = new FillLayout(SWT.VERTICAL);
    layout1.marginWidth = layout1.marginHeight = 10;
    shell.setLayout(layout1);//from w w w.  j av a 2 s  . c o m
    Group group = new Group(shell, SWT.NONE);
    group.setText("Group ");
    RowLayout layout2 = new RowLayout(SWT.VERTICAL);
    layout2.marginWidth = layout2.marginHeight = layout2.spacing = 10;
    group.setLayout(layout2);
    for (int i = 0; i < 8; i++) {
        Button button = new Button(group, SWT.RADIO);
        button.setText("Button " + i);
    }
    shell.addListener(SWT.Resize, event -> {
        Rectangle rect = shell.getClientArea();
        Image newImage = new Image(display, Math.max(1, rect.width), 1);
        GC gc = new GC(newImage);
        gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
        gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
        gc.dispose();
        shell.setBackgroundImage(newImage);
        if (oldImage != null)
            oldImage.dispose();
        oldImage = newImage;
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (oldImage != null)
        oldImage.dispose();
    display.dispose();
}

From source file:Snippet107.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Button 1");
    final Sash sash = new Sash(shell, SWT.VERTICAL);
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Button 2");

    final FormLayout form = new FormLayout();
    shell.setLayout(form);/*from   w  w w .j  a v  a  2 s .  c o  m*/

    FormData button1Data = new FormData();
    button1Data.left = new FormAttachment(0, 0);
    button1Data.right = new FormAttachment(sash, 0);
    button1Data.top = new FormAttachment(0, 0);
    button1Data.bottom = new FormAttachment(100, 0);
    button1.setLayoutData(button1Data);

    final int limit = 20, percent = 50;
    final FormData sashData = new FormData();
    sashData.left = new FormAttachment(percent, 0);
    sashData.top = new FormAttachment(0, 0);
    sashData.bottom = new FormAttachment(100, 0);
    sash.setLayoutData(sashData);
    sash.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            Rectangle sashRect = sash.getBounds();
            Rectangle shellRect = shell.getClientArea();
            int right = shellRect.width - sashRect.width - limit;
            e.x = Math.max(Math.min(e.x, right), limit);
            if (e.x != sashRect.x) {
                sashData.left = new FormAttachment(0, e.x);
                shell.layout();
            }
        }
    });

    FormData button2Data = new FormData();
    button2Data.left = new FormAttachment(sash, 0);
    button2Data.right = new FormAttachment(100, 0);
    button2Data.top = new FormAttachment(0, 0);
    button2Data.bottom = new FormAttachment(100, 0);
    button2.setLayoutData(button2Data);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet8.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Lazy Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        TreeItem root = new TreeItem(tree, 0);
        root.setText(roots[i].toString());
        root.setData(roots[i]);/*from ww w .ja  v a 2s. c om*/
        new TreeItem(root, 0);
    }
    tree.addListener(SWT.Expand, new Listener() {
        public void handleEvent(final Event event) {
            final TreeItem root = (TreeItem) event.item;
            TreeItem[] items = root.getItems();
            for (int i = 0; i < items.length; i++) {
                if (items[i].getData() != null)
                    return;
                items[i].dispose();
            }
            File file = (File) root.getData();
            File[] files = file.listFiles();
            if (files == null)
                return;
            for (int i = 0; i < files.length; i++) {
                TreeItem item = new TreeItem(root, 0);
                item.setText(files[i].getName());
                item.setData(files[i]);
                if (files[i].isDirectory()) {
                    new TreeItem(item, 0);
                }
            }
        }
    });
    Point size = tree.computeSize(300, SWT.DEFAULT);
    int width = Math.max(300, size.x);
    int height = Math.max(300, size.y);
    shell.setSize(shell.computeSize(width, height));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet167.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 167");
    shell.setLayout(new FillLayout());

    final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button1 = new Button(sc1, SWT.PUSH);
    button1.setText("Button 1");
    button1.setSize(400, 300);//  www . j a va2s . c  om
    sc1.setContent(button1);

    final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button2 = new Button(sc2, SWT.PUSH);
    button2.setText("Button 2");
    button2.setSize(300, 400);
    sc2.setContent(button2);

    final ScrollBar vBar1 = sc1.getVerticalBar();
    final ScrollBar vBar2 = sc2.getVerticalBar();
    final ScrollBar hBar1 = sc1.getHorizontalBar();
    final ScrollBar hBar2 = sc2.getHorizontalBar();
    SelectionListener listener1 = widgetSelectedAdapter(e -> {
        int x = hBar1.getSelection() * (hBar2.getMaximum() - hBar2.getThumb())
                / Math.max(1, hBar1.getMaximum() - hBar1.getThumb());
        int y = vBar1.getSelection() * (vBar2.getMaximum() - vBar2.getThumb())
                / Math.max(1, vBar1.getMaximum() - vBar1.getThumb());
        sc2.setOrigin(x, y);
    });
    SelectionListener listener2 = widgetSelectedAdapter(e -> {
        int x = hBar2.getSelection() * (hBar1.getMaximum() - hBar1.getThumb())
                / Math.max(1, hBar2.getMaximum() - hBar2.getThumb());
        int y = vBar2.getSelection() * (vBar1.getMaximum() - vBar1.getThumb())
                / Math.max(1, vBar2.getMaximum() - vBar2.getThumb());
        sc1.setOrigin(x, y);
    });
    vBar1.addSelectionListener(listener1);
    hBar1.addSelectionListener(listener1);
    vBar2.addSelectionListener(listener2);
    hBar2.addSelectionListener(listener2);

    shell.setSize(400, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:BackgroundImageControl.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    FillLayout layout1 = new FillLayout(SWT.VERTICAL);
    layout1.marginWidth = layout1.marginHeight = 10;
    shell.setLayout(layout1);//from w  ww.ja va2s  .  com
    Group group = new Group(shell, SWT.NONE);
    group.setText("Group ");
    RowLayout layout2 = new RowLayout(SWT.VERTICAL);
    layout2.marginWidth = layout2.marginHeight = layout2.spacing = 10;
    group.setLayout(layout2);
    for (int i = 0; i < 8; i++) {
        Button button = new Button(group, SWT.RADIO);
        button.setText("Button " + i);
    }
    shell.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle rect = shell.getClientArea();
            Image newImage = new Image(display, Math.max(1, rect.width), 1);
            GC gc = new GC(newImage);
            gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
            gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
            gc.dispose();
            shell.setBackgroundImage(newImage);
            if (oldImage != null)
                oldImage.dispose();
            oldImage = newImage;
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (oldImage != null)
        oldImage.dispose();
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet107.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 107");
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Button 1");
    final Sash sash = new Sash(shell, SWT.VERTICAL);
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Button 2");

    final FormLayout form = new FormLayout();
    shell.setLayout(form);//from  www.ja  va  2 s .co m

    FormData button1Data = new FormData();
    button1Data.left = new FormAttachment(0, 0);
    button1Data.right = new FormAttachment(sash, 0);
    button1Data.top = new FormAttachment(0, 0);
    button1Data.bottom = new FormAttachment(100, 0);
    button1.setLayoutData(button1Data);

    final int limit = 20, percent = 50;
    final FormData sashData = new FormData();
    sashData.left = new FormAttachment(percent, 0);
    sashData.top = new FormAttachment(0, 0);
    sashData.bottom = new FormAttachment(100, 0);
    sash.setLayoutData(sashData);
    sash.addListener(SWT.Selection, e -> {
        Rectangle sashRect = sash.getBounds();
        Rectangle shellRect = shell.getClientArea();
        int right = shellRect.width - sashRect.width - limit;
        e.x = Math.max(Math.min(e.x, right), limit);
        if (e.x != sashRect.x) {
            sashData.left = new FormAttachment(0, e.x);
            shell.layout();
        }
    });

    FormData button2Data = new FormData();
    button2Data.left = new FormAttachment(sash, 0);
    button2Data.right = new FormAttachment(100, 0);
    button2Data.top = new FormAttachment(0, 0);
    button2Data.bottom = new FormAttachment(100, 0);
    button2.setLayoutData(button2Data);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet8.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Lazy Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        TreeItem root = new TreeItem(tree, 0);
        root.setText(roots[i].toString());
        root.setData(roots[i]);//from w w  w  .  j a  v  a 2s .  co  m
        new TreeItem(root, 0);
    }
    tree.addListener(SWT.Expand, event -> {
        final TreeItem root = (TreeItem) event.item;
        TreeItem[] items = root.getItems();
        for (int i1 = 0; i1 < items.length; i1++) {
            if (items[i1].getData() != null)
                return;
            items[i1].dispose();
        }
        File file = (File) root.getData();
        File[] files = file.listFiles();
        if (files == null)
            return;
        for (int i2 = 0; i2 < files.length; i2++) {
            TreeItem item = new TreeItem(root, 0);
            item.setText(files[i2].getName());
            item.setData(files[i2]);
            if (files[i2].isDirectory()) {
                new TreeItem(item, 0);
            }
        }
    });
    Point size = tree.computeSize(300, SWT.DEFAULT);
    int width = Math.max(300, size.x);
    int height = Math.max(300, size.y);
    shell.setSize(shell.computeSize(width, height));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet218.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 218");
    shell.setLayout(new FillLayout());
    final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setText(text);//from  w w  w. j  a  v  a 2s .  c  o 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, 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:ScrollBarSelectionListener.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button1 = new Button(sc1, SWT.PUSH);
    button1.setText("Button 1");
    button1.setSize(400, 300);/*ww w .jav a 2  s.  c om*/
    sc1.setContent(button1);

    final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button button2 = new Button(sc2, SWT.PUSH);
    button2.setText("Button 2");
    button2.setSize(300, 400);
    sc2.setContent(button2);

    final ScrollBar vBar1 = sc1.getVerticalBar();
    final ScrollBar vBar2 = sc2.getVerticalBar();
    final ScrollBar hBar1 = sc1.getHorizontalBar();
    final ScrollBar hBar2 = sc2.getHorizontalBar();
    SelectionListener listener1 = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int x = hBar1.getSelection() * (hBar2.getMaximum() - hBar2.getThumb())
                    / Math.max(1, hBar1.getMaximum() - hBar1.getThumb());
            int y = vBar1.getSelection() * (vBar2.getMaximum() - vBar2.getThumb())
                    / Math.max(1, vBar1.getMaximum() - vBar1.getThumb());
            sc2.setOrigin(x, y);
        }
    };
    SelectionListener listener2 = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            int x = hBar2.getSelection() * (hBar1.getMaximum() - hBar1.getThumb())
                    / Math.max(1, hBar2.getMaximum() - hBar2.getThumb());
            int y = vBar2.getSelection() * (vBar1.getMaximum() - vBar1.getThumb())
                    / Math.max(1, vBar2.getMaximum() - vBar2.getThumb());
            sc1.setOrigin(x, y);
        }
    };
    vBar1.addSelectionListener(listener1);
    hBar1.addSelectionListener(listener1);
    vBar2.addSelectionListener(listener2);
    hBar2.addSelectionListener(listener2);

    shell.setSize(400, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}