我一直在尝试在 Eclipse 中创建一个球。我试图找出在这篇文章中要做什么,但我似乎不知道在我的 Ball 课上放什么。有什么建议么?
问问题
2716 次
3 回答
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);
}
}
于 2012-10-17T21:19:27.667 回答
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);
}
}
于 2012-10-27T14:40:43.413 回答