Black Screen When Using Canvas - java

I am trying to make a match 3 game. For starters, I wanted to draw something on the screen, but no matter what I try, I get just a black screen.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//MAKE FULLSCREEN
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
GameView gameview = new GameView(this);
setContentView(gameview);
}
}
public class GameView extends SurfaceView{
private int numCol, numRow;
private int bWid, bHeight;
private Bitmap purpleBit;
private Bitmap rainbowBitmp;
private Bitmap redBitmp;
public GameView(Context context){
super(context);
}
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.rgb(250, 0, 0));
canvas.drawRect(100, 100, 200, 200, paint);
}
}

Set the paint width, color, stroke
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//MAKE FULLSCREEN
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
GameView gameview = new GameView(this);
setContentView(gameview);
}
}
public class GameView extends SurfaceView{
private int numCol, numRow;
private int bWid, bHeight;
private Bitmap purpleBit;
private Bitmap rainbowBitmp;
private Bitmap redBitmp;
public GameView(Context context){
super(context);
}
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint mPaint = new Paint();
//this is missing
mPaint.setColor(Color.rgb(250, 0, 0));
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(10);
canvas.drawRect(100, 100, 200, 200, mPaint);
}
}

Related

Tell me the solution to the simple grammatical errors of Android Studios

public void onClick (View v) {
There is a simple grammatical error in this area and I don't know how to solve it.
i don't know sorry
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView view = new MyView();
public void onClick (View v) {
switch (view.getId()) {
case R.id.btn :
view.isDraw = true;
break;
}
}
}
MyView class
public class MyView extends View {
public boolean isDraw = false;
public MyView() {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
Random random = new Random();
int x = random.nextInt(500);
int y = random.nextInt(800);
int r = (random.nextInt(3)+1) * 100;
#Override
protected void onDraw(Canvas canvas) {
isDraw = false;
super.onDraw(canvas);
canvas.drawColor(Color.LTGRAY);
Paint Pnt = new Paint();
Pnt.setColor(Color.BLUE);
if(isDraw == true) {
canvas.drawCircle(x,y,r,Pnt);
}
}
}
I want to draw a circle on the coordinates of the random value when I press the button.
You can't have a void on your onCreate. Try to put it outside.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView view = new MyView();
}
public void onClick (View v) {
switch (view.getId()) {
case R.id.btn :
view.isDraw = true;
break;
}
}
I want to draw a circle on the coordinates of the random value when I press the button.
If you want to do that, first, you need to find your button using findViewById an example coulld be :
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = findViewById(R.id.IdfromActivity_main);
}
Then if you want to make an onClickListener there are multiple ways to do this.
In your code an example could be :
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = findViewById(R.id.IdfromActivity_main);
MyView view = new MyView();
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button clicked!",
Toast.LENGTH_LONG).show();
//Add whatever action you need
}
});
}
And then you have also an error, that you have a method inside another method, let's say you have inside the onCreate method onClick method, so you should split them and put it outside of this onCreate method.

How to update data to an object created from a custom view class ,so that it can be drawn on custom view?

My application requires to post data to a custom view and redraw the view according to sent data from MainActivity.
Here are sample code.
Custom view->
public class custom_view extends View {
private simple_line line;
public custom_view(Context context) {
super(context);
line=new simple_line(0);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
line.draw_line(canvas);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(),getMeasuredHeight());
}
private class simple_line{
private float x_val;
private Paint mpaint;
public simple_line(float val){
x_val=val;
mpaint=new Paint();
mpaint.setColor(Color.RED);
mpaint.setStyle(Paint.Style.STROKE);
mpaint.setStrokeWidth(10);
}
public float getX_val() {
return x_val;
}
public void setX_val(float x_val) {
this.x_val = x_val;
invalidate();
}
public void draw_line(Canvas canvas){
Path mpath=new Path();
mpath.moveTo(getWidth()/2+x_val,0);
mpath.lineTo(getWidth()/2+x_val,getHeight());
mpath.close();
canvas.drawPath(mpath,mpaint);
}
}
public void update_xval(float val){
line.setX_val(val);
}
}
Here is Main activity->
public class MainActivity extends AppCompatActivity{
private Button button;
private custom_view myview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.mybutton);
myview=(custom_view)findViewById(R.id.myview);
myview=new custom_view(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myview.update_xval(100);
}
});
}
}
I am unable to update x_val when button is pressed.Where am I going wrong.What needs to be done to update x_val?
there is an issue in draw_line(Canvas canvas) function. you are not calling this function in custom_view. you need to invalidate() after draw_line(Canvas canvas)called.

