use of com.codename1.location.Location in project CodenameOne by codenameone.
the class XYChart method draw.
/**
* The graphical representation of the XY chart.
*
* @param canvas the canvas to paint to
* @param x the top left x value of the view to draw to
* @param y the top left y value of the view to draw to
* @param width the width of the view to draw to
* @param height the height of the view to draw to
* @param paint the paint
*/
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
paint.setAntiAlias(mRenderer.isAntialiasing());
int legendSize = getLegendSize(mRenderer, height / 5, mRenderer.getAxisTitleTextSize());
int[] margins = mRenderer.getMargins();
int left = x + margins[1] + (int) mRenderer.getAxisTitleTextSize() + (mRenderer.isShowLabels() ? (int) mRenderer.getLabelsTextSize() : 0);
int top = y + margins[0];
int right = x + width - margins[3];
int sLength = mDataset.getSeriesCount();
String[] titles = new String[sLength];
for (int i = 0; i < sLength; i++) {
titles[i] = mDataset.getSeriesAt(i).getTitle();
}
if (mRenderer.isFitLegend() && mRenderer.isShowLegend()) {
legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, true);
}
int bottom = y + height - margins[2] - legendSize - (int) mRenderer.getAxisTitleTextSize() - (mRenderer.isShowLabels() ? (int) mRenderer.getLabelsTextSize() : 0);
if (mScreenR == null) {
mScreenR = new Rectangle();
}
mScreenR.setBounds(left, top, right - left, bottom - top);
drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
if (paint.getTypeface() == null || (mRenderer.getTextTypeface() != null && paint.getTypeface().equals(mRenderer.getTextTypeface())) || !paint.getTypeface().toString().equals(mRenderer.getTextTypefaceName()) || paint.getTypeface().getStyle() != mRenderer.getTextTypefaceStyle()) {
if (mRenderer.getTextTypeface() != null) {
paint.setTypeface(mRenderer.getTextTypeface());
} else {
paint.setTypeface(Font.createSystemFont(mRenderer.getTextTypefaceName(), mRenderer.getTextTypefaceStyle(), Font.SIZE_SMALL));
}
}
Orientation or = mRenderer.getOrientation();
if (or == Orientation.VERTICAL) {
right -= legendSize;
bottom += legendSize - 20;
}
int angle = or.getAngle();
boolean rotate = angle == 90;
mScale = (float) (height) / width;
mTranslate = Math.abs(width - height) / 2;
if (mScale < 1) {
mTranslate *= -1;
}
mCenter = new Point((x + width) / 2, (y + height) / 2);
if (rotate) {
transform(canvas, angle, false);
}
int maxScaleNumber = -Integer.MAX_VALUE;
for (int i = 0; i < sLength; i++) {
maxScaleNumber = Math.max(maxScaleNumber, mDataset.getSeriesAt(i).getScaleNumber());
}
maxScaleNumber++;
if (maxScaleNumber < 0) {
return;
}
double[] minX = new double[maxScaleNumber];
double[] maxX = new double[maxScaleNumber];
double[] minY = new double[maxScaleNumber];
double[] maxY = new double[maxScaleNumber];
boolean[] isMinXSet = new boolean[maxScaleNumber];
boolean[] isMaxXSet = new boolean[maxScaleNumber];
boolean[] isMinYSet = new boolean[maxScaleNumber];
boolean[] isMaxYSet = new boolean[maxScaleNumber];
for (int i = 0; i < maxScaleNumber; i++) {
minX[i] = mRenderer.getXAxisMin(i);
maxX[i] = mRenderer.getXAxisMax(i);
minY[i] = mRenderer.getYAxisMin(i);
maxY[i] = mRenderer.getYAxisMax(i);
isMinXSet[i] = mRenderer.isMinXSet(i);
isMaxXSet[i] = mRenderer.isMaxXSet(i);
isMinYSet[i] = mRenderer.isMinYSet(i);
isMaxYSet[i] = mRenderer.isMaxYSet(i);
if (mCalcRange.get(i) == null) {
mCalcRange.put(i, new double[4]);
}
}
double[] xPixelsPerUnit = new double[maxScaleNumber];
double[] yPixelsPerUnit = new double[maxScaleNumber];
for (int i = 0; i < sLength; i++) {
XYSeries series = mDataset.getSeriesAt(i);
int scale = series.getScaleNumber();
if (series.getItemCount() == 0) {
continue;
}
if (!isMinXSet[scale]) {
double minimumX = series.getMinX();
if (minimumX != MathHelper.NULL_VALUE) {
minX[scale] = minX[scale] == MathHelper.NULL_VALUE ? minimumX : Math.min(minX[scale], minimumX);
mCalcRange.get(scale)[0] = minX[scale];
} else {
minX[scale] = 0;
mCalcRange.get(scale)[0] = 0;
}
}
if (!isMaxXSet[scale]) {
double maximumX = series.getMaxX();
if (maximumX != MathHelper.NULL_VALUE) {
maxX[scale] = maxX[scale] == MathHelper.NULL_VALUE ? maximumX : Math.max(maxX[scale], maximumX);
mCalcRange.get(scale)[1] = maxX[scale];
} else {
maxX[scale] = minX[scale] + 1;
mCalcRange.get(scale)[1] = maxX[scale];
}
}
if (!isMinYSet[scale]) {
double minimumY = series.getMinY();
if (minimumY != MathHelper.NULL_VALUE) {
minY[scale] = minY[scale] == MathHelper.NULL_VALUE ? minimumY : Math.min(minY[scale], (float) minimumY);
mCalcRange.get(scale)[2] = minY[scale];
} else {
minY[scale] = 0;
mCalcRange.get(scale)[2] = 0;
}
}
if (!isMaxYSet[scale]) {
double maximumY = series.getMaxY();
if (maximumY != MathHelper.NULL_VALUE) {
maxY[scale] = maxY[scale] == MathHelper.NULL_VALUE ? maximumY : Math.max(maxY[scale], (float) maximumY);
mCalcRange.get(scale)[3] = maxY[scale];
} else {
maxY[scale] = minY[scale] + 1;
mCalcRange.get(scale)[3] = maxY[scale];
}
}
}
for (int i = 0; i < maxScaleNumber; i++) {
if (maxX[i] - minX[i] != 0) {
xPixelsPerUnit[i] = (right - left) / (maxX[i] - minX[i]);
}
if (maxY[i] - minY[i] != 0) {
yPixelsPerUnit[i] = (float) ((bottom - top) / (maxY[i] - minY[i]));
}
// the X axis on multiple scales was wrong without this fix
if (i > 0) {
xPixelsPerUnit[i] = xPixelsPerUnit[0];
minX[i] = minX[0];
maxX[i] = maxX[0];
}
}
boolean hasValues = false;
// use a linked list for these reasons:
// 1) Avoid a large contiguous memory allocation
// 2) We don't need random seeking, only sequential reading/writing, so
// linked list makes sense
clickableAreas = new HashMap<Integer, List<ClickableArea>>();
for (int i = 0; i < sLength; i++) {
XYSeries series = mDataset.getSeriesAt(i);
int scale = series.getScaleNumber();
if (series.getItemCount() == 0) {
continue;
}
hasValues = true;
XYSeriesRenderer seriesRenderer = (XYSeriesRenderer) mRenderer.getSeriesRendererAt(i);
// int originalValuesLength = series.getItemCount();
// int valuesLength = originalValuesLength;
// int length = valuesLength * 2;
List<Float> points = new ArrayList<Float>();
List<Double> values = new ArrayList<Double>();
float yAxisValue = Math.min(bottom, (float) (bottom + yPixelsPerUnit[scale] * minY[scale]));
LinkedList<ClickableArea> clickableArea = new LinkedList<ClickableArea>();
clickableAreas.put(i, clickableArea);
synchronized (series) {
SortedMap<Double, Double> range = series.getRange(minX[scale], maxX[scale], seriesRenderer.isDisplayBoundingPoints());
int startIndex = -1;
for (Double value : range.keySet()) {
double xValue = value;
Double rValue = range.get(value);
double yValue = rValue.doubleValue();
if (startIndex < 0 && (!isNullValue(yValue) || isRenderNullValues())) {
startIndex = series.getIndexForKey(xValue);
}
// points.add((float) (left + xPixelsPerUnit[scale]
// * (value.getKey().floatValue() - minX[scale])));
// points.add((float) (bottom - yPixelsPerUnit[scale]
// * (value.getValue().floatValue() - minY[scale])));
values.add(value);
values.add(rValue);
if (!isNullValue(yValue)) {
points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
points.add((float) (bottom - yPixelsPerUnit[scale] * (yValue - minY[scale])));
} else if (isRenderNullValues()) {
points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
points.add((float) (bottom - yPixelsPerUnit[scale] * (-minY[scale])));
} else {
if (points.size() > 0) {
drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex);
ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(points, values, yAxisValue, i, startIndex);
clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
points.clear();
values.clear();
startIndex = -1;
}
clickableArea.add(null);
}
}
int count = series.getAnnotationCount();
if (count > 0) {
paint.setColor(seriesRenderer.getAnnotationsColor());
paint.setTextSize(seriesRenderer.getAnnotationsTextSize());
paint.setTextAlign(seriesRenderer.getAnnotationsTextAlign());
Rectangle2D bound = new Rectangle2D();
for (int j = 0; j < count; j++) {
float xS = (float) (left + xPixelsPerUnit[scale] * (series.getAnnotationX(j) - minX[scale]));
float yS = (float) (bottom - yPixelsPerUnit[scale] * (series.getAnnotationY(j) - minY[scale]));
paint.getTextBounds(series.getAnnotationAt(j), 0, series.getAnnotationAt(j).length(), bound);
if (xS < (xS + bound.getWidth()) && yS < canvas.getHeight()) {
drawString(canvas, series.getAnnotationAt(j), xS, yS, paint);
}
}
}
if (points.size() > 0) {
drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex);
ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(points, values, yAxisValue, i, startIndex);
clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
}
}
}
// draw stuff over the margins such as data doesn't render on these areas
drawBackground(mRenderer, canvas, x, bottom, width, height - bottom, paint, true, mRenderer.getMarginsColor());
drawBackground(mRenderer, canvas, x, y, width, margins[0], paint, true, mRenderer.getMarginsColor());
if (or == Orientation.HORIZONTAL) {
drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
drawBackground(mRenderer, canvas, right, y, margins[3], height - y, paint, true, mRenderer.getMarginsColor());
} else if (or == Orientation.VERTICAL) {
drawBackground(mRenderer, canvas, right, y, width - right, height - y, paint, true, mRenderer.getMarginsColor());
drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
}
boolean showLabels = mRenderer.isShowLabels() && hasValues;
boolean showGridX = mRenderer.isShowGridX();
boolean showTickMarks = mRenderer.isShowTickMarks();
// boolean showCustomTextGridX = mRenderer.isShowCustomTextGridX();
boolean showCustomTextGridY = mRenderer.isShowCustomTextGridY();
if (showLabels || showGridX) {
List<Double> xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels()));
Map<Integer, List<Double>> allYLabels = getYLabels(minY, maxY, maxScaleNumber);
int xLabelsLeft = left;
if (showLabels) {
paint.setColor(mRenderer.getXLabelsColor());
paint.setTextSize(mRenderer.getLabelsTextSize());
paint.setTextAlign(mRenderer.getXLabelsAlign());
// if (mRenderer.getXLabelsAlign() == Align.LEFT) {
// xLabelsLeft += mRenderer.getLabelsTextSize() / 4;
// }
}
drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, xLabelsLeft, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]);
drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY);
if (showLabels) {
paint.setColor(mRenderer.getLabelsColor());
for (int i = 0; i < maxScaleNumber; i++) {
int axisAlign = mRenderer.getYAxisAlign(i);
Double[] yTextLabelLocations = mRenderer.getYTextLabelLocations(i);
for (Double location : yTextLabelLocations) {
if (minY[i] <= location && location <= maxY[i]) {
float yLabel = (float) (bottom - yPixelsPerUnit[i] * (location.doubleValue() - minY[i]));
String label = mRenderer.getYTextLabel(location, i);
paint.setColor(mRenderer.getYLabelsColor(i));
paint.setTextAlign(mRenderer.getYLabelsAlign(i));
if (or == Orientation.HORIZONTAL) {
if (axisAlign == Align.LEFT) {
if (showTickMarks) {
canvas.drawLine(left + getLabelLinePos(axisAlign), yLabel, left, yLabel, paint);
}
drawText(canvas, label, left - mRenderer.getYLabelsPadding(), yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle());
} else {
if (showTickMarks) {
canvas.drawLine(right, yLabel, right + getLabelLinePos(axisAlign), yLabel, paint);
}
drawText(canvas, label, right - mRenderer.getYLabelsPadding(), yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle());
}
if (showCustomTextGridY) {
paint.setColor(mRenderer.getGridColor(i));
canvas.drawLine(left, yLabel, right, yLabel, paint);
}
} else {
if (showTickMarks) {
canvas.drawLine(right - getLabelLinePos(axisAlign), yLabel, right, yLabel, paint);
}
drawText(canvas, label, right + 10, yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle());
if (showCustomTextGridY) {
paint.setColor(mRenderer.getGridColor(i));
canvas.drawLine(right, yLabel, left, yLabel, paint);
}
}
}
}
}
}
if (showLabels) {
paint.setColor(mRenderer.getLabelsColor());
float size = mRenderer.getAxisTitleTextSize();
paint.setTextSize(size);
paint.setTextAlign(Align.CENTER);
if (or == Orientation.HORIZONTAL) {
drawText(canvas, mRenderer.getXTitle(), x + width / 2, bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding() + size, paint, 0);
for (int i = 0; i < maxScaleNumber; i++) {
int axisAlign = mRenderer.getYAxisAlign(i);
if (axisAlign == Align.LEFT) {
drawText(canvas, mRenderer.getYTitle(i), x + size, y + height / 2, paint, -90);
} else {
drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90);
}
}
paint.setTextSize(mRenderer.getChartTitleTextSize());
drawText(canvas, mRenderer.getChartTitle(), x + width / 2, y + mRenderer.getChartTitleTextSize(), paint, 0);
} else if (or == Orientation.VERTICAL) {
drawText(canvas, mRenderer.getXTitle(), x + width / 2, y + height - size + mRenderer.getXLabelsPadding(), paint, -90);
drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0);
paint.setTextSize(mRenderer.getChartTitleTextSize());
drawText(canvas, mRenderer.getChartTitle(), x + size, top + height / 2, paint, 0);
}
}
}
if (or == Orientation.HORIZONTAL) {
drawLegend(canvas, mRenderer, titles, left, right, y + (int) mRenderer.getXLabelsPadding(), width, height, legendSize, paint, false);
} else if (or == Orientation.VERTICAL) {
transform(canvas, angle, true);
drawLegend(canvas, mRenderer, titles, left, right, y + (int) mRenderer.getXLabelsPadding(), width, height, legendSize, paint, false);
transform(canvas, angle, false);
}
if (mRenderer.isShowAxes()) {
paint.setColor(mRenderer.getXAxisColor());
canvas.drawLine(left, bottom, right, bottom, paint);
paint.setColor(mRenderer.getYAxisColor());
boolean rightAxis = false;
for (int i = 0; i < maxScaleNumber && !rightAxis; i++) {
rightAxis = mRenderer.getYAxisAlign(i) == Align.RIGHT;
}
if (or == Orientation.HORIZONTAL) {
canvas.drawLine(left, top, left, bottom, paint);
if (rightAxis) {
canvas.drawLine(right, top, right, bottom, paint);
}
} else if (or == Orientation.VERTICAL) {
canvas.drawLine(right, top, right, bottom, paint);
}
}
if (rotate) {
transform(canvas, angle, true);
}
}
use of com.codename1.location.Location in project CodenameOne by codenameone.
the class StubLocationManager method addGeoFencing.
@Override
public void addGeoFencing(final Class GeofenceListenerClass, Geofence gf) {
if (gf.getId() != null) {
String id = gf.getId();
int index = -1;
for (Geofence f : geoFences) {
if (id.equals(f.getId())) {
index = geoFences.indexOf(f);
break;
}
}
if (index >= 0) {
geoFences.remove(index);
}
if (gf.getRadius() < 0) {
throw new IllegalArgumentException("Attempt to add geofence with negative radius");
}
if (gf.getRadius() < 100) {
Log.p("Adding Geofence with a radius of " + gf.getRadius() + " metres. On an actual device, the effective radius will vary. Typical Android and iOS devices have a minimum geofence radius of approximately 100m");
}
long expires = gf.getExpiration();
geoFences.add(gf);
if (geofenceTimer == null) {
geofenceTimer = new java.util.Timer();
geofenceTask = new TimerTask() {
public void run() {
Display.getInstance().callSerially(new Runnable() {
public void run() {
Location loc;
try {
loc = getCurrentLocation();
if (JavaSEPort.locSimulation == null) {
loc.setLongitude(loc.getLongitude() + 0.001);
loc.setLatitude(loc.getLatitude() + +0.001);
} else {
loc.setLongitude(JavaSEPort.locSimulation.getLongitude());
loc.setLatitude(JavaSEPort.locSimulation.getLatitude());
}
// Do exits first
for (final Geofence f : geoFences) {
if (!isInRegion(loc, f) && insideFences.contains(f.getId())) {
insideFences.remove(f.getId());
try {
final GeofenceListener l = (GeofenceListener) GeofenceListenerClass.newInstance();
new Thread() {
public void run() {
// In a separate thread to simulate that
// this might not happen on EDT
l.onExit(f.getId());
}
}.start();
} catch (Throwable t) {
Log.e(t);
}
}
}
// Do entrances next
for (final Geofence f : geoFences) {
if (isInRegion(loc, f) && !insideFences.contains(f.getId())) {
insideFences.add(f.getId());
try {
final GeofenceListener l = (GeofenceListener) GeofenceListenerClass.newInstance();
new Thread() {
public void run() {
// In a separate thread to simulate that
// this might not happen on EDT
l.onEntered(f.getId());
}
}.start();
} catch (Throwable t) {
Log.e(t);
}
}
}
} catch (IOException ex) {
Logger.getLogger(StubLocationManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
};
geofenceTimer.schedule(geofenceTask, new Date(System.currentTimeMillis() + 10000L), 10000L);
}
} else {
Log.p("Attempt to add Geofence with null ID", Log.WARNING);
}
}
use of com.codename1.location.Location in project codenameone-google-maps by codenameone.
the class MapContainer method addMarker.
/**
* Adds a marker to the map with the given attributes
* @param opts The marker options.
* @return marker reference object that should be used when removing the marker
*/
public MapObject addMarker(MarkerOptions opts) {
// public MapObject addMarker(EncodedImage icon, Coord location, String text, String longText, final ActionListener onClick) {
EncodedImage icon = opts.getIcon();
Coord location = opts.getLocation();
String text = opts.getText();
String longText = opts.getLongText();
ActionListener onClick = opts.getOnClick();
if (internalNative != null) {
byte[] iconData = null;
if (icon != null) {
iconData = icon.getImageData();
internalNative.setMarkerSize(icon.getWidth(), icon.getHeight());
}
long key = internalNative.addMarker(iconData, location.getLatitude(), location.getLongitude(), text, longText, onClick != null, opts.anchorU, opts.anchorV);
MapObject o = new MapObject();
o.mapKey = key;
o.callback = onClick;
markers.add(o);
return o;
} else {
if (internalLightweightCmp != null) {
PointLayer pl = new PointLayer(location, text, icon);
if (points == null) {
points = new PointsLayer();
internalLightweightCmp.addLayer(points);
}
points.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
PointLayer point = (PointLayer) evt.getSource();
for (MapObject o : markers) {
if (o.point == point) {
if (o.callback != null) {
o.callback.actionPerformed(new ActionEvent(o));
}
evt.consume();
return;
}
}
}
});
points.addPoint(pl);
MapObject o = new MapObject();
o.point = pl;
o.callback = onClick;
markers.add(o);
internalLightweightCmp.revalidate();
return o;
} else {
String uri = null;
int iconWidth = 0;
int iconHeight = 0;
int anchorU = 0;
int anchorV = 0;
if (icon != null) {
uri = WebBrowser.createDataURI(icon.getImageData(), "image/png");
iconWidth = icon.getWidth();
iconHeight = icon.getHeight();
anchorU = (int) (icon.getWidth() * opts.anchorU);
anchorV = (int) (icon.getHeight() * opts.anchorV);
}
// browserBridge.waitForReady();
MapObject o = new MapObject();
o.callback = onClick;
o.pending = true;
final String fUri = uri;
final int fIconWidth = iconWidth;
final int fIconHeight = iconHeight;
final float fAnchorU = anchorU;
final float fAnchorV = anchorV;
browserBridge.ready(() -> {
internalBrowser.execute("callback.onSuccess(" + BRIDGE + ".addMarker(${0}, ${1}, ${2}, ${3}, ${4}, ${5}, ${6}, ${7}, ${8}))", new Object[] { // long key = ((Double)browserBridge.bridge.call("addMarker", new Object[]{
fUri, location.getLatitude(), location.getLongitude(), text, longText, fIconWidth, fIconHeight, fAnchorU, fAnchorV }, jsres -> {
o.mapKey = jsres.getInt();
o.pending = false;
});
});
// MapObject o = new MapObject();
// o.mapKey = res.getInt();
// o.callback = onClick;
markers.add(o);
// System.out.println("MapKey added "+o.mapKey);
return o;
}
}
}
use of com.codename1.location.Location in project codenameone-google-maps by codenameone.
the class MapInfoPanel method createMarkerButton.
private Button createMarkerButton(MapObject mo, String label, Image icon, final Coord location) {
Button b = new Button(label, icon);
b.addActionListener(e -> {
map.zoom(location, (int) map.getZoom());
});
return b;
}
use of com.codename1.location.Location in project ChessProject by DylanSantora.
the class ChessBoard method promotion.
public Location promotion() {
for (int c = 0; c < 8; c++) {
if (myBoard[0][c].getChessPiece().getMyPieceType() == ("pawn") && myBoard[0][c].getChessPiece().getMyColor() == 1) {
myBoard[0][c].setChessPiece(new Queen(1));
Location promLoc = new Location(0, c);
return promLoc;
}
if (myBoard[7][c].getChessPiece().getMyPieceType() == ("pawn") && myBoard[7][c].getChessPiece().getMyColor() == -1) {
myBoard[7][c].setChessPiece(new Queen(-1));
Location promLoc = new Location(7, c);
return promLoc;
}
}
return new Location(4, 4);
}
Aggregations