我一直在尝试在Eclipse中创建一个球。我试图找出在this post做什么,我似乎无法弄清楚在Ball课程中要放什么。有什么建议吗?
答案 0 :(得分:1)
这是一个简单的java类,您可以从中创建一个球对象:
public class Ball {
private int x, y, r;
private Color c = Color.YELLOW;
public Ball (int x, int y, int r)
{
this.x = x;
this.y = y;
this.r = r;
}
// draws the ball
public void draw (Graphics g)
{
g.setColor(c);
g.fillOval(x-r, y-r, 2*r, 2*r);
}
}
答案 1 :(得分:1)
如果您决定改变主意,请尝试此操作。
public class MainActivity extends Activity {
private int c = Color.YELLOW;
private Canvas g;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
draw();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// draws the ball
public void draw ()
{
Display display = getWindowManager().getDefaultDisplay();
int width = display.getHeight();
int height = display.getWidth();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
g = new Canvas(bitmap);
g.drawColor(c);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
g.drawCircle(50, 10, 25, paint); //Put your values
// In order to display this image, we need to create a new ImageView that we can display.
ImageView imageView = new ImageView(this);
// Set this ImageView's bitmap to ours
imageView.setImageBitmap(bitmap);
// Create a simple layout and add imageview to it.
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(imageView, params);
layout.setBackgroundColor(Color.BLACK);
// Show layout in activity.
setContentView(layout);
}
}
答案 2 :(得分:0)