Android - Changing colours in override methods

I'm making a drawing app. I have two brushes (img_brush and img_brush2). The initial colour is set to black in PaintView() (on the line 'this.colour = Color.BLACK;'). What I want to do is set different colours for both brushes in the override '(onClick) methods, for example blue for img_brush and red for img_brush2, how can I do it? This is the code I have got:
public class Draw_Activity extends Activity {
private PaintView pv;
protected View layout;
protected int progress;
protected Dialog dialog;
protected Dialog textdialog;
private String[] arrImagesStrings;
protected float stroke = 6;
int postion;
private String fileName;
private int ColorAh = Color.BLACK;
String Foldername;
RelativeLayout re;
private AdView mAdView;
ImageView img_brush, img_brush2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drawpic_activity);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
re = (RelativeLayout) findViewById(R.id.rell);
img_brush = (ImageView) findViewById(R.id.image_brush);
img_brush2 = (ImageView) findViewById(R.id.image_brush2);
this.fileName = null;
super.onCreate(savedInstanceState);
this.pv = new PaintView(this);
re.addView(pv);
//setContentView(this.pv);
this.pv.togglePencil(true);
Intent i = getIntent();
Foldername = i.getStringExtra("Folder");
img_brush.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Draw_Activity.this.pv.togglePencil(true);
}
});
img_brush2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Draw_Activity.this.pv.togglePencil(true);
}
});
}
//for color
private void showColorPickerDialogDemo() {
int initialColor = Color.WHITE;
ColorPickerDialog colorPickerDialog = new ColorPickerDialog(Draw_Activity.this, initialColor, new OnColorSelectedListener() {
public void onColorSelected(int color) {
Draw_Activity.this.pv.setColor(color);
}
});
colorPickerDialog.show();
}
private PaintView(Context c) {
super(c);
setDrawingCacheEnabled(true); // to save images
this.context = c;
this.colour = Color.BLACK;
this.path = new Path();
this.bmpPaint = new Paint();
this.paint = new Paint();
this.paint.setAntiAlias(true);
this.paint.setDither(true);
this.paint.setColor(this.colour);
this.paint.setStyle(Paint.Style.STROKE);
this.paint.setStrokeJoin(Paint.Join.ROUND);
this.paint.setStrokeCap(Paint.Cap.ROUND);
this.paint.setStrokeWidth(50);
this._paintBlur = new Paint();
this._paintBlur.set(paint);
this._paintBlur.setAntiAlias(true);
this._paintBlur.setDither(true);
this._paintBlur.setStyle(Paint.Style.STROKE);
this._paintBlur.setStrokeJoin(Paint.Join.ROUND);
this._paintBlur.setStrokeCap(Paint.Cap.ROUND);
this._paintBlur.setColor(Color.parseColor("#FA2B2B"));
this._paintBlur.setStrokeWidth(6.0F);
this._paintBlur.setMaskFilter(new BlurMaskFilter(10.0F, BlurMaskFilter.Blur.OUTER));
this.bitmapforground = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
this.bitmapforground = BitmapFactory.decodeResource(getResources(),
R.color.textbg);
}
public void setColor(int c) {
this.paint.setColor(c);
this.colour = c;
}
protected int getColor() {
return this.colour;
}
}

