Example usage for java.awt Graphics setFont

List of usage examples for java.awt Graphics setFont

Introduction

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

Prototype

public abstract void setFont(Font font);

Source Link

Document

Sets this graphics context's font to the specified font.

Usage

From source file:DialogSeparator.java

public void paint(Graphics g) {
    g.setColor(getBackground());// w ww  .  j  a  v  a  2 s .  com
    g.fillRect(0, 0, getWidth(), getHeight());

    Dimension d = getSize();
    int y = (d.height - 3) / 2;
    g.setColor(Color.white);
    g.drawLine(1, y, d.width - 1, y);
    y++;
    g.drawLine(0, y, 1, y);
    g.setColor(Color.gray);
    g.drawLine(d.width - 1, y, d.width, y);
    y++;
    g.drawLine(1, y, d.width - 1, y);

    String text = getText();
    if (text.length() == 0)
        return;

    g.setFont(getFont());
    FontMetrics fm = g.getFontMetrics();
    y = (d.height + fm.getAscent()) / 2;
    int fontWidth = fm.stringWidth(text);

    g.setColor(getBackground());
    g.fillRect(OFFSET - 5, 0, OFFSET + fontWidth, d.height);

    g.setColor(getForeground());
    g.drawString(text, OFFSET, y);
}

From source file:uk.co.modularaudio.mads.base.oscilloscope.ui.OscilloscopeDisplayUiJComponent.java

private void paintIntoImage(final Graphics g, final OscilloscopeWriteableScopeData scopeData) {
    //      Graphics2D g2d = (Graphics2D)g;
    //      Rectangle clipBounds = g.getClipBounds();
    final Font f = g.getFont();
    final Font newFont = f.deriveFont(12);
    g.setFont(newFont);
    final int x = 0;
    final int y = 0;

    newMaxMag0 = 0.5f;/*ww  w .java2 s. c o  m*/
    newMaxMag1 = 0.5f;

    final int width = getWidth();
    final int height = getHeight();

    final int plotWidth = width - (SCALE_WIDTH * 2);
    final int plotStart = SCALE_WIDTH;
    final int plotHeight = getHeight();

    //      g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    g.setColor(Color.BLACK);
    g.fillRect(x, y, width, height);

    if (scopeData != null) {

        final int numSamplesInBuffer = scopeData.desiredDataLength;

        final float[] float0Data = scopeData.floatBuffer0;
        final FloatType float0Type = scopeData.floatBuffer0Type;
        switch (float0Type) {
        case AUDIO:
            maxMag0 = 1.0f;
            break;
        case CV:
            break;
        }
        final boolean displayFloat0 = scopeData.float0Written;

        final float[] float1Data = scopeData.floatBuffer1;
        final FloatType float1Type = scopeData.floatBuffer1Type;
        switch (float1Type) {
        case AUDIO:
            maxMag1 = 1.0f;
            break;
        case CV:
            break;
        }
        final boolean displayFloat1 = scopeData.float1Written;

        if (displayFloat0) {
            // Float data draw
            g.setColor(Color.RED);
            drawScale(g, height, maxMag0, 0);
            plotAllDataValues(g, plotWidth, plotStart, plotHeight, numSamplesInBuffer, float0Data, true);
        }

        if (displayFloat1) {
            // CV data draw
            g.setColor(Color.YELLOW);
            drawScale(g, height, maxMag1, plotStart + plotWidth);
            plotAllDataValues(g, plotWidth, plotStart, plotHeight, numSamplesInBuffer, float1Data, false);
        }
        maxMag0 = newMaxMag0;
        maxMag1 = newMaxMag1;
    }
}

From source file:com.yanbang.portal.controller.PortalController.java

/**
 * ???/*from w  ww  .jav  a 2 s. c o  m*/
 * 
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(params = "action=handleRnd")
public void handleRnd(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0L);
    response.setContentType("image/jpeg");
    BufferedImage image = new BufferedImage(65, 25, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, 65, 25);
    g.setColor(Color.yellow);
    Font font = new Font("", Font.BOLD, 20);
    g.setFont(font);
    Random r = new Random();
    String rnd = "";
    int ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 5, 18);
    g.setColor(Color.red);
    ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 20, 18);
    g.setColor(Color.blue);
    ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 35, 18);
    g.setColor(Color.green);
    ir = r.nextInt(10);
    rnd = rnd + "" + ir;
    g.drawString("" + ir, 50, 18);
    request.getSession().setAttribute("RND", rnd);
    ServletOutputStream out = response.getOutputStream();
    out.write(ImageUtil.imageToBytes(image, "gif"));
    out.flush();
    out.close();
}

From source file:AppletMenuBarDemo.java

/**
 * This method is called to draw tell the component to redraw itself. If we
 * were implementing a Swing component, we'd override paintComponent()
 * instead// www .j av a 2  s.  co m
 */
