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:SWTTest.java

/**
 * Prepare the AWT offscreen image for the rendering of the rectangular
 * region given as parameter./*from  ww w  .ja v a2 s .  co  m*/
 */
private void prepareRendering(int clipX, int clipY, int clipW, int clipH) {
    // check that the offscreen images are initialized and large enough
    checkOffScreenImages(clipW, clipH);
    // fill the region in the AWT image with the transparent color
    java.awt.Graphics awtGraphics = awtImage.getGraphics();
    awtGraphics.setColor(new java.awt.Color(TRANSPARENT_COLOR));
    awtGraphics.fillRect(clipX, clipY, clipW, clipH);
}

From source file:Main.java

protected void paintComponent(Graphics g) {
    // this will paint the background
    super.paintComponent(g);

    Color oldColor = g.getColor();
    g.setColor(isEnabled() ? getForeground() : getForeground().brighter());

    // paint the arrows
    int w = getSize().width;
    int h = getSize().height;
    for (int i = 0; i < arrowCount; i++) {
        paintArrow(g, (w - arrowSize/*from  ww  w  .j  a v a  2 s.  c o  m*/
                * (direction == SwingConstants.EAST || direction == SwingConstants.WEST ? arrowCount : 1)) / 2
                + arrowSize * (direction == SwingConstants.EAST || direction == SwingConstants.WEST ? i : 0),
                (h - arrowSize * (direction == SwingConstants.EAST || direction == SwingConstants.WEST ? 1
                        : arrowCount)) / 2
                        + arrowSize * (direction == SwingConstants.EAST || direction == SwingConstants.WEST ? 0
                                : i),
                g.getColor());
    }

    g.setColor(oldColor);
}

From source file:com.cburch.draw.tools.LineTool.java

@Override
public void draw(Canvas canvas, Graphics g) {
    if (active) {
        Location start = mouseStart;
        Location end = mouseEnd;/*from w  ww .j a va2s  .  co m*/
        g.setColor(Color.GRAY);
        g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
    }
}

From source file:com.cburch.logisim.circuit.appear.AppearanceAnchor.java

@Override
public void paint(Graphics g, HandleGesture gesture) {
    Location location = getLocation();
    int x = location.getX();
    int y = location.getY();
    g.setColor(SYMBOL_COLOR);
    g.drawOval(x - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS);
    Location e0 = location.translate(facing, RADIUS);
    Location e1 = location.translate(facing, RADIUS + INDICATOR_LENGTH);
    g.drawLine(e0.getX(), e0.getY(), e1.getX(), e1.getY());
}

From source file:MyButtonUI.java

public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    Dimension d = b.getSize();/*from www  .  jav a 2s.  c om*/

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();

    g.setColor(b.getForeground());
    String caption = b.getText();
    int x = (d.width - fm.stringWidth(caption)) / 2;
    int y = (d.height + fm.getAscent()) / 2;
    g.drawString(caption, x, y);

}

From source file:TrackFocusDemo.java

protected void paintComponent(Graphics graphics) {
    Graphics g = graphics.create();

    //Draw in our entire space, even if isOpaque is false.
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, image == null ? 125 : image.getWidth(this), image == null ? 125 : image.getHeight(this));

    if (image != null) {
        //Draw image at its natural size of 125x125.
        g.drawImage(image, 0, 0, this);
    }/*from w  w w . jav a 2s  .co  m*/

    //Add a border, red if picture currently has focus
    if (isFocusOwner()) {
        g.setColor(Color.RED);
    } else {
        g.setColor(Color.BLACK);
    }
    g.drawRect(0, 0, image == null ? 125 : image.getWidth(this), image == null ? 125 : image.getHeight(this));
    g.dispose();
}

From source file:org.openlegacy.rpc.render.DefaultRpcImageRenderer.java

private void drawText(String source, Graphics graphics) {
    source = source.replaceAll("\r", "");
    String[] lines = source.split("\n");
    int rowNumber = 1;
    graphics.setColor(imageBackgroundColor);
    for (String line : lines) {
        int startY = toHeight(rowNumber);

        if (drawLineNumbers) {
            // draw row number
            graphics.setColor(imageSorroundingTextColor);
            graphics.drawString(String.valueOf(String.format("%2d", rowNumber)), 0, startY);
        }/*from ww w.j a  v a  2 s . c om*/

        line = line.replaceAll("\t", "  ");
        graphics.drawString(line, 100, startY);
        rowNumber++;
    }
}

From source file:TextLayoutLeft.java

public void paint(Graphics g) {
    d = getSize();//w w  w.jav a 2  s .c o m
    g.setFont(f);
    if (fm == null) {
        fm = g.getFontMetrics();
        ascent = fm.getAscent();
        fh = ascent + fm.getDescent();
        space = fm.stringWidth(" ");
    }
    g.setColor(Color.black);
    StringTokenizer st = new StringTokenizer(
            "this is a text. this is a test <BR> this is a text. this is a test");
    int x = 0;
    int nextx;
    int y = 0;
    String word, sp;
    int wordCount = 0;
    String line = "";
    while (st.hasMoreTokens()) {
        word = st.nextToken();
        if (word.equals("<BR>")) {
            drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
            line = "";
            wordCount = 0;
            x = 0;
            y = y + (fh * 2);
        } else {
            int w = fm.stringWidth(word);
            if ((nextx = (x + space + w)) > d.width) {
                drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
                line = "";
                wordCount = 0;
                x = 0;
                y = y + fh;
            }
            if (x != 0) {
                sp = " ";
            } else {
                sp = "";
            }
            line = line + sp + word;
            x = x + space + w;
            wordCount++;
        }
    }
    drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFNumberColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;//from   www.jav a2 s .  c  om
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof BigDecimal) {
        BigDecimal val = (BigDecimal) value;
        Format fmt = getNumberFormat(digits, precis);
        str = fmt.format(val);
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:components.Rule.java

protected 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);/*from w  w w  . j  a va  2s  . co  m*/

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

    // Use clipping bounds to calculate first and last tick locations.
    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);
            }
        }
    }
}