Sudoku Design (4)

Source: Internet
Author: User
Tags getcolor

After the first two days of setup, we can again view class, the game layout, and the operation.

Package Com.mars.demo;import Android.content.context;import Android.graphics.canvas;import android.graphics.Color; Import Android.graphics.paint;import Android.graphics.paint.fontmetrics;import Android.util.log;import Android.view.motionevent;import Android.view.view;public class MyView extends view{//defines the width and height of the cell and the public float width; public float height;private int selectx;private int selecty;public Game game = new Game ();p ublic MyView (context context) {s Uper (context);//TODO auto-generated constructor stub} @Overrideprotected void Onsizechanged (int w, int h, int oldw, int ol DH) {//TODO auto-generated Method stubthis.width = W/9f;this.height = h/9f;super.onsizechanged (W, H, OLDW, OLDH);} @Overrideprotected void OnDraw (canvas canvas) {//Defines the Brush object--draws the background paint paint = new paint ();//sets the color of the brush Paint.setcolor ( Getresources (). GetColor (R.color.shudu_background))//Draw phone screen background color canvas.drawrect (0, 0, getwidth (), getheight (), Paint );//define the Brush object and draw the grid line paint Darkpaint = new paint ();d Arkpaint.setcolor (Getresources (). GetColor (R.color.shudu_dark)); Paint Hilitepaint = new paint (); Hilitepaint.setcolor (Getresources (). GetColor (R.color.shudu_hilite)); Paint Lightpaint = new paint (); Lightpaint.setcolor (Getresources (). GetColor (R.color.shudu_light));//Draw nine lattice for (int i = 0 ; i<9; i++) {//Draw horizontal canvas.drawline (0, I*height, getwidth (), I*height, Lightpaint); Canvas.drawline (0, I*height+1, GetWidth (), I*height+1, Hilitepaint);//Draw the longitudinal line canvas.drawline (i*width, 0, I*width, getheight (), lightpaint); Canvas.drawline (i* width+1, 0, I*width+1, getheight (), hilitepaint);} This for loop is drawn with four striking lines for (int i = 0; i<9; i++) {if (i% 3! = 0) {continue;} Draw horizontal Canvas.drawline (0, I*height, getwidth (), I*height, Darkpaint); Canvas.drawline (0, I*height+1, getwidth (), i* Height+1, Hilitepaint);//Draw the longitudinal line canvas.drawline (i*width, 0, I*width, getheight (), darkpaint); Canvas.drawline (i*width+1 , 0, I*width+1, getheight (), hilitepaint);} Draw the digital paint numberpaint = new paint ();//Set the text of a series of properties Numberpaint.setcolor (Color.Black); Numberpaint.setstyle ( Paint.Style.STROKE); numberpaint.settextsize (height*0.75f); numberpaint.settextalign (Paint.Align.CENTER); FontMetrics FontMetrics = Numberpaint.getfontmetrics (); float x = width/2;float y = height/2-(fontmetrics.ascent+fontmet rics.descent)/2;for (int i=0; i<9; i++) for (int j=0; j<9; J + +) {Canvas.drawtext (Game.gettitlestring (i, J), I*width + X, J*height+y, numberpaint);} Super.ondraw (canvas);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubif (event.getaction ()! = Motionevent.action_down) {return super.ontouchevent (event);} Selectx = (int) (EVENT.GETX ()/width), selecty = (int) (Event.gety ()/height), int[] used = Game.getusedtile (Selectx, SELECTY); StringBuffer sb = new StringBuffer (); for (int i = 0; i<used.length; i++) {sb.append (used[i]);} LOG.I ("TAG", sb.tostring ()); Keydialog Keydialog = new Keydialog (This.getcontext (), used, this), Keydialog.show (),/*//defines a Layoutinflate object, Reprint View File Layoutinflater Layoutinflater = Layoutinflater.from (This.getcontext ()); View view = LayouTinflater.inflate (R.layout.dialog, NULL); TextView TextView = (TextView) View.findviewbyid (R.id.usedtextid); Textview.settext (sb.tostring ());//Create dialog box, Eject Viewalertdialog.builder Builder = new Alertdialog.builder (This.getcontext ()); Builder.setview (view); Alertdialog dialog = Builder.create ();d ialog.show (); */return true;} The view class accepts data passed by Keydialog, invokes the business logic game class, Processes public void setselecttile (int tile) {log.i ("TAG", Game.settileifvalid ( Selectx, Selecty, Tile) + "" +tile), if (Game.settileifvalid (Selectx,selecty,tile)) {invalidate ();//Redraw View Object}}}

Call the MyView object in the main class.

Package Com.mars.demo;import Android.os.bundle;import Android.app.activity;public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//setcontentview ( R.layout.activity_main); Setcontentview (new MyView (This));}}

Through the above Sudoku game, I learned about the paint class, the canvas class. I have learned some basic knowledge of drawing graphics and have further consolidated some of the basics of Android. From this, I found that as a programmer, must constantly go to knock code, the courage to practice, the program knocked out. Many times, we see others write procedures, the sense of logic are very clear, the idea is not disordered, but, really when you go to practice, you will find that, in fact, is not so easy. When you have a problem, you have to try to debug, so that we can continue to grow.

Programmers have grown up in debugging bugs and debugging errors. This is a word I very much agree with. Here, give yourself a word of encouragement, hope that in the future study days, not afraid of difficulties, used to try.

Sudoku Design (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.