Search in sources :

Example 21 with Location

use of com.codename1.location.Location in project ChessProject by DylanSantora.

the class ChessBoard method rookMove90.

public ArrayList<Location> rookMove90(Location loc) {
    ArrayList<Location> moveLocs90 = new ArrayList<Location>();
    int r = loc.getRow();
    int c = loc.getCol();
    int tempC = c + 1;
    int rookColor = myBoard[r][c].getChessPiece().getMyColor();
    Boolean blocked90 = false;
    while (blocked90 == false) {
        if ((tempC < 8) && (tempC > -1)) {
            if ((myBoard[r][tempC].getChessPiece().getMyColor() == rookColor) && (!blocked90)) {
                blocked90 = true;
                break;
            } else if ((myBoard[r][tempC].getChessPiece().getMyColor() == (0)) && (!blocked90)) {
                moveLocs90.add(new Location(r, tempC));
            } else if ((myBoard[r][tempC].getChessPiece().getMyColor() - rookColor) == -2 || (myBoard[r][tempC].getChessPiece().getMyColor() - rookColor) == 2 && (!blocked90)) {
                System.out.println("-2 || +2");
                blocked90 = true;
                moveLocs90.add(new Location(r, tempC));
            }
        }
        if (!blocked90) {
            tempC++;
        }
        if (blocked90) {
            break;
        }
        if (!((tempC < 8) && (tempC > -1))) {
            blocked90 = true;
        }
    }
    return moveLocs90;
}
Also used : ArrayList(java.util.ArrayList) Location(location.Location)

Example 22 with Location

use of com.codename1.location.Location in project ChessProject by DylanSantora.

the class ChessBoard method pawnMove.

public ArrayList<Location> pawnMove(Location loc) {
    ArrayList<Location> moveLocs = new ArrayList<Location>();
    int r = loc.getRow();
    int c = loc.getCol();
    System.out.println("in pawnMove()");
    if (myBoard[r][c].getChessPiece().getMyColor() == -1) {
        if (myBoard[r + 1][c].getChessPiece().getMyColor() == 0) {
            moveLocs.add(new Location(r + 1, c));
        }
        if (r == 1 && myBoard[r + 1][c].getChessPiece().getMyColor() == 0) {
            if (myBoard[r + 2][c].getChessPiece().getMyColor() == 0) {
                moveLocs.add(new Location(r + 2, c));
            }
        }
        if (r < 8 && c > 0) {
            if (myBoard[r + 1][c - 1].getChessPiece().getMyColor() == 1) {
                moveLocs.add(new Location(r + 1, c - 1));
            }
        }
        if (r < 8 && c < 7) {
            if (myBoard[r + 1][c + 1].getChessPiece().getMyColor() == 1) {
                moveLocs.add(new Location(r + 1, c + 1));
            }
        }
    }
    if (myBoard[r][c].getChessPiece().getMyColor() == (1)) {
        if (myBoard[r - 1][c].getChessPiece().getMyColor() == (0)) {
            moveLocs.add(new Location(r - 1, c));
        }
        if (r == 6 && myBoard[r - 1][c].getChessPiece().getMyColor() == (0)) {
            if (myBoard[r - 2][c].getChessPiece().getMyColor() == 0) {
                moveLocs.add(new Location(r - 2, c));
            }
        }
        if (r > 0 && c > 0) {
            if (myBoard[r - 1][c - 1].getChessPiece().getMyColor() == (-1)) {
                moveLocs.add(new Location(r - 1, c - 1));
            }
        }
        if (r > 0 && c < 7) {
            if (myBoard[r - 1][c + 1].getChessPiece().getMyColor() == (-1)) {
                moveLocs.add(new Location(r - 1, c + 1));
            }
        }
    }
    System.out.println("out of pawnMove()");
    return moveLocs;
}
Also used : ArrayList(java.util.ArrayList) Location(location.Location)

Example 23 with Location

use of com.codename1.location.Location in project CodenameOne by codenameone.

the class CodenameOneImplementation method downloadImageToFileSystem.

/**
 * Downloads an image to file system. This will *not* first check to see if the file exists already.
 * It will download and overwrite any existing image at the provided location.
 *
 * <p>Some platforms may override this method to use platform-level caching.  E.g. Javascript will use
 * the browser cache for downloading the image.</p>
 *
 * @param url The URL of the image to download.
 * @param fileName The storage key to be used to store the image.
 * @param onSuccess Callback on success.  Will be executed on EDT.
 * @param onFail Callback on failure.  Will be executed on EDT.
 */