lock canvas fail and canvas is null when useing SurfaceView

The question of Android SurfaceView.
I want to draw a Circle in SurfaceView ,but canvas is null in SimpleDraw method.Why?
No 1 ,canvas is null.
No 2,SimpleDraw method run in ClickListener,is right , canvas is not null,why?
1.public class SurfaceTestActivity extends Activity {
SurfaceView sfv;
SurfaceHolder sfh;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sfv=(SurfaceView)findViewById(R.id.surface);
sfh=sfv.getHolder();
SimpleDraw();
}
void SimpleDraw(){
Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
getWindowManager().getDefaultDisplay().getHeight()));
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(2);
canvas.drawCircle(150, 150, 80, mPaint);
sfh.unlockCanvasAndPost(canvas);
}
}
2.public class SurfaceTestActivity extends Activity {
Button simpleButton;
SurfaceView sfv;
SurfaceHolder sfh;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simpleButton=(Button)findViewById(R.id.simple);
sfv=(SurfaceView)findViewById(R.id.surface);
sfh=sfv.getHolder();
simpleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//SimpleDraw();
}
});
}
void SimpleDraw(){
Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
getWindowManager().getDefaultDisplay().getHeight()));
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(2);
canvas.drawCircle(150, 150, 80, mPaint);
sfh.unlockCanvasAndPost(canvas);
}
}
The difference is timing.
No. 1 Tries to draw to the canvas before it is created and fails. The call to sfh.lockCanvas(...) fails and returns null, not the Canvas.
No. 2 Tries to draw after the canvas has been created, so it works.
Do this:
public class SurfaceTestActivity extends Activity implements SurfaceHolder.Callback {
SurfaceView sfv;
SurfaceHolder sfh;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sfv=(SurfaceView)findViewById(R.id.surface);
sfh=sfv.getHolder();
sfh.addCallback(this);
}
void SimpleDraw(){
Canvas canvas = sfh.lockCanvas(new Rect(0, 0, 300,
getWindowManager().getDefaultDisplay().getHeight()));
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(2);
canvas.drawCircle(150, 150, 80, mPaint);
sfh.unlockCanvasAndPost(canvas);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// draw AFTER surface created!
simpleDraw();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
CHANGES:
adding SurfaceHolder.Callback
adding sfh.addCallback(this);
adding 3 #Override methods with new SimpleDraw -- after the canvas is created.

How to fit a background image to the screen of an Android device

I am using Eclipse to write an Android app. I want my app to display a background image which is stretched to the size of the screen.
I have written the following code, but in the emulator it immediately exited the app when I ran it. Could someone please help me to understand the problem...
Here is my code...
public class Roller extends Activity {
Display display = getWindowManager().getDefaultDisplay();
int dwidth = display.getWidth();
int dheight = display.getHeight();
Bitmap background1 = BitmapFactory.decodeResource(getResources(),R.drawable.sunnybackground);
Bitmap BSunny = Bitmap.createScaledBitmap(background1,dwidth,dheight,true);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
class Panel extends View {
public Panel(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(BSunny, 0, 0, null);
}
}
}
Why not just something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ImageView iv = new ImageView(this, null);
iv.setBackgroundResource(R.drawable.sunnybackground);
setContentView(iv);
}
Why don't you set the background image in the layout xml file?
Do you have to set it programmatically at runtime?
I think you do as below
(It seems that you can't get display info unless activity is created)
public class Roller extends Activity {
Bitmap BSunny;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Display display = getWindowManager().getDefaultDisplay();
int dwidth = display.getWidth();
int dheight = display.getHeight();
Bitmap background1 = BitmapFactory.decodeResource(getResources(),R.drawable.sunnybackground);
BSunny = Bitmap.createScaledBitmap(background1,dwidth,dheight,true);
setContentView(new Panel(this));
}
class Panel extends View {
public Panel(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(BSunny, 0, 0, null);
}
}
}

Categories