Java Draw Image drawVerticalBar(final BufferedImage image, final double max, final double min)

Here you can find the source of drawVerticalBar(final BufferedImage image, final double max, final double min)

Description

draw Vertical Bar

License

Open Source License

Declaration

private static void drawVerticalBar(final BufferedImage image, final double max, final double min) 

Method Source Code


//package com.java2s;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    private static final Color COLOR_TEXT = Color.BLACK;
    private static final int BAR_SIZE_VERT = 30;
    private static final NumberFormat nf = new DecimalFormat("0.0#");
    private static final double COLOR_CENTER = 75 / 360.0;
    private static final double COLOR_GAP = 5 / 360.0;
    private static final double COLOR_RANGE_POS = 160 / 360.0;
    private static final double COLOR_RANGE_NEG = 75 / 360.0;
    private static final int LIMIT_HEIGHT = 180;
    private static final int COLOR_NaN = Color.MAGENTA.getRGB();
    private static boolean debugMode;

    private static void drawVerticalBar(final BufferedImage image, final double max, final double min) {
        final int height = image.getHeight();

        final Graphics2D g = image.createGraphics();
        final FontMetrics metrics = g.getFontMetrics(g.getFont());

        final int x = image.getWidth() - 1 - BAR_SIZE_VERT;
        final double middle = (max + min) / 2.0;
        final double quarter = (max - middle) / 2.0;

        final int width = image.getWidth();
        // positive part           
        for (int y = 0; y < height; y++) {
            g.setColor(new Color(deformationToRGB(height - 1 - y, 0, height - 1)));
            g.drawRect(x, y, BAR_SIZE_VERT, 1);
        }/*from   w  w w . j  a  va  2 s  .c o m*/

        g.setColor(COLOR_TEXT);
        if (height > LIMIT_HEIGHT) {
            g.drawString(nf.format(middle - quarter), width - BAR_SIZE_VERT, height / 4 * 3);
            g.drawString(nf.format(middle + quarter), width - BAR_SIZE_VERT, height / 4);
        }
        g.drawString(nf.format(middle), width - BAR_SIZE_VERT, height / 2 + metrics.getHeight() / 3);
        g.drawString(nf.format(max), width - BAR_SIZE_VERT, metrics.getHeight() / 3 * 2);
        g.drawString(nf.format(min), width - BAR_SIZE_VERT, height - 2);

        g.dispose();
    }

    private static int deformationToRGB(final double val, final double min, final double max) {
        final int result;
        if (Double.isNaN(val)) {
            if (debugMode) {
                result = COLOR_NaN;
            } else {
                result = Color.HSBtoRGB(0, 1, 0);
            }
        } else {
            float h, s = 1, b = 1;
            double fract;
            final double midlle = (max + min) / 2.0;
            final double half = max - midlle;
            if (val >= min && val <= max) {
                if (val < midlle) {
                    fract = -(val - midlle) / half;
                    h = (float) (fract * COLOR_RANGE_POS + COLOR_CENTER + COLOR_GAP);
                } else {
                    fract = (val - midlle) / half;
                    h = (float) (COLOR_CENTER - COLOR_GAP - (fract * COLOR_RANGE_NEG));
                }
            } else {
                h = 1;
                s = 0;
                b = 0.5f;
            }

            result = Color.HSBtoRGB(h, s, b);
        }
        return result;
    }
}

Related

  1. drawScaledImage(Image image, Component canvas, Graphics g)
  2. drawSquare(BufferedImage image, int x, int y, int size, int pixel)
  3. drawStretchedImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer)
  4. drawTiledImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight, int tilePadding)
  5. drawTransparentImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink, double transparency)
  6. drawVerticalLineAt(int x, int dottedLine, Color color, BufferedImage displayMap)
  7. drawVolatileImage(Graphics2D g, VolatileImage volatileImage, int x, int y, Image orig)