The MIDP1.0 of the triangle in the

Source: Internet
Author: User
Tags integer

Due to the adaptation needs of development, J2ME programmers can not use MIDP2.0 for development in many cases. But MIDP1.0 's function is much different from 2.0, and many useful functions are not provided. This requires the programmer to implement it themselves. This paper introduces a useful function in the canvas, which is used to populate the Triangle region in the canvas.

Import Javax.microedition.lcdui.canvas;import Javax.microedition.lcdui.command;import Javax.microedition.lcdui.commandlistener;import Javax.microedition.lcdui.displayable;import Javax.microedition.lcdui.Graphics;

public class Triangle extends Canvas implements Commandlistener {private Graphics g; Public triangle () {}

protected void Paint (Graphics arg0) {g = arg0;        G.setcolor (0XFFFFFF);        G.fillrect (0,0,this.getwidth (), This.getheight ());        G.setcolor (0x000000);    Filltriangle (5,40,100,10,200,200); }

   /**     * Conditions: Point (X0,y0), (X1,y1), (x2,y2) constitute a triangle.      * Solution: Set straight L over Point (x1,y1), (X2,y2) two points      * (X3,Y3) is the point on the line L, from (X0,y0) draw line to (X3, Y3).      * As long as we pick up all the displayed pixel points above the line L, we can make the triangle fill.      *      * problem: Straight line L have two states, vertical and tilt (including horizontal), vertical state, we are easy to solve.      * Tilt State, we need to get the slope of the line, which seems to use floating-point numbers, but we can use integer solution.      * Because when the horizontal axis of a line (line) (X1,y1), (X2,y2) is x1!=x2,      * that is, the absolute value of X1-X2 is greater than or equal to 1), The difference y2-y1 of the      * ordinate is the highest of the screen. Therefore, the value of (Y1-Y2)/(X1-X2) is not greater than the height of the screen.      * This value differs a lot from the integer.max_value. So we can magnify this slope 1000 times times and then use it.      * It is possible to slope the accuracy of the requirement.      */    private void filltriangle (int x0, int y0, int x1, int y1, int x2, int y2) {& nbsp;       int temp,x3,y3;        if (y1>y2) { Guarantee Y2>y1, easy to calculate, this is just the exchange of two points of reference, does not affectResults.             temp=x1;             x1=x2;            x2=temp;             temp=y1;             y1=y2;            y2=temp;        }                    if (x1==x2) {                         x3=x1;             y3=y1+1;            do{                G.drawLine (x0,y0, x3,y3);                y3++;            }while (y3<y2);        }else{            if (y1==y2) {                 if (x1>x2) {                     temp=x1;                     x1 =x2;                     x2=temp;                     temp=y1;                     y1=y2;                     y2=temp;                                     }                 x3=x1;                Y3 =y1;                do{                     G.drawline (X0,Y0,X3,Y3);                     x3++;                }wHile (x3<x2);           }else{                 int k= (y1-y2) *1000/(X1-X2);//Slope, Magnified by 1000 times times                 System.out.println ("K is" +k);                 y3=y1+1;                x3= (y3-y1) *1000/k+x1;                 do{                     g.drawline (x0,y0,x3,y3);                     y3++;                     x3= (y3-y1) *1000/k+x1;                }while (y3<y2);           }                    }    }     public void commandaction (Command arg0, displayable arg1) {

}

}

This code does not perform a checksum of three points to make up a triangle. Since the mechanism of implementation is to draw lines from one point to the corresponding line, the endpoint of the line does not use the vertex of the obtuse angle, as this may result in a fill dissatisfaction. Of course, this can be solved within the function, but I haven't found a suitable method for a while.

The code has been tested. However, my tests may not be perfect and you can test them before you use them. The efficiency of the code do not worry, after testing, paint use about 0.016 seconds. Use the Loop 100 times to get the result.

This method of filling the triangle although the idea is correct, but there are a lot of deficiencies, I hope you can modify the good, and then the modified function to tell me. I also small "open source" a bit, hehe.

Msn:cuilichen@hotmail.com




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.