Example usage for java.awt Graphics setColor

List of usage examples for java.awt Graphics setColor

Introduction

In this page you can find the example usage for java.awt Graphics setColor.

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);//from  w ww .  j a v a  2  s. c o  m

    FontMetrics fm = g.getFontMetrics(font);

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

}

From source file:RedGreenBorder.java

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Insets insets = getBorderInsets(c);
    Color horizontalColor;//  w ww.  j  a  v a2s. c o m
    if (c.isEnabled()) {
        boolean pressed = false;
        if (c instanceof AbstractButton) {
            ButtonModel model = ((AbstractButton) c).getModel();
            pressed = model.isPressed();
        }
        if (pressed) {
            horizontalColor = Color.RED;
        } else {
            horizontalColor = Color.GREEN;
        }
    } else {
        horizontalColor = Color.LIGHT_GRAY;
    }
    g.setColor(horizontalColor);

    g.fillRect(0, 0, width, insets.top);
}

From source file:SortItem.java

/**
 * Paint the array of numbers as a list of horizontal lines of varying
 * lenghts.//from w  ww.  jav  a  2  s .  c o m
 */
public void paint(Graphics g) {
    int[] a = arr;
    int y = size().height - 1;

    // Erase old lines
    g.setColor(Color.lightGray);
    for (int i = a.length; --i >= 0; y -= 2) {
        g.drawLine(arr[i], y, size().width, y);
    }

    // Draw new lines
    g.setColor(Color.black);
    y = size().height - 1;
    for (int i = a.length; --i >= 0; y -= 2) {
        g.drawLine(0, y, arr[i], y);
    }

    if (h1 >= 0) {
        g.setColor(Color.red);
        y = h1 * 2 + 1;
        g.drawLine(0, y, size().width, y);
    }
    if (h2 >= 0) {
        g.setColor(Color.blue);
        y = h2 * 2 + 1;
        g.drawLine(0, y, size().width, y);
    }
}

From source file:org.deegree.ogcwebservices.wms.dataaccess.ID2PInterpolation.java

public BufferedImage perform(GetLegendGraphic glg) {

    BufferedImage bi = new BufferedImage(150, colorMap.size() * 25, BufferedImage.TYPE_4BYTE_ABGR);
    Iterator<double[]> iterator = colorMap.keySet().iterator();
    List<double[]> list = new ArrayList<double[]>(colorMap.size());

    while (iterator.hasNext()) {
        double[] ds = iterator.next();
        list.add(ds);/*from  w  w  w .j  av a  2 s  .  c om*/
    }

    for (int i = list.size() - 1; 0 <= i; i--) {
        for (int j = 0; j < i; j++) {
            if (list.get(j + 1)[0] < list.get(j)[0]) {
                double[] ds = list.get(j + 1);
                list.set(j + 1, list.get(j));
                list.set(j, ds);
            }
        }
    }

    int i = 0;
    Graphics g = bi.getGraphics();
    for (double[] ds : list) {
        Color color = colorMap.get(ds);
        g.setColor(color);
        g.fillRect(2, 2 + i * 25, 20, 20);
        g.setColor(Color.BLACK);
        g.drawRect(2, 2 + i * 25, 20, 20);
        g.drawString(Double.toString(ds[0]) + " - " + Double.toString(ds[1]), 25, 17 + i * 25);
        i++;
    }
    g.dispose();
    return bi;

}

From source file:FancyCaret.java

public void paint(Graphics g) {
    JTextComponent comp = getComponent();
    if (comp == null)
        return;//from   w w  w .j  a v  a 2  s  .  c o  m

    int dot = getDot();
    Rectangle r = null;
    char dotChar;
    try {
        r = comp.modelToView(dot);
        if (r == null)
            return;
        dotChar = comp.getText(dot, 1).charAt(0);
    } catch (BadLocationException e) {
        return;
    }

    if ((x != r.x) || (y != r.y)) {
        repaint();
        x = r.x;
        y = r.y;
        height = r.height;
    }

    g.setColor(comp.getCaretColor());
    g.setXORMode(comp.getBackground());

    if (dotChar == '\n') {
        int diam = r.height;
        if (isVisible())
            g.fillArc(r.x - diam / 2, r.y, diam, diam, 270, 180); // half circle
        width = diam / 2 + 2;
        return;
    }

    if (dotChar == '\t')
        try {
            Rectangle nextr = comp.modelToView(dot + 1);
            if ((r.y == nextr.y) && (r.x < nextr.x)) {
                width = nextr.x - r.x;
                if (isVisible())
                    g.fillRoundRect(r.x, r.y, width, r.height, 12, 12);
                return;
            } else
                dotChar = ' ';
        } catch (BadLocationException e) {
            dotChar = ' ';
        }

    width = g.getFontMetrics().charWidth(dotChar);
    if (isVisible())
        g.fillRect(r.x, r.y, width, r.height);
}

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

/**
 * Convert a html code to an image/*from  www . ja v  a 2s  .com*/
 * 
 * @param html html to convert
 * @return html converted to png
 * @throws IOException if error
 * @throws PPTGeneratorException 
 */
