Infolinks In Text Ads

Jun 23, 2010

How to draw ruler in applet code

import java.io.PrintStream;

public class Ruler extends Button
{

    public Ruler()
    {
        mid1 = 77;
    }

    public void init()
    {
        setBackground(Color.GRAY);
    }

    public void paint(Graphics g)
    {
        drawNum(g, 10, 520, 12, mid1, 1);
        drawRuler(g, 10, 520, 8);
    }

    private void drawRuler(Graphics g, int left, int right, int level)
    {
        if(level < 1)
        {
            return;
        } else
        {
            int mid = (left + right) / 2;
            g.setColor(Color.BLUE);
            g.drawLine(80, mid, 80 - level * 3, mid);
            drawRuler(g, left, mid - 1, level - 1);
            drawRuler(g, mid + 1, right, level - 1);
            return;
        }
    }

    private void drawNum(Graphics g, int left, int right, int level, int mid1, int i)
    {
        if(level < 1)
            return;
        char m[] = new char[10];
        String c1 = (new Integer(i)).toString();
        char ch[] = c1.toCharArray();
        if(i < 4)
        {
            m[0] = ch[0];
            g.setColor(Color.GREEN);
            g.drawChars(m, 0, 1, 50, mid1);
            mid1--;
        } else
        if(i < 12)
        {
            m[0] = ch[0];
            g.setColor(Color.GREEN);
            g.drawChars(m, 0, 1, 50, mid1);
            mid1 -= 2;
        } else
        {
            m[0] = ch[0];
            m[1] = ch[1];
            g.setColor(Color.GREEN);
            g.drawChars(m, 0, 2, 50, mid1);
            System.out.println((new StringBuilder()).append("ch-------").append(ch).toString());
        }
        i++;
        drawNum(g, left, right, level - 1, mid1 + 66, i);
    }

    private static final int theSize = 511;
    int mid1;
}

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...