public void paint(Graphics g) {
    if (remeasure)
        measure(); // Remeasure everything first, if needed

    // Figure out Y coordinate to draw at
    Dimension size = getSize();
    int baseline = size.height - margins.bottom - descent;
    // Set the font to draw with
    g.setFont(getFont());
    // Loop through the labels
    int nummenus = labels.size();
    for (int i = 0; i < nummenus; i++) {
        // Set the drawing color. Highlight the current item
        if ((i == highlightedItem) && (highlightColor != null))
            g.setColor(getHighlightColor());
        else
            g.setColor(getForeground());

        // Draw the menu label at the position computed in measure()
        g.drawString((String) labels.elementAt(i), startPositions[i], baseline);
    }

    // Now draw a groove at the bottom of the menubar.
    Color bg = getBackground();
    g.setColor(bg.darker());
    g.drawLine(0, size.height - 2, size.width, size.height - 2);
    g.setColor(bg.brighter());
    g.drawLine(0, size.height - 1, size.width, size.height - 1);
}

From source file:Clock.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Color colorRetainer = g.getColor();

    g.setColor(getBackground());//from  www. ja  v a2  s. c o  m
    g.fillRect(0, 0, getWidth(), getHeight());
    getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

    calendar.setTime(new Date()); // get current time
    int hrs = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);

    g.setColor(getForeground());
    if (isDigital) {
        String time = "" + hrs + ":" + min;
        g.setFont(getFont());
        FontMetrics fm = g.getFontMetrics();
        int y = (getHeight() + fm.getAscent()) / 2;
        int x = (getWidth() - fm.stringWidth(time)) / 2;
        g.drawString(time, x, y);
    } else {
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        int rh = getHeight() / 4;
        int rm = getHeight() / 3;

        double ah = ((double) hrs + min / 60.0) / 6.0 * Math.PI;
        double am = min / 30.0 * Math.PI;

        g.drawLine(x, y, (int) (x + rh * Math.sin(ah)), (int) (y - rh * Math.cos(ah)));
        g.drawLine(x, y, (int) (x + rm * Math.sin(am)), (int) (y - rm * Math.cos(am)));
    }

    g.setColor(colorRetainer);
}

From source file:FontPanel.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Font f = new Font("SansSerif", Font.BOLD, 14);
    Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);
    FontMetrics fm = g.getFontMetrics(f);
    FontMetrics fim = g.getFontMetrics(fi);

    String s1 = "Java ";
    String s2 = "Source and Support";
    String s3 = " at www.java2s.com";
    int width1 = fm.stringWidth(s1);
    int width2 = fim.stringWidth(s2);
    int width3 = fm.stringWidth(s3);

    Dimension d = getSize();//from  w w w.j av  a 2  s  .c o m
    int cx = (d.width - width1 - width2 - width3) / 2;
    int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent();

    g.setFont(f);
    g.drawString(s1, cx, cy);
    cx += width1;
    g.setFont(fi);
    g.drawString(s2, cx, cy);
    cx += width2;
    g.setFont(f);
    g.drawString(s3, cx, cy);
}

From source file:AccessibleScrollDemo.java

public void paintComponent(Graphics g) {
    Rectangle drawHere = g.getClipBounds();

    // Fill clipping area with dirty brown/orange.
    g.setColor(new Color(230, 163, 4));
    g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);

    // Do the ruler labels in a small font that's black.
    g.setFont(new Font("SansSerif", Font.PLAIN, 10));
    g.setColor(Color.black);//  w  ww  .j  a v  a 2  s.  com

    // Some vars we need.
    int end = 0;
    int start = 0;
    int tickLength = 0;
    String text = null;

    // Use clipping bounds to calculate first tick
    // and last tick location.
    if (orientation == HORIZONTAL) {
        start = (drawHere.x / increment) * increment;
        end = (((drawHere.x + drawHere.width) / increment) + 1) * increment;
    } else {
        start = (drawHere.y / increment) * increment;
        end = (((drawHere.y + drawHere.height) / increment) + 1) * increment;
    }

    // Make a special case of 0 to display the number
    // within the rule and draw a units label.
    if (start == 0) {
        text = Integer.toString(0) + (isMetric ? " cm" : " in");
        tickLength = 10;
        if (orientation == HORIZONTAL) {
            g.drawLine(0, SIZE - 1, 0, SIZE - tickLength - 1);
            g.drawString(text, 2, 21);
        } else {
            g.drawLine(SIZE - 1, 0, SIZE - tickLength - 1, 0);
            g.drawString(text, 9, 10);
        }
        text = null;
        start = increment;
    }

    // ticks and labels
    for (int i = start; i < end; i += increment) {
        if (i % units == 0) {
            tickLength = 10;
            text = Integer.toString(i / units);
        } else {
            tickLength = 7;
            text = null;
        }

        if (tickLength != 0) {
            if (orientation == HORIZONTAL) {
                g.drawLine(i, SIZE - 1, i, SIZE - tickLength - 1);
                if (text != null)
                    g.drawString(text, i - 3, 21);
            } else {
                g.drawLine(SIZE - 1, i, SIZE - tickLength - 1, i);
                if (text != null)
                    g.drawString(text, 9, i + 3);
            }
        }
    }
}

From source file:org.kchine.r.server.impl.RGraphicsPanelRemote.java

