Need help with novice paint program

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Need help with novice paint program

Postby Jadis » Fri Nov 06, 2009 4:59 am

Hey guys, I followed some different tutorials and "made" (pieced together) a paint program with a menu. In my menu, I have a 'Clear Screen' item and a 'Quit' item. Everything works great except for the Clear Screen.

Can someone take a look at my code and tell me what I need to do? Specifically, I don't know what I need to put at the "case R.id.clear_board: break; " part. I've been reading for like an hour and just can't seem to grasp what I need to do. I tried removing views, but can't seem to get it right. I'm an extreme novice at Java and a book on it is in the mail to me :) Can anyone help me clear the screen! :D

DrawCircle.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.mpruitt105.drawcircle;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.Menu;
  10.  
  11. import android.view.MenuInflater;
  12.  
  13. import android.view.MenuItem;
  14.  
  15. import android.view.MotionEvent;
  16.  
  17. import android.view.View;
  18.  
  19. import android.widget.FrameLayout;
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. public class DrawCircle extends Activity {
  28.  
  29.        
  30.  
  31.         int counter = 0;
  32.  
  33.  
  34.  
  35.     @Override
  36.  
  37.     public void onCreate(Bundle savedInstanceState) {
  38.  
  39.         super.onCreate(savedInstanceState);
  40.  
  41.         setContentView(R.layout.main);
  42.  
  43.        
  44.  
  45.         FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
  46.  
  47.         if (counter>0) {
  48.  
  49.                 main.addView(new Ball(this,50,50,25));
  50.  
  51.         } counter++;
  52.  
  53.        
  54.  
  55.         main.setOnTouchListener(new View.OnTouchListener() {
  56.  
  57.             public boolean onTouch(View v, MotionEvent e) {
  58.  
  59.                 float x = e.getX();
  60.  
  61.                     float y = e.getY();
  62.  
  63.                 FrameLayout flView = (FrameLayout) v;
  64.  
  65.                     flView.addView(new Ball(getParent(), x,y,25));
  66.  
  67.                     return false;
  68.  
  69.             }
  70.  
  71.         });
  72.  
  73.        
  74.  
  75.     }
  76.  
  77.    
  78.  
  79.     @Override
  80.  
  81.     public boolean onCreateOptionsMenu(Menu menu) {
  82.  
  83.         MenuInflater inflater = getMenuInflater();
  84.  
  85.         inflater.inflate(R.layout.menu, menu);
  86.  
  87.         return true;
  88.  
  89.     }
  90.  
  91.    
  92.  
  93.     @Override
  94.  
  95.     public boolean onOptionsItemSelected(MenuItem item) {
  96.  
  97.         switch (item.getItemId()) {
  98.  
  99.             case R.id.clear_board:  
  100.  
  101.                
  102.  
  103.                 break;
  104.  
  105.             case R.id.quit_program:    
  106.  
  107.                 finish();
  108.  
  109.                 break;
  110.  
  111.         }
  112.  
  113.         return true;
  114.  
  115.     }
  116.  
  117.    
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Ball.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.mpruitt105.drawcircle;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.graphics.Canvas;
  8.  
  9. import android.graphics.Paint;
  10.  
  11. import android.view.View;
  12.  
  13.  
  14.  
  15. public class Ball extends View {
  16.  
  17.  
  18.  
  19.     private final float x;
  20.  
  21.     private final float y;
  22.  
  23.     private final int r;
  24.  
  25.     private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  26.  
  27.    
  28.  
  29.  
  30.  
  31.     public Ball(Context context, float x, float y, int r) {
  32.  
  33.         super(context);
  34.  
  35.         mPaint.setColor(0xFFFF0000);
  36.  
  37.         this.x = x;
  38.  
  39.         this.y = y;
  40.  
  41.         this.r = r;
  42.  
  43.     }
  44.  
  45.  
  46.  
  47.     @Override
  48.  
  49.     protected void onDraw(Canvas canvas) {
  50.  
  51.         super.onDraw(canvas);
  52.  
  53.         canvas.drawCircle(x, y, r, mPaint);
  54.  
  55. }
  56.  
  57. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Jadis
Freshman
Freshman
 
Posts: 7
Joined: Thu Oct 29, 2009 9:11 pm

Top

Postby Jadis » Fri Nov 06, 2009 7:10 pm

Anyone have any suggestions? I'm thinking that maybe I should just redo the code using GraphicsActivity and use drawing objects / canvas instead of creating a new view for each circle... Bleh, I really, really wish Eclipse worked on my computer at work :evil:
Jadis
Freshman
Freshman
 
Posts: 7
Joined: Thu Oct 29, 2009 9:11 pm

Postby chaoz1336 » Mon Nov 23, 2009 11:52 am

still got the prob? ^^
I wanted to code a game too,
now I do painting like this:

-create own Control (myView)
-overwrite "onDraw"
-use canvas.drawBitmap and so on
-notify the drawer from the mainThread using an own handler wich overwrites handleMessage()
-redraw using the handler to call invalidate on myView


...greetz
chaoz
chaoz1336
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Wed Dec 10, 2008 5:19 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests