Wednesday, August 17, 2011

Custom Progress Bar in J2me



protected void paintSpinner(Graphics g, int xPostion, int yPosition, int widthOfProgressBar,
            int heightOfProgressBar) {
        int side = Math.min(widthOfProgressBar, heightOfProgressBar);
        int diameter = side / 6;
        int radius = diameter / 2;
        double toCenter = side / 3;
        int temp = (side / 2) - radius;
        int left = xPostion + temp;
        int top = yPosition+ temp;
        int horizontalAlignment = Graphics.HCENTER;
        if (horizontalAlignment == Graphics.HCENTER) {
            left += (widthOfProgressBar- side) / 2;
        } else if (horizontalAlignment == Graphics.RIGHT) {
            left += (widthOfProgressBar- side);
        }
                  int foreground = 0x0033FF; //starting color
                  int background = 0xCCCC66;// end color
             
        int redStart = (background & 0x00FF0000) >> 16;
        int greenStart = (background & 0x0000FF00) >> 8;
        int blueStart = (background & 0x000000FF);

        int redDelta = (foreground & 0x00FF0000) >> 16;
        int greenDelta = (foreground & 0x0000FF00) >> 8;
        int blueDelta = (foreground & 0x000000FF);

        redDelta = (redDelta - redStart) / 12;
        greenDelta = (greenDelta - greenStart) / 12;
        blueDelta = (blueDelta - blueStart) / 12;
        for (int hour = 1; hour <= 12; hour++) {
            int angle = ((hour - 3) * -30 + 360) % 360;
            double radians = Math.toRadians(angle);
            int cx = (int) (Math.cos(radians) * toCenter);
            int cy = (int) (Math.sin(radians) * toCenter) * -1;
            int offset = ((hour - spinnerHour) + 12) % 12;
            int red = redStart + (offset * redDelta);
            int green = greenStart + (offset * greenDelta);
            int blue = blueStart + (offset * blueDelta);
            g.setColor(red, green, blue);
            g.fillRoundRect(left + cx, top + cy, diameter, diameter, diameter,
                    diameter);
        }
    }

we have to call this method from paint method. it will display 12 circles in round shape with each color has different color...By using timers , we should call repaint method with some interval.then colors of each circles will be changed .. so it will look like that circles are rotated with 360 degress angle...

By making some simple changes in diameter of circles and colors..we can create lot of custom Progress bars...
We can change Colors by changing the color code in foreground and background values in above code...


OutPUT:







1 comment:

  1. tnx dude !!! gud work.. i hope u will continue ur blog with more good works...

    ReplyDelete