public synchronized void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension d = getSize();//  w w  w.java2s . c o  m

    if (!d.equals(_lastSize)) {
        _lastResizeTime = System.currentTimeMillis();
        _lastSize = d;
        return;
    } else {

    }

    if (forceAntiAliasing) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

    int i = 0, j = _l.size();
    g.setFont(_gs.f);
    g.setClip(0, 0, d.width, d.height); // reset clipping rect
    g.setColor(Color.white);
    g.fillRect(0, 0, d.width, d.height);
    while (i < j) {
        GDObject o = (GDObject) _l.elementAt(i++);
        o.paint(this, _gs, g);
    }
}

From source file:org.fcrepo.localservices.imagemanip.ImageManipulation.java

/**
 * Method automatically called by browser to handle image manipulations.
 * //ww  w . ja  v a  2 s.  c  o m
 * @param req
 *        Browser request to servlet res Response sent back to browser after
 *        image manipulation
 * @throws IOException
 *         If an input or output exception occurred ServletException If a
 *         servlet exception occurred
 */
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    System.setProperty("java.awt.headless", "true");
    // collect all possible parameters for servlet
    String url = req.getParameter("url");
    String op = req.getParameter("op");
    String newWidth = req.getParameter("newWidth");
    String brightAmt = req.getParameter("brightAmt");
    String zoomAmt = req.getParameter("zoomAmt");
    String wmText = req.getParameter("wmText");
    String cropX = req.getParameter("cropX");
    String cropY = req.getParameter("cropY");
    String cropWidth = req.getParameter("cropWidth");
    String cropHeight = req.getParameter("cropHeight");
    String convertTo = req.getParameter("convertTo");
    if (convertTo != null) {
        convertTo = convertTo.toLowerCase();
    }
    try {
        if (op == null) {
            throw new ServletException("op parameter not specified.");
        }
        String outputMimeType;
        // get the image via url and put it into the ImagePlus processor.
        BufferedImage img = getImage(url);
        // do watermarking stuff
        if (op.equals("watermark")) {
            if (wmText == null) {
                throw new ServletException("Must specify wmText.");
            }
            Graphics g = img.getGraphics();
            int fontSize = img.getWidth() * 3 / 100;
            if (fontSize < 10) {
                fontSize = 10;
            }
            g.setFont(new Font("Lucida Sans", Font.BOLD, fontSize));
            FontMetrics fm = g.getFontMetrics();
            int stringWidth = (int) fm.getStringBounds(wmText, g).getWidth();
            int x = img.getWidth() / 2 - stringWidth / 2;
            int y = img.getHeight() - fm.getHeight();
            g.setColor(new Color(180, 180, 180));
            g.fill3DRect(x - 10, y - fm.getHeight() - 4, stringWidth + 20, fm.getHeight() + 12, true);
            g.setColor(new Color(100, 100, 100));
            g.drawString(wmText, x + 2, y + 2);
            g.setColor(new Color(240, 240, 240));
            g.drawString(wmText, x, y);
        }
        ImageProcessor ip = new ImagePlus("temp", img).getProcessor();
        // if the inputMimeType is image/gif, need to convert to RGB in any case
        if (inputMimeType.equals("image/gif")) {
            ip = ip.convertToRGB();
            alreadyConvertedToRGB = true;
        }
        // causes scale() and resize() to do bilinear interpolation
        ip.setInterpolate(true);
        if (!op.equals("convert")) {
            if (op.equals("resize")) {
                ip = resize(ip, newWidth);
            } else if (op.equals("zoom")) {
                ip = zoom(ip, zoomAmt);
            } else if (op.equals("brightness")) {
                ip = brightness(ip, brightAmt);
            } else if (op.equals("watermark")) {
                // this is now taken care of beforehand (see above)
            } else if (op.equals("grayscale")) {
                ip = grayscale(ip);
            } else if (op.equals("crop")) {
                ip = crop(ip, cropX, cropY, cropWidth, cropHeight);
            } else {
                throw new ServletException("Invalid operation: " + op);
            }
            outputMimeType = inputMimeType;
        } else {
            if (convertTo == null) {
                throw new ServletException("Neither op nor convertTo was specified.");
            }
            if (convertTo.equals("jpg") || convertTo.equals("jpeg")) {
                outputMimeType = "image/jpeg";
            } else if (convertTo.equals("gif")) {
                outputMimeType = "image/gif";
            } else if (convertTo.equals("tiff")) {
                outputMimeType = "image/tiff";
            } else if (convertTo.equals("bmp")) {
                outputMimeType = "image/bmp";
            } else if (convertTo.equals("png")) {
                outputMimeType = "image/png";
            } else {
                throw new ServletException("Invalid format: " + convertTo);
            }
        }
        res.setContentType(outputMimeType);
        BufferedOutputStream out = new BufferedOutputStream(res.getOutputStream());
        outputImage(ip, out, outputMimeType);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                e.getClass().getName() + ": " + e.getMessage());
    }
}

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  .  j a  va2  s  .c  o  m
    }
    return s.toString();
}