protected byte[] htmlToImage(String html) throws IOException, PPTGeneratorException {
    try {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);
        editor.setSize(editor.getPreferredSize());
        editor.addNotify();
        LOGGER.debug("Panel is built");
        BufferedImage bufferSave = new BufferedImage(editor.getPreferredSize().width,
                editor.getPreferredSize().height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics g = bufferSave.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, editor.getPreferredSize().width, editor.getPreferredSize().height);
        editor.paint(g);
        LOGGER.debug("graphics is drawn");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferSave, "png", out);
        return out.toByteArray();
    } catch (HeadlessException e) {
        LOGGER.error("X Server no initialized or -Djava.awt.headless=true not set !");
        throw new PPTGeneratorException("X Server no initialized or -Djava.awt.headless=true not set !");
    }
}

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

/**
 * Add an image with a html code without change its dimension
 * /*  ww w. j  a v a2 s.c o  m*/
 * @param slideToSet slide to set
 * @param html html code
 * @param x horizontal position
 * @param y vertical position
 * @throws IOException if error
 * @throws PPTGeneratorException 
 */
protected void addHtmlPicture(Slide slideToSet, String html, int x, int y)
        throws IOException, PPTGeneratorException {
    try {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        if (!ge.isHeadlessInstance()) {
            LOGGER.warn("Runtime is not configured for supporting graphiv manipulation !");
        }
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);
        LOGGER.debug("Editor pane is built");
        editor.setSize(editor.getPreferredSize());
        editor.addNotify(); // Serveur X requis
        LOGGER.debug("Panel rendering is done");
        BufferedImage bufferSave = new BufferedImage(editor.getPreferredSize().width,
                editor.getPreferredSize().height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics g = bufferSave.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, editor.getPreferredSize().width, editor.getPreferredSize().height);
        editor.paint(g);
        LOGGER.debug("graphics is drawn");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferSave, "png", out);
        LOGGER.debug("image is written");
        addPicture(slideToSet, out.toByteArray(),
                new Rectangle(x, y, editor.getPreferredSize().width, editor.getPreferredSize().height));
        LOGGER.debug("image is added");
    } catch (HeadlessException e) {
        LOGGER.error("X Server no initialized or -Djava.awt.headless=true not set !");
        throw new PPTGeneratorException("X Server no initialized or -Djava.awt.headless=true not set !");
    }
}

From source file:Main.java

private void paintArrow(Graphics g, int x, int y, Color highlight) {
    int mid, i, j;

    Color oldColor = g.getColor();
    boolean isEnabled = isEnabled();

    j = 0;/*from w ww .  j av  a2s  .  c  o  m*/
    arrowSize = Math.max(arrowSize, 2);
    mid = (arrowSize / 2) - 1;

    g.translate(x, y);

    switch (direction) {
    case NORTH:
        for (i = 0; i < arrowSize; i++) {
            g.drawLine(mid - i, i, mid + i, i);
        }
        if (!isEnabled) {
            g.setColor(highlight);
            g.drawLine(mid - i + 2, i, mid + i, i);
        }
        break;
    case SOUTH:
        if (!isEnabled) {
            g.translate(1, 1);
            g.setColor(highlight);
            for (i = arrowSize - 1; i >= 0; i--) {
                g.drawLine(mid - i, j, mid + i, j);
                j++;
            }
            g.translate(-1, -1);
            g.setColor(oldColor);
        }
        j = 0;
        for (i = arrowSize - 1; i >= 0; i--) {
            g.drawLine(mid - i, j, mid + i, j);
            j++;
        }
        break;
    case WEST:
        for (i = 0; i < arrowSize; i++) {
            g.drawLine(i, mid - i, i, mid + i);
        }
        if (!isEnabled) {
            g.setColor(highlight);
            g.drawLine(i, mid - i + 2, i, mid + i);
        }
        break;
    case EAST:
        if (!isEnabled) {
            g.translate(1, 1);
            g.setColor(highlight);
            for (i = arrowSize - 1; i >= 0; i--) {
                g.drawLine(j, mid - i, j, mid + i);
                j++;
            }
            g.translate(-1, -1);
            g.setColor(oldColor);
        }
        j = 0;
        for (i = arrowSize - 1; i >= 0; i--) {
            g.drawLine(j, mid - i, j, mid + i);
            j++;
        }
        break;
    }

    g.translate(-x, -y);
    g.setColor(oldColor);
}

From source file:PlatformTest.java

private Shape3D createLabel(String szText, float x, float y, float z) {
    BufferedImage bufferedImage = new BufferedImage(25, 14, BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.getGraphics();
    g.setColor(Color.white);
    g.drawString(szText, 2, 12);/* ww w  .ja  v a  2  s  .c o m*/

    ImageComponent2D imageComponent2D = new ImageComponent2D(ImageComponent2D.FORMAT_RGB, bufferedImage);

    // create the Raster for the image
    javax.media.j3d.Raster renderRaster = new javax.media.j3d.Raster(new Point3f(x, y, z),
            javax.media.j3d.Raster.RASTER_COLOR, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(),
            imageComponent2D, null);

    return new Shape3D(renderRaster);
}

From source file:com.fengduo.bee.commons.servlet.ValidateCodeServlet.java

private String createCharacter(Graphics g) {
    char[] codeSeq = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
    String[] fontTypes = { "Arial", "Arial Black", "AvantGarde Bk BT", "Calibri" };
    Random random = new Random();
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < 4; i++) {
        String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);// random.nextInt(10));
        g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
        g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)], Font.BOLD, 26));
        g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
        // g.drawString(r, i*w/4, h-5);
        s.append(r);/*from w  w w.  ja v  a 2 s . co m*/
    }
    return s.toString();
}