public void downloadImageToFileSystem(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setPost(false);
    cr.setFailSilently(true);
    cr.setReadResponseForErrors(false);
    cr.setDuplicateSupported(true);
    cr.setUrl(url);
    cr.downloadImageToFileSystem(fileName, onSuccess, onFail);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 24 with Location

use of com.codename1.location.Location in project CodenameOne by codenameone.

the class CodenameOneImplementation method downloadImageToStorage.

/**
 * Downloads an image to storage. This will *not* first check to see if the image is located in storage
 * already.  It will download and overwrite any existing image at the provided location.
 *
 * <p>Some platforms may override this method to use platform-level caching.  E.g. Javascript will use
 * the browser cache for downloading the image.</p>
 *
 * @param url The URL of the image to download.
 * @param fileName The storage key to be used to store the image.
 * @param onSuccess Callback on success.  Will be executed on EDT.
 * @param onFail Callback on failure.  Will be executed on EDT.
 */
public void downloadImageToStorage(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setPost(false);
    cr.setFailSilently(true);
    cr.setReadResponseForErrors(false);
    cr.setDuplicateSupported(true);
    cr.setUrl(url);
    cr.downloadImageToStorage(fileName, onSuccess, onFail);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 25 with Location

use of com.codename1.location.Location in project CodenameOne by codenameone.

the class ConnectionRequest method performOperation.

/**
 * Performs the actual network request on behalf of the network manager
 */
void performOperation() throws IOException {
    if (shouldStop()) {
        return;
    }
    if (cacheMode == CachingMode.OFFLINE) {
        InputStream is = getCachedData();
        if (is != null) {
            readResponse(is);
            Util.cleanup(is);
        } else {
            responseCode = 404;
            throw new IOException("File unavilable in cache");
        }
        return;
    }
    CodenameOneImplementation impl = Util.getImplementation();
    Object connection = null;
    input = null;
    output = null;
    redirecting = false;
    try {
        String actualUrl = createRequestURL();
        if (timeout > 0) {
            connection = impl.connect(actualUrl, isReadRequest(), isPost() || isWriteRequest(), timeout);
        } else {
            connection = impl.connect(actualUrl, isReadRequest(), isPost() || isWriteRequest());
        }
        if (shouldStop()) {
            return;
        }
        initConnection(connection);
        if (httpMethod != null) {
            impl.setHttpMethod(connection, httpMethod);
        }
        if (isCookiesEnabled()) {
            Vector v = impl.getCookiesForURL(actualUrl);
            if (v != null) {
                int c = v.size();
                if (c > 0) {
                    StringBuilder cookieStr = new StringBuilder();
                    Cookie first = (Cookie) v.elementAt(0);
                    cookieSent(first);
                    cookieStr.append(first.getName());
                    cookieStr.append("=");
                    cookieStr.append(first.getValue());
                    for (int iter = 1; iter < c; iter++) {
                        Cookie current = (Cookie) v.elementAt(iter);
                        cookieStr.append(";");
                        cookieStr.append(current.getName());
                        cookieStr.append("=");
                        cookieStr.append(current.getValue());
                        cookieSent(current);
                    }
                    impl.setHeader(connection, cookieHeader, initCookieHeader(cookieStr.toString()));
                } else {
                    String s = initCookieHeader(null);
                    if (s != null) {
                        impl.setHeader(connection, cookieHeader, s);
                    }
                }
            } else {
                String s = initCookieHeader(null);
                if (s != null) {
                    impl.setHeader(connection, cookieHeader, s);
                }
            }
        }
        if (checkSSLCertificates && canGetSSLCertificates()) {
            sslCertificates = getSSLCertificatesImpl(connection, url);
            checkSSLCertificates(sslCertificates);
            if (shouldStop()) {
                return;
            }
        }
        if (isWriteRequest()) {
            progress = NetworkEvent.PROGRESS_TYPE_OUTPUT;
            output = impl.openOutputStream(connection);
            if (shouldStop()) {
                return;
            }
            if (NetworkManager.getInstance().hasProgressListeners() && output instanceof BufferedOutputStream) {
                ((BufferedOutputStream) output).setProgressListener(this);
            }
            if (requestBody != null) {
                if (shouldWriteUTFAsGetBytes()) {
                    output.write(requestBody.getBytes("UTF-8"));
                } else {
                    OutputStreamWriter w = new OutputStreamWriter(output, "UTF-8");
                    w.write(requestBody);
                }
            } else {
                buildRequestBody(output);
            }
            if (shouldStop()) {
                return;
            }
            if (output instanceof BufferedOutputStream) {
                ((BufferedOutputStream) output).flushBuffer();
                if (shouldStop()) {
                    return;
                }
            }
        }
        timeSinceLastUpdate = System.currentTimeMillis();
        responseCode = impl.getResponseCode(connection);
        if (isCookiesEnabled()) {
            String[] cookies = impl.getHeaderFields("Set-Cookie", connection);
            if (cookies != null && cookies.length > 0) {
                ArrayList cook = new ArrayList();
                int clen = cookies.length;
                for (int iter = 0; iter < clen; iter++) {
                    Cookie coo = parseCookieHeader(cookies[iter]);
                    if (coo != null) {
                        cook.add(coo);
                        cookieReceived(coo);
                    }
                }
                impl.addCookie((Cookie[]) cook.toArray(new Cookie[cook.size()]));
            }
        }
        if (responseCode == 304 && cacheMode != CachingMode.OFF) {
            cacheUnmodified();
            return;
        }
        if (responseCode - 200 < 0 || responseCode - 200 > 100) {
            readErrorCodeHeaders(connection);
            // redirect to new location
            if (followRedirects && (responseCode == 301 || responseCode == 302 || responseCode == 303 || responseCode == 307)) {
                String uri = impl.getHeaderField("location", connection);
                if (!(uri.startsWith("http://") || uri.startsWith("https://"))) {
                    // relative URI's in the location header are illegal but some sites mistakenly use them
                    url = Util.relativeToAbsolute(url, uri);
                } else {
                    url = uri;
                }
                if (requestArguments != null && url.indexOf('?') > -1) {
                    requestArguments.clear();
                }
                if ((responseCode == 302 || responseCode == 303)) {
                    if (this.post && shouldConvertPostToGetOnRedirect()) {
                        this.post = false;
                        setWriteRequest(false);
                    }
                }
                impl.cleanup(output);
                impl.cleanup(connection);
                connection = null;
                output = null;
                if (!onRedirect(url)) {
                    redirecting = true;
                    retry();
                }
                return;
            }
            responseErrorMessge = impl.getResponseMessage(connection);
            handleErrorResponseCode(responseCode, responseErrorMessge);
            if (!isReadResponseForErrors()) {
                return;
            }
        }
        responseContentType = getHeader(connection, "Content-Type");
        if (cacheMode == CachingMode.SMART || cacheMode == CachingMode.MANUAL) {
            String last = getHeader(connection, "Last-Modified");
            String etag = getHeader(connection, "ETag");
            Preferences.set("cn1MSince" + createRequestURL(), last);
            Preferences.set("cn1Etag" + createRequestURL(), etag);
        }
        readHeaders(connection);
        contentLength = impl.getContentLength(connection);
        timeSinceLastUpdate = System.currentTimeMillis();
        progress = NetworkEvent.PROGRESS_TYPE_INPUT;
        if (isReadRequest()) {
            input = impl.openInputStream(connection);
            if (shouldStop()) {
                return;
            }
            if (input instanceof BufferedInputStream) {
                if (NetworkManager.getInstance().hasProgressListeners()) {
                    ((BufferedInputStream) input).setProgressListener(this);
                }
                ((BufferedInputStream) input).setYield(getYield());
            }
            if (!post && cacheMode == CachingMode.SMART && destinationFile == null && destinationStorage == null) {
                byte[] d = Util.readInputStream(input);
                OutputStream os = FileSystemStorage.getInstance().openOutputStream(getCacheFileName());
                os.write(d);
                os.close();
                readResponse(new ByteArrayInputStream(d));
            } else {
                readResponse(input);
            }
            if (shouldAutoCloseResponse()) {
                input.close();
            }
            input = null;
        }
    } finally {
        // always cleanup connections/streams even in case of an exception
        impl.cleanup(output);
        impl.cleanup(input);
        impl.cleanup(connection);
        timeSinceLastUpdate = -1;
        input = null;
        output = null;
        connection = null;
    }
    if (!isKilled()) {
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                postResponse();
            }
        });
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector) CodenameOneImplementation(com.codename1.impl.CodenameOneImplementation)

Aggregations

ArrayList (java.util.ArrayList)18 Location (location.Location)17 Location (com.codename1.location.Location)5 IOException (java.io.IOException)5 Point (com.codename1.ui.geom.Point)4 Component (com.codename1.ui.Component)3 ActionEvent (com.codename1.ui.events.ActionEvent)3 ConnectionRequest (com.codename1.io.ConnectionRequest)2 BrowserComponent (com.codename1.ui.BrowserComponent)2 ActionListener (com.codename1.ui.events.ActionListener)2 QualifiedCoordinates (javax.microedition.location.QualifiedCoordinates)2 Paint (com.codename1.charts.compat.Paint)1 Point (com.codename1.charts.models.Point)1 XYSeries (com.codename1.charts.models.XYSeries)1 Orientation (com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation)1 XYSeriesRenderer (com.codename1.charts.renderers.XYSeriesRenderer)1 BindTarget (com.codename1.cloud.BindTarget)1 CodenameOneImplementation (com.codename1.impl.CodenameOneImplementation)1 BufferedOutputStream (com.codename1.io.BufferedOutputStream)1 FileSystemStorage (com.codename1.io.FileSystemStorage)1