AI2 Component  (Version nb184)
Ball.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2019 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
20 
21 import android.graphics.Canvas;
22 import android.graphics.Paint;
23 
39 @DesignerComponent(version = YaVersion.BALL_COMPONENT_VERSION,
40  description = "<p>A round 'sprite' that can be placed on a " +
41  "<code>Canvas</code>, where it can react to touches and drags, " +
42  "interact with other sprites (<code>ImageSprite</code>s and other " +
43  "<code>Ball</code>s) and the edge of the Canvas, and move according " +
44  "to its property values.</p>" +
45  "<p>For example, to have a <code>Ball</code> move 4 pixels toward the " +
46  "top of a <code>Canvas</code> every 500 milliseconds (half second), " +
47  "you would set the <code>Speed</code> property to 4 [pixels], the " +
48  "<code>Interval</code> property to 500 [milliseconds], the " +
49  "<code>Heading</code> property to 90 [degrees], and the " +
50  "<code>Enabled</code> property to <code>True</code>.</p>" +
51  "<p>The difference between a <code>Ball</code> and an <code>ImageSprite</code> is " +
52  "that the latter can get its appearance from an image file, while a " +
53  "<code>Ball</code>'s appearance can be changed only by varying its " +
54  "<code>PaintColor</code> and <code>Radius</code> properties.</p>",
55  category = ComponentCategory.ANIMATION)
56 @SimpleObject
57 public final class Ball extends Sprite {
58  private int radius;
59  private int paintColor;
60  private Paint paint;
61  static final int DEFAULT_RADIUS = 5;
62 
63  public Ball(ComponentContainer container) {
64  super(container);
65  paint = new Paint();
66 
67  // Set default properties.
69  Radius(DEFAULT_RADIUS);
70  }
71 
72  // Implement or override methods
73 
74  @Override
75  protected void onDraw(Canvas canvas) {
76  if (visible) {
77  float correctedXLeft = (float)(xLeft * form.deviceDensity());
78  float correctedYTop = (float)(yTop * form.deviceDensity());
79  float correctedRadius = radius * form.deviceDensity();
80  canvas.drawCircle(correctedXLeft + correctedRadius, correctedYTop +
81  correctedRadius, correctedRadius, paint);
82  }
83  }
84 
85  // The following four methods are required by abstract superclass
86  // VisibleComponent. Because we don't want to expose them to the Simple
87  // programmer, we omit the SimpleProperty and DesignerProperty pragmas.
88  @Override
89  public int Height() {
90  return 2 * radius;
91  }
92 
93  @Override
94  public void Height(int height) {
95  // ignored
96  }
97 
98  @Override
99  public void HeightPercent(int pCent) {
100  // ignored
101  }
102 
103  @Override
104  public int Width() {
105  return 2 * radius;
106  }
107 
108  @Override
109  public void Width(int width) {
110  // ignored
111  }
112 
113  @Override
114  public void WidthPercent(int pCent) {
115  // ignored
116  }
117 
118  @Override
119  public boolean containsPoint(double qx, double qy) {
120  return ((qx - xCenter) * (qx - xCenter) + (qy - yCenter) * (qy - yCenter))
121  <= radius * radius;
122  }
123 
124 
125  // Additional properties
126 
128  defaultValue = "5")
129  @SimpleProperty(description = "The distance from the edge of the Ball to its center.")
130  public void Radius(int radius) {
131  int dr = radius - this.radius;
132  // If the origin is at the center, the upper left corner moves to keep the center constant.
133  if (originAtCenter) {
134  xLeft -= dr;
135  yTop -= dr;
136  }
137  this.radius = radius;
138  registerChange();
139  }
140 
145  public int Radius() {
146  return radius;
147  }
148 
155  description = "The color of the Ball.",
156  category = PropertyCategory.APPEARANCE)
157  @IsColor
158  public int PaintColor() {
159  return paintColor;
160  }
161 
169  defaultValue = Component.DEFAULT_VALUE_COLOR_BLACK)
171  public void PaintColor(int argb) {
172  paintColor = argb;
173  if (argb != Component.COLOR_DEFAULT) {
174  PaintUtil.changePaint(paint, argb);
175  } else {
176  // The default paint color is black.
178  }
179  registerChange();
180  }
181 
182  // We need to override methods defined in the superclass to generate appropriate documentation.
183 
189  defaultValue = DEFAULT_ORIGIN_AT_CENTER ? "True" : "False")
190  @SimpleProperty(userVisible = false,
191  description = "Whether the x- and y-coordinates should represent the center of the Ball " +
192  "(true) or its left and top edges (false).")
193  public void OriginAtCenter(boolean b) {
194  super.OriginAtCenter(b);
195  }
196 
203  description = "The horizontal coordinate of the Ball, increasing as the Ball moves right. " +
204  "If the property OriginAtCenter is true, the coordinate is for the center of the Ball; " +
205  "otherwise, it is for the leftmost point of the Ball.")
206  @Override
207  public double X() {
208  return super.X();
209  }
210 
217  description = "The vertical coordinate of the Ball, increasing as the Ball moves " +
218  "down. If the property OriginAtCenter is true, the coordinate is for the center of the Ball; " +
219  "otherwise, it is for the uppermost point of the Ball.")
220  @Override
221  public double Y() {
222  return super.Y();
223  }
224 
233  description = "Sets the x and y coordinates of the Ball. If CenterAtOrigin is " +
234  "true, the center of the Ball will be placed here. Otherwise, the top left edge of the Ball " +
235  "will be placed at the specified coordinates.")
236  @Override
237  public void MoveTo(double x, double y) {
238  super.MoveTo(x, y);
239  }
240 }
com.google.appinventor.components.runtime.Ball.Ball
Ball(ComponentContainer container)
Definition: Ball.java:63
com.google.appinventor.components.runtime.Ball.X
double X()
Definition: Ball.java:207
com.google.appinventor.components.runtime.Ball.containsPoint
boolean containsPoint(double qx, double qy)
Definition: Ball.java:119
com.google.appinventor.components.runtime.Component.COLOR_DEFAULT
static final int COLOR_DEFAULT
Definition: Component.java:68
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.Ball.MoveTo
void MoveTo(double x, double y)
Definition: Ball.java:237
com.google.appinventor.components.runtime.Sprite.visible
boolean visible
Definition: Sprite.java:65
com.google.appinventor.components.runtime.util.PaintUtil.changePaint
static void changePaint(Paint paint, int argb)
Definition: PaintUtil.java:27
com.google.appinventor.components.runtime.Ball.onDraw
void onDraw(Canvas canvas)
Definition: Ball.java:75
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.PaintUtil
Definition: PaintUtil.java:17
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.runtime.Sprite.canvas
final Canvas canvas
Definition: Sprite.java:48
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.Sprite.originAtCenter
boolean originAtCenter
Definition: Sprite.java:72
com.google.appinventor.components.runtime.Ball.Height
void Height(int height)
Definition: Ball.java:94
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER
static final String PROPERTY_TYPE_NON_NEGATIVE_INTEGER
Definition: PropertyTypeConstants.java:206
com.google.appinventor.components.runtime.Ball.Radius
int Radius()
Definition: Ball.java:145
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.Sprite
Definition: Sprite.java:37
com.google.appinventor.components.runtime.Ball.Width
int Width()
Definition: Ball.java:104
com.google.appinventor.components.runtime.Sprite.yCenter
double yCenter
Definition: Sprite.java:74
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.Ball.HeightPercent
void HeightPercent(int pCent)
Definition: Ball.java:99
com.google.appinventor.components.runtime.Sprite.DEFAULT_ORIGIN_AT_CENTER
static final boolean DEFAULT_ORIGIN_AT_CENTER
Definition: Sprite.java:46
com.google.appinventor.components.runtime.Ball.PaintColor
int PaintColor()
Definition: Ball.java:158
com.google.appinventor.components.runtime.Sprite.yTop
double yTop
Definition: Sprite.java:67
com.google.appinventor.components.runtime.Ball.Width
void Width(int width)
Definition: Ball.java:109
com.google.appinventor.components.runtime.Ball.PaintColor
void PaintColor(int argb)
Definition: Ball.java:171
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.Sprite.form
Form form
Definition: Sprite.java:76
com.google.appinventor.components.runtime.Ball.OriginAtCenter
void OriginAtCenter(boolean b)
Definition: Ball.java:193
com.google.appinventor.components.runtime.Ball.Height
int Height()
Definition: Ball.java:89
com.google.appinventor.components.runtime.Ball.WidthPercent
void WidthPercent(int pCent)
Definition: Ball.java:114
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.Sprite.registerChange
void registerChange()
Definition: Sprite.java:728
com.google.appinventor.components.runtime.Component.COLOR_BLACK
static final int COLOR_BLACK
Definition: Component.java:55
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.Ball.Y
double Y()
Definition: Ball.java:221
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Ball
Definition: Ball.java:57
com.google.appinventor.components.runtime.Canvas
Definition: Canvas.java:132
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_COLOR
static final String PROPERTY_TYPE_COLOR
Definition: PropertyTypeConstants.java:63
com.google
com.google.appinventor.components.runtime.Sprite.xLeft
double xLeft
Definition: Sprite.java:66
com
com.google.appinventor.components.runtime.Sprite.xCenter
double xCenter
Definition: Sprite.java:73
com.google.appinventor.components.annotations.IsColor
Definition: IsColor.java:13
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_BLACK
static final String DEFAULT_VALUE_COLOR_BLACK
Definition: Component.java:71
com.google.appinventor