Example usage for com.google.gwt.user.client Random nextInt

List of usage examples for com.google.gwt.user.client Random nextInt

Introduction

In this page you can find the example usage for com.google.gwt.user.client Random nextInt.

Prototype

public static native int nextInt(int upperBound) ;

Source Link

Document

Returns a random int between 0 (inclusive) and upperBound (exclusive) with roughly equal probability of returning any particular int in this range.

Usage

From source file:client.Tetromino.java

License:Apache License

public static void newTetro() {
    x = Tilemap.getWidth() / 2 - 2;/*from   w w w .ja v  a 2  s.com*/
    y = 0;
    z = 0;
    type = Random.nextInt(7);
    draw();
    if (collisionCheck(x, y, z)) {
        timer.cancel();
        Tetris.gameOver();
    } else {
        startTimer();
    }
}

From source file:com.allen_sauer.gwt.dnd.demo.client.example.insertpanel.InsertPanelExample.java

License:Apache License

public InsertPanelExample(DemoDragHandler demoDragHandler) {
    addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE);
    int count = 0;

    // use the boundary panel as this composite's widget
    AbsolutePanel boundaryPanel = new AbsolutePanel();
    boundaryPanel.setSize("100%", "100%");
    setWidget(boundaryPanel);//from ww w . j av  a  2s. c o  m

    // initialize our column drag controller
    PickupDragController columnDragController = new PickupDragController(boundaryPanel, false);
    columnDragController.setBehaviorMultipleSelection(false);
    columnDragController.addDragHandler(demoDragHandler);

    // initialize our widget drag controller
    PickupDragController widgetDragController = new PickupDragController(boundaryPanel, false);
    widgetDragController.setBehaviorMultipleSelection(false);
    widgetDragController.addDragHandler(demoDragHandler);

    // initialize horizontal panel to hold our columns
    HorizontalPanel horizontalPanel = new HorizontalPanel();
    horizontalPanel.addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE_CONTAINER);
    horizontalPanel.setSpacing(SPACING);
    boundaryPanel.add(horizontalPanel);

    // initialize our column drop controller
    HorizontalPanelDropController columnDropController = new HorizontalPanelDropController(horizontalPanel);
    columnDragController.registerDropController(columnDropController);

    for (int col = 1; col <= COLUMNS; col++) {
        // initialize a vertical panel to hold the heading and a second vertical
        // panel
        VerticalPanel columnCompositePanel = new VerticalPanel();
        columnCompositePanel.addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE_COLUMN_COMPOSITE);

        // initialize inner vertical panel to hold individual widgets
        VerticalPanel verticalPanel = new VerticalPanelWithSpacer();
        verticalPanel.addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE_CONTAINER);
        verticalPanel.setSpacing(SPACING);
        horizontalPanel.add(columnCompositePanel);

        // initialize a widget drop controller for the current column
        VerticalPanelDropController widgetDropController = new VerticalPanelDropController(verticalPanel);
        widgetDragController.registerDropController(widgetDropController);

        // Put together the column pieces
        Label heading = new Label("Column " + col);
        heading.addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE_HEADING);
        columnCompositePanel.add(heading);
        columnCompositePanel.add(verticalPanel);

        // make the column draggable by its heading
        columnDragController.makeDraggable(columnCompositePanel, heading);

        for (int row = 1; row <= ROWS; row++) {
            // initialize a widget
            HTML widget = new HTML("Draggable&nbsp;#" + ++count);
            widget.addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE_WIDGET);
            widget.setHeight(Random.nextInt(4) + 2 + "em");
            verticalPanel.add(widget);

            // make the widget draggable
            widgetDragController.makeDraggable(widget);
        }
    }
}

From source file:com.allen_sauer.gwt.dnd.demo.client.example.resetcache.ResetCacheExample.java

License:Apache License

