Java Draw Line awt drawLine(Graphics render, int row, String text)

Here you can find the source of drawLine(Graphics render, int row, String text)

Description

Draws a line on the screen at the specified index.

License

Open Source License

Parameter

Parameter Description
render The Graphics object to be used.
row The index where you want the text.
text The text you want to render. Colours can be set like [red].

Declaration

public static void drawLine(Graphics render, int row, String text) 

Method Source Code


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

import java.awt.*;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final String[] COLOURS_STR = new String[] { "red", "green", "cyan", "purple", "white" };
    private static final Map<String, Color> COLOR_MAP = new HashMap<String, Color>();

    /**// w  w  w .jav a  2  s  . c om
     * Draws a line on the screen at the specified index. Default is green.
     * <p/>
     * Available colours: red, green, cyan, purple, white.
     *
     * @param render The Graphics object to be used.
     * @param row    The index where you want the text.
     * @param text   The text you want to render. Colours can be set like [red].
     */
    public static void drawLine(Graphics render, int row, String text) {
        FontMetrics metrics = render.getFontMetrics();
        int height = metrics.getHeight() + 4; // height + gap
        int y = row * height + 15 + 19;
        String[] texts = text.split("\\[");
        int xIdx = 7;
        Color cur = Color.GREEN;
        for (String t : texts) {
            for (@SuppressWarnings("unused")
            String element : COLOURS_STR) {
                // String element = COLOURS_STR[i];
                // Don't search for a starting '[' cause it they don't exists.
                // we split on that.
                int endIdx = t.indexOf(']');
                if (endIdx != -1) {
                    String colorName = t.substring(0, endIdx);
                    if (COLOR_MAP.containsKey(colorName)) {
                        cur = COLOR_MAP.get(colorName);
                    } else {
                        try {
                            Field f = Color.class.getField(colorName);
                            int mods = f.getModifiers();
                            if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) {
                                cur = (Color) f.get(null);
                                COLOR_MAP.put(colorName, cur);
                            }
                        } catch (Exception ignored) {
                        }
                    }
                    t = t.replace(colorName + "]", "");
                }
            }
            render.setColor(Color.BLACK);
            render.drawString(t, xIdx, y + 1);
            render.setColor(cur);
            render.drawString(t, xIdx, y);
            xIdx += metrics.stringWidth(t);
        }
    }
}

Related

  1. drawLine(Graphics g, int x1, int y1, int x2, int y2)
  2. drawLine(Graphics g, int x1, int y1, int x2, int y2, boolean thick)
  3. drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  4. drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)