Java Graphics Draw drawIcon(Graphics g, String icon, int x, int y)

Here you can find the source of drawIcon(Graphics g, String icon, int x, int y)

Description

draw Icon

License

Open Source License

Declaration

public static void drawIcon(Graphics g, String icon, int x, int y) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

import java.awt.Font;
import java.awt.Graphics;

public class Main {
    public static void drawIcon(Graphics g, String icon, int x, int y) {
        if (g == null) {
            return;
        }/*from  w  w  w . j ava  2s.c om*/
        int length = icon.length();
        int x1, y1, x2, y2;
        int pc = 0;
        while (true) {
            char command = icon.charAt(pc++);
            if (pc >= length) {
                break;
            }
            switch (command) {
            case 'B':
                x += v(pc++, icon);
                y += v(pc++, icon);
                break; // reset base
            case 'R':
                g.drawRect(x + v(pc++, icon), y + v(pc++, icon), v(pc++, icon), v(pc++, icon));
                break; // rectangle
            case 'F':
                g.fillRect(x + v(pc++, icon), y + v(pc++, icon), v(pc++, icon), v(pc++, icon));
                break; // filled rectangle
            case 'O':
                g.drawOval(x + v(pc++, icon), y + v(pc++, icon), v(pc++, icon), v(pc++, icon));
                break; // oval
            case 'o':
                g.fillOval(x + v(pc++, icon), y + v(pc++, icon), v(pc++, icon), v(pc++, icon));
                break; // filled oval
            case 'C':
                g.setColor(new Color(v(pc++, icon) * 16, v(pc++, icon) * 16, v(pc++, icon) * 16));
                break; // set color
            case 'L':
                g.drawLine(x + v(pc++, icon), y + v(pc++, icon), x + v(pc++, icon), y + v(pc++, icon));
                break; // line
            case 'D':
                g.fillRect(x + v(pc++, icon), y + v(pc++, icon), 1, 1);
                break; // dot
            case 'P': // polyline
                x1 = x + v(pc++, icon);
                y1 = y + v(pc++, icon);
                while (true) {
                    x2 = v(pc++, icon);
                    if (x2 == 0) {
                        break;
                    }
                    y2 = v(pc++, icon);
                    if (y2 == 0) {
                        break;
                    }
                    x2 += x;
                    y2 += y;
                    g.drawLine(x1, y1, x2, y2);
                    x1 = x2;
                    y1 = y2;
                }
                break;
            case 'T': // text (one character)
                x2 = x + v(pc++, icon);
                y2 = y + v(pc++, icon);
                int size = v(pc++, icon) * 10 + v(pc++, icon);
                char[] c = new char[1];
                c[0] = pc < icon.length() ? icon.charAt(pc++) : 'e';
                g.setFont(new Font("SansSerif", Font.BOLD, size));
                g.drawString(new String(c), x2, y2);
                break;
            default:
                break;
            }
            if (pc >= length) {
                break;
            }
        }
    }

    private static int v(int pc, String icon) {
        if (pc >= icon.length()) {
            return 0;
        }
        char c = icon.charAt(pc);
        switch (c) {
        case '0':
            return 0;
        case '1':
            return 1;
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case '5':
            return 5;
        case '6':
            return 6;
        case '7':
            return 7;
        case '8':
            return 8;
        case '9':
            return 9;
        case 'a':
            return 10;
        case 'b':
            return 11;
        case 'c':
            return 12;
        case 'd':
            return 13;
        case 'e':
            return 14;
        case 'f':
            return 15;
        default:
            return 0;
        }
    }
}

Related

  1. drawGatter(Graphics g, int xDist, int yDist)
  2. drawGlow(Graphics2D g2, Area area, double width, Color color)
  3. drawGroove(Graphics g, int x, int y, int w, int h, Color shadow, Color highlight)
  4. drawGroove(Graphics g, int x, int y, int width, int height, Color shadow, Color highlight)
  5. drawHandles(final Graphics g, final int[] x, final int[] y)
  6. drawInnerButtonDecoration(Graphics g, int x, int y, int w, int h, Color baseColor)
  7. drawISPip(Graphics2D g2d, float width, float height)
  8. drawLightBeamVertical(Graphics g, Color c, int midx, int y1, int y2)
  9. drawLoweredBezel(Graphics g, int x, int y, int w, int h, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)