public ResetCacheExample(PickupDragController dragController) {
    super(dragController);
    addStyleName(CSS_DEMO_CACHE_EXAMPLE);

    // some colors to go with each tab
    String[] colors = { "#AAAAFF", "#AAFFAA", "#FFAAAA", "#FFFFAA", "#FFAAFF", "#AAFFFF", };

    // use the containing panel as this composite's widget
    AbsolutePanel containingPanel = new AbsolutePanel();
    containingPanel.setPixelSize(600, 300);
    setWidget(containingPanel);/*w ww . ja v  a  2  s . co  m*/

    // create a tab panel and populate with a few tabs
    TabPanel tabPanel = new TabPanel();
    tabPanel.addStyleName(CSS_DEMO_CACHE_EXAMPLE_TAB_PANEL);
    tabPanel.setPixelSize(300, 200);
    containingPanel.add(tabPanel, 40, 20);

    for (int i = 0; i < colors.length; i++) {
        // create a simple panel for the tab content
        AbsolutePanel contentPanel = new AbsolutePanel();
        contentPanel.setHeight("200px");
        contentPanel.getElement().getStyle().setProperty("backgroundColor", colors[i]);

        // create a tab widget
        HTML tabWidget = new HTML("Tab #" + (i + 1));
        tabWidget.setWordWrap(false);
        tabWidget.getElement().getStyle().setProperty("backgroundColor", colors[i]);
        tabPanel.add(contentPanel, tabWidget);

        // add drop controller to allow automatic tab selection and dropping on tab
        TabSelectingDropController tabSelectingDropController = new TabSelectingDropController(tabWidget,
                tabPanel, i);
        dragController.registerDropController(tabSelectingDropController);

        // create a sample draggable
        Widget draggableLabel = new Label("Drag me to another tab");
        draggableLabel.getElement().getStyle().setProperty("backgroundColor", colors[i]);
        draggableLabel.addStyleName(CSS_DEMO_CACHE_EXAMPLE_DRAGGABLE);

        // make label draggable
        dragController.makeDraggable(draggableLabel);

        // determine random location within target panel
        int left = Random.nextInt(200);
        int top = Random.nextInt(150);
        contentPanel.add(draggableLabel, left, top);

        // create a drop controller for the content panel
        AbsolutePositionDropController contentPanelDropController = new AbsolutePositionDropController(
                contentPanel);
        dragController.registerDropController(contentPanelDropController);
    }
    tabPanel.selectTab(0);

    // create a drop controller for the containing panel
    containerDropController = new AbsolutePositionDropController(containingPanel);
    dragController.registerDropController(containerDropController);
}

From source file:com.allen_sauer.gwt.dnd.demo.client.example.resetcache.TabSelectingDropController.java

License:Apache License

@Override
public void onDrop(DragContext context) {
    // assume content widget is an AbsolutePanel for now
    AbsolutePanel absolutePanel = (AbsolutePanel) tabPanel.getWidget(tabIndex);

    for (Widget widget : context.selectedWidgets) {
        // temporarily (invisibly) add draggable to get its dimensions
        widget.getElement().getStyle().setProperty("visibility", "hidden");
        absolutePanel.add(widget, 0, 0);

        // move widget to random location, and restore visibility
        int left = Random.nextInt(DOMUtil.getClientWidth(absolutePanel.getElement()) - widget.getOffsetWidth());
        int top = Random
                .nextInt(DOMUtil.getClientHeight(absolutePanel.getElement()) - widget.getOffsetHeight());
        absolutePanel.add(widget, left, top);
        widget.getElement().getStyle().setProperty("visibility", "");
    }/*from  www  .j  av  a  2  s  .c o m*/
    super.onDrop(context);
}

From source file:com.allen_sauer.gwt.dnd.demo.client.ui.MultiRowTabPanel.java

License:Apache License

public void selectTabByHistoryToken(String historyToken) {
    if ("IndexedPanelExample".equals(historyToken)) {
        historyToken = "InsertPanelExample";
    }/*from www.j a  v  a2  s .c o m*/
    Integer tabIndex = historyTokenMap.getIndex(historyToken);
    if (tabIndex == null) {
        // select a random example
        tabIndex = Random.nextInt(getTabCount() - 1);
    }
    selectTab(tabIndex);
}

From source file:com.allen_sauer.gwt.voices.demo.client.DemoSoundPanel.java

License:Apache License

