List of usage examples for com.google.gwt.user.client Random nextInt
public static native int nextInt(int upperBound) ;
int
between 0 (inclusive) and upperBound
(exclusive) with roughly equal probability of returning any particular int
in this range. From source file:org.thechiselgroup.biomixer.client.core.development.BenchmarkResourceSetFactory.java
License:Apache License
/** * Because the Mercator projection used in many maps gets useless for * extreme values and these are often not displayed, the latitude values are * limited to +/- 85 degrees./*ww w . ja v a 2 s .c o m*/ */ private static Resource createRandomLocation() { Resource locationResource = new Resource(); int randomLatitudeValue = Random.nextInt(100 * 85 * 2); double latitude = (randomLatitudeValue / 100d) - 85; locationResource.putValue(ResourceSetUtils.LATITUDE, latitude); int randomLongitudeValue = Random.nextInt(100 * 180 * 2); double longitude = (randomLongitudeValue / 100d) - 180; locationResource.putValue(ResourceSetUtils.LONGITUDE, longitude); return locationResource; }
From source file:org.thechiselgroup.biomixer.client.core.development.BenchmarkResourceSetFactory.java
License:Apache License
private static Double createRandomNumber(int min, int max) { int intervalLength = max - min; int randomValue = Random.nextInt(intervalLength); return (double) (randomValue + min); }
From source file:org.thechiselgroup.biomixer.client.core.development.BenchmarkResourceSetFactory.java
License:Apache License
private static String createRandomText(int numberOfCategories) { int randomValue = Random.nextInt(numberOfCategories); return "category-" + randomValue; }
From source file:org.thechiselgroup.biomixer.client.core.development.BenchmarkResourceSetFactory.java
License:Apache License
private static UriList createRandomUriList(String uriClass, int numberOfResources) { UriList uriList = new UriList(); int randomValue = Random.nextInt(numberOfResources); uriList.add(uriClass + ":" + randomValue); return uriList; }
From source file:org.vectomatic.svg.edu.client.maze.Cell.java
License:Open Source License
public Cell getNeighborWithAllWalls() { int count = 0; for (Cell cell : getNeighbors()) { if (cell.hasAllWalls()) { count++;// w w w . ja v a 2 s . c o m } } if (count > 0) { count = Random.nextInt(count); for (Cell cell : getNeighbors()) { if (cell.hasAllWalls()) { if (count == 0) { return cell; } else { count--; } } } } return null; }
From source file:org.vectomatic.svg.edu.client.maze.Maze.java
License:Open Source License
public void perfectRandomize() { long t1 = System.currentTimeMillis(); // Restore all the walls for (Cell cell : cells) { for (Cell neighbor : cell.getNeighbors()) { cell.setHasWall(neighbor, true); ((RectangularCell) cell).setOnPath(false); }/* w ww. j a v a 2s. c om*/ } // Generate a maze by DFS int visitedCount = 1; Cell currentCell = cells[Random.nextInt(cells.length)]; Stack<Cell> stack = new Stack<Cell>(); while (visitedCount < cells.length) { Cell cell = currentCell.getNeighborWithAllWalls(); if (cell != null) { cell.setHasWall(currentCell, false); stack.push(currentCell); currentCell = cell; visitedCount++; } else { currentCell = stack.pop(); } } long t2 = System.currentTimeMillis(); GWT.log("randomize = " + (t2 - t1)); }
From source file:org.vectomatic.svg.edu.client.push.PushMain.java
License:Open Source License
private void generate() { OMSVGDocument document = OMSVGParser.currentDocument(); OMSVGSVGElement rootSvg = document.createSVGSVGElement(); rootSvg.addMouseDownHandler(this); OMSVGDefsElement defs = document.createSVGDefsElement(); rootSvg.appendChild(defs);/*w w w. j a va2 s .c o m*/ // Copy the source SVG in a dedicated group inside // the defs OMSVGGElement imgGroup = document.createSVGGElement(); imgGroup.setId(ID_IMAGE); for (OMNode node : srcSvg.getChildNodes()) { imgGroup.appendChild(node.cloneNode(true)); } defs.appendChild(imgGroup); OMSVGRect viewBox = srcSvg.getViewBox().getBaseVal(); float width = viewBox.getWidth(); float height = viewBox.getHeight(); bbox = rootSvg.createSVGRect(); viewBox.assignTo(bbox); // Compute the number of tiles if (width < height) { xcount = levelList.getSelectedIndex() + 3; ycount = (int) (xcount * height / width); } else { ycount = levelList.getSelectedIndex() + 3; xcount = (int) (ycount * width / height); } hole = xcount * ycount - 1; // Create a thick border with rounded corners around the // drawing (15% of the original drawing size, corner radius // 2.5% of the original drawing size) // Add a 3 pixel margin around the tiles float borderWidth = (int) (0.075f * width); float borderHeight = (int) (0.075f * height); float borderRx = (int) (0.025f * width); float borderRy = (int) (0.025f * height); OMSVGRectElement borderOut = document.createSVGRectElement(viewBox.getX() - borderWidth - MARGIN, viewBox.getY() - borderHeight - MARGIN, viewBox.getWidth() + 2 * (borderWidth + MARGIN), viewBox.getHeight() + 2 * (borderHeight + MARGIN), borderRx, borderRy); borderOut.setClassNameBaseVal(style.borderOut()); OMSVGRectElement borderIn = document.createSVGRectElement(viewBox.getX() - MARGIN, viewBox.getY() - MARGIN, viewBox.getWidth() + 2 * MARGIN, viewBox.getHeight() + 2 * MARGIN, borderRx, borderRy); borderIn.setClassNameBaseVal(style.borderIn()); rootSvg.appendChild(borderOut); rootSvg.appendChild(borderIn); // rootSvg.setWidth(OMSVGLength.SVG_LENGTHTYPE_PERCENTAGE, 65f); // rootSvg.setHeight(OMSVGLength.SVG_LENGTHTYPE_PERCENTAGE, 65f); rootSvg.setViewBox(viewBox.getX() - borderWidth - MARGIN, viewBox.getY() - borderHeight - MARGIN, viewBox.getWidth() + 2 * (borderWidth + MARGIN), viewBox.getHeight() + 2 * (borderHeight + MARGIN)); // Create the tile clip-path // <clipPath id="cp"> // <rect id="cpr" x="0" y="0" width="130" height="130" rx="10" ry="10"/> // </clipPath> OMSVGClipPathElement clipPath = document.createSVGClipPathElement(); clipPath.setId(ID_CLIP_PATH); OMSVGRectElement clipRect = document.createSVGRectElement(viewBox.getX(), viewBox.getY(), width / xcount, height / ycount, borderRx, borderRy); clipRect.setId(ID_CLIP_RECT); clipPath.appendChild(clipRect); defs.appendChild(clipPath); // Create the tiles tiles = new OMSVGUseElement[xcount * ycount]; game = new int[xcount][]; for (int i = 0; i < xcount; i++) { game[i] = new int[ycount]; for (int j = 0; j < ycount; j++) { int index = i * ycount + j; if (index != hole) { // Create the tile definition // Each tile definition has the following structure // <g id="tileXXX"> // <g style="clip-path:url(#cp)"> // <g transform="translate(-260,0)"> // <use x="0" y="0" xlink:href="#puzzle"/> // </g> // </g> // <use x="0" y="0" xlink:href="#cp1r" style="fill:none;stroke:black;"/> // </g> OMSVGGElement tileDef = document.createSVGGElement(); tileDef.setId(ID_TILE + index); OMSVGGElement tileClipPath = document.createSVGGElement(); tileClipPath.getStyle().setSVGProperty(SVGConstants.CSS_CLIP_PATH_PROPERTY, "url(#" + ID_CLIP_PATH + ")"); OMSVGGElement tileTransform = document.createSVGGElement(); OMSVGTransform xform = rootSvg.createSVGTransform(); xform.setTranslate(viewBox.getX() - i * width / xcount, viewBox.getY() - j * height / ycount); tileTransform.getTransform().getBaseVal().appendItem(xform); OMSVGUseElement imgUse = document.createSVGUseElement(); imgUse.getX().getBaseVal().setValue(viewBox.getX()); imgUse.getY().getBaseVal().setValue(viewBox.getY()); imgUse.getHref().setBaseVal("#" + ID_IMAGE); OMSVGUseElement tileBorder = document.createSVGUseElement(); tileBorder.getX().getBaseVal().setValue(viewBox.getX()); tileBorder.getY().getBaseVal().setValue(viewBox.getY()); tileBorder.getHref().setBaseVal("#" + ID_CLIP_RECT); tileBorder.setClassNameBaseVal(style.tileBorder()); tileDef.appendChild(tileClipPath); tileClipPath.appendChild(tileTransform); tileTransform.appendChild(imgUse); tileDef.appendChild(tileBorder); defs.appendChild(tileDef); // Create the tile itself // <use x="130" y="260" xlink:href="#tileXXX"/> tiles[index] = document.createSVGUseElement(); tiles[index].getHref().setBaseVal("#" + ID_TILE + index); rootSvg.appendChild(tiles[index]); } setTile(i, j, index); } } // Add the SVG to the HTML page Element div = svgContainer.getElement(); if (pushSvg != null) { div.replaceChild(rootSvg.getElement(), pushSvg.getElement()); } else { div.appendChild(rootSvg.getElement()); } pushSvg = rootSvg; if (!GWT.isScript()) { GWT.log(pushSvg.getMarkup()); } // Display the puzzle in order for 1 sec, then scramble it waitTimer = new Timer() { public void run() { scrambleTimer = new Timer() { int repeatCount; @Override public void run() { int tileCount = xcount * ycount; List<Integer> array = new ArrayList<Integer>(); for (int i = 0; i < tileCount; i++) { array.add(i); } // Shuffle the tiles for (int i = 0; i < xcount; i++) { for (int j = 0; j < ycount; j++) { setTile(i, j, array.remove(Random.nextInt(tileCount--))); } } repeatCount++; if (repeatCount >= 5) { playing = true; cancel(); } } }; if ("true".equals(Window.Location.getParameter("win"))) { winAnimation(); } else { scrambleTimer.scheduleRepeating(200); } } }; waitTimer.schedule(1000); }
From source file:org.vectomatic.svg.edu.client.puzzle.Puzzle.java
License:Open Source License
public void shuffle() { List<Piece> pieceList = new ArrayList<Piece>(this.pieceList); // Shuffle the pieces int pieceCount = colCount * rowCount; for (int i = 0; i < colCount; i++) { for (int j = 0; j < rowCount; j++) { assemblyZone.setPiece(null, i, j); tileZone.setPiece(pieceList.remove(Random.nextInt(pieceCount--)), i, j); }//w w w.j av a2 s . co m } }
From source file:org.vectomatic.svg.samples.client.events.EventSample.java
License:Open Source License
@Override public TabLayoutPanel getPanel() { if (tabPanel == null) { tabPanel = binder.createAndBindUi(this); tabPanel.setTabText(0, "Events"); createCodeTabs("EventSample"); // Cast the document into a SVG document Element div = svgContainer.getElement(); OMSVGDocument doc = OMSVGParser.currentDocument(); // Create the root svg element svg = doc.createSVGSVGElement(); svg.setViewBox(0f, 0f, 400f, 200f); // Create a circle final OMSVGCircleElement circle = doc.createSVGCircleElement(80f, 80f, 40f); circle.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_PROPERTY, SVGConstants.CSS_BLACK_VALUE); setCircleColor(circle, SVGConstants.CSS_RED_VALUE); svg.appendChild(circle);//from w w w. j a va 2 s . co m // Set a mousedown event handler circle.addMouseDownHandler(new MouseDownHandler() { final String[] colors = new String[] { SVGConstants.CSS_RED_VALUE, SVGConstants.CSS_BLUE_VALUE, SVGConstants.CSS_GREEN_VALUE, SVGConstants.CSS_YELLOW_VALUE, SVGConstants.CSS_PINK_VALUE }; @Override public void onMouseDown(MouseDownEvent event) { String color = getCircleColor(circle); while (color.equals(getCircleColor(circle))) { setCircleColor(circle, colors[Random.nextInt(colors.length)]); } event.stopPropagation(); event.preventDefault(); } }); // Create a square square = doc.createSVGRectElement(140f, 40f, 80f, 80f, 0f, 0f); square.getStyle().setSVGProperty(SVGConstants.CSS_STROKE_PROPERTY, SVGConstants.CSS_BLACK_VALUE); square.getStyle().setSVGProperty(SVGConstants.CSS_FILL_PROPERTY, SVGConstants.CSS_GREEN_VALUE); square.addMouseDownHandler(this); square.addMouseUpHandler(this); square.addMouseMoveHandler(this); svg.appendChild(square); // Insert the SVG root element into the HTML UI div.appendChild(svg.getElement()); } return tabPanel; }
From source file:ro.utcluj.larkc.visual.client.OfcgwtCharts.java
License:Open Source License
private Widget addScatterChart() { ChartWidget chart = new ChartWidget(); ChartData cd = new ChartData("X Y Distribution", "font-size: 14px; font-family: Verdana; text-align: center;"); cd.setBackgroundColour("#ffffff"); ScatterChart scat = new ScatterChart(); scat.setDotSize(3);// ww w . j a va2s . c o m for (int n = 0; n < 100; n++) { int x = Random.nextInt(50) - 25; int y = Random.nextInt(50) - 25; scat.addPoints(new ScatterChart.Point(x, y)); } XAxis xa = new XAxis(); xa.setRange(-25, 25, 5); cd.setXAxis(xa); YAxis ya = new YAxis(); ya.setRange(-25, 25, 5); cd.setYAxis(ya); cd.addElements(scat); chart.setSize("600", "600"); chart.setJsonData(cd.toString()); return chart; }