public DemoSoundPanel(final ThirdPartySound thirdPartySound) {
    // use a horizontal panel to hold our content
    HorizontalPanel horizontalPanel = new HorizontalPanel();
    initWidget(horizontalPanel);/*from w ww . j av  a 2  s  .com*/

    // add a (temporarily disabled) play button
    final Button playButton = new Button("wait...");
    playButton.setEnabled(false);
    playButton.addStyleName("voices-button");
    horizontalPanel.add(playButton);

    final Button stopButton = new Button("wait...");
    stopButton.setEnabled(false);
    stopButton.addStyleName("voices-button");
    horizontalPanel.add(stopButton);

    // display a description of the sound next to the button
    horizontalPanel.add(new HTML("&nbsp;" + thirdPartySound.toHTMLString()));

    // display a load state status
    final HTML loadStateHTML = new HTML();
    horizontalPanel.add(loadStateHTML);

    // enable the play button once the sound has loaded
    thirdPartySound.getSound().addEventHandler(new SoundHandler() {
        @Override
        public void onPlaybackComplete(PlaybackCompleteEvent event) {
        }

        @Override
        public void onSoundLoadStateChange(final SoundLoadStateChangeEvent event) {
            // simulate a slight variable delay for local development
            new Timer() {
                @Override
                public void run() {
                    loadStateHTML.setHTML("&nbsp; (<code>" + event.getLoadState().name() + "</code>)");
                    switch (event.getLoadState()) {
                    case LOAD_STATE_SUPPORTED_AND_READY:
                    case LOAD_STATE_SUPPORTED_NOT_READY:
                    case LOAD_STATE_SUPPORTED_MAYBE_READY:
                        playButton.setEnabled(true);
                        playButton.setText("play");
                        stopButton.setEnabled(true);
                        stopButton.setText("stop");
                        break;
                    case LOAD_STATE_NOT_SUPPORTED:
                        playButton.setText("(sound or plugin unavailable)");
                        break;
                    case LOAD_STATE_SUPPORT_NOT_KNOWN:
                        playButton.setEnabled(true);
                        playButton.setText("play (may not work)");
                        stopButton.setEnabled(true);
                        stopButton.setText("stop");
                        break;
                    case LOAD_STATE_UNINITIALIZED:
                    default:
                        throw new IllegalArgumentException("Unhandled state " + event.getLoadState());
                    }
                }
            }.schedule(Random.nextInt(500) + 200);
        }
    });

    // play the sound when button is clicked
    playButton.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            thirdPartySound.getSound().play();
        }
    });

    stopButton.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            thirdPartySound.getSound().stop();
        }
    });
}

From source file:com.arcbees.gquery.tooltip.client.contactcell.ContactDatabase.java

License:Apache License

@SuppressWarnings("deprecation")
private ContactInfo createContactInfo() {
    // set all created contact in OTHERS category
    ContactInfo contact = new ContactInfo();
    contact.setLastName(nextValue(LAST_NAMES));
    if (Random.nextBoolean()) {
        // Male.//  w ww . j  ava 2s.  c o m
        contact.setFirstName(nextValue(MALE_FIRST_NAMES));
    } else {
        // Female.
        contact.setFirstName(nextValue(FEMALE_FIRST_NAMES));
    }

    // Create a birthday between 20-80 years ago.
    int year = (new Date()).getYear() - 21 - Random.nextInt(61);
    contact.setBirthday(new Date(year, Random.nextInt(12), 1 + Random.nextInt(31)));

    // Create an address.
    int addrNum = 1 + Random.nextInt(999);
    String addrStreet = nextValue(STREET_NAMES);
    String addrSuffix = nextValue(STREET_SUFFIX);
    contact.setAddress(addrNum + " " + addrStreet + " " + addrSuffix);
    return contact;
}

From source file:com.arcbees.gquery.tooltip.client.contactcell.ContactDatabase.java

License:Apache License

private <T> T nextValue(T[] array) {
    return array[Random.nextInt(array.length)];
}

From source file:com.bramosystems.oss.player.core.client.impl.playlist.PlaylistIndexOracle.java

License:Apache License

private int suggestIndexImpl(boolean up) {
    return _randomMode ? Random.nextInt(_indexSize) : (up ? ++_currentIndex : --_currentIndex);
}

From source file:com.charlie.client.panels.SonglistPanel.java

License:Open Source License

protected void randomizePlaylist() {

    previousSongs.clear();/*ww  w  . j  a  va2  s. c o m*/
    previousSongs.addAll(grid.getStore().getModels());

    SongModelData[] randomizedSongs = previousSongs.toArray(new SongModelData[] {});

    // --- Shuffle by exchanging each element randomly
    for (int i = 0; i < randomizedSongs.length; i++) {
        int randomPosition = Random.nextInt(randomizedSongs.length);
        SongModelData temp = randomizedSongs[i];
        randomizedSongs[i] = randomizedSongs[randomPosition];
        randomizedSongs[randomPosition] = temp;
    }

    grid.getStore().removeAll();
    grid.getStore().add(Arrays.asList(randomizedSongs));
    populatePlaylist(